Forum Moderators: not2easy

Message Too Old, No Replies

Beginner's Q: How to 'clip' the width of a div to its content

instead of the available width

         

davidbraun

1:34 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



Hello

Sorry if this has been adressed before - and I'm sure it has been - I just couldn't find it...

How should a <div> be defined to have it stop at it's contents end? If I just throw in

<div style="border: solid black 1px"><p>something</p></div>

the <div> is extended to the windows available width, which is annoying.

TIA
david

phidentity

6:52 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



width: auto;

From just looking.

However I may have read your question totally wrong!

Jon

MWpro

6:56 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



Welcome to WebmasterWorld, davidbraun!

This was actually a very good question, not a beginner's one at all.

The width of a div, unless otherwise set, is 100%. So if you wanted to have it smaller, you would have to specifically set the width as a different value.

Or instead of the div, which is a block element, try using a span, which is an inline element.

<span style="border: 1px solid #000;">
something
</span>

edit: technically width: auto; should work, but it doesn't. I think, and am sure DrDoc will correct me if I am wrong, that setting a width of auto only works for inline elements, and because span is an inline element, that's why it will give davidbraun the effect that he wants.

davidbraun

7:53 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



Hello and Thanks for the answers.

I haven't checked on all available browsers (it seems that what for safari is "look ma, no hands" is the same for the mozilla/netscape families), but Microsofts Browser (of course...) doesn't do it that easily. It expands a div to the end of the screen (where no browser has gone before). Since IE is THE majority, no need to look further, it can be considered a nogo IMO.

My problem is that I need a block-level element (my goal is to have a fluid, indefinitely resizable, rounded corner box, drop-shadow included), because it'll contain further block-elements, such as lists, paragraphs etc.

As for now, I have to live with the majority not getting what I was planning to deliver. (or find a different solution)

Still, I'm open to further suggestions :)

Thanks again

david