I want the width of a div element to be equal to the width of the content inside it, and also (and more importantly) that any content after this div does not start to the right of the div(as though the div was float:left;
), but display below the current div.
I know one way is that after this div, I create another empty div and set clear:both;
, but in my case that would not be very preferable.
Asked
Active
Viewed 108 times
0
1 Answers
2
I think that the following may be close to what you need:
The HTML demo code is:
<div>
Some text may be before the element.
<div id="info">This is my bible!</div>And some text may follow. the element.
</div>
And the CSS styles are:
#info {
display:inline;
background-color:#CFFF9F;
color:black;
font-weight:normal;
border:black solid 1px;
}
#info:after {
content:"\A";
white-space: pre;
}
Fiddle reference: http://jsfiddle.net/audetwebdesign/6FNQc/
Explanation of How This Works
Use :after
to generate some content (known as a pseudo-element in CSS). The "\A" is interpreted as a line break (linefeed) provided that the white space is preserved, hence you need to set white-space: pre
. Finally, the element has to be inline, hence display: inline
.
Related References:
CSS to line break before/after a particular `inline-block` item
To learn more about "\A" (linefeed), see:
http://www.w3.org/TR/CSS21/syndata.html#strings

Community
- 1
- 1

Marc Audet
- 46,011
- 11
- 63
- 83
` after your current div? – Jon Mar 20 '13 at 11:41
` tags! Any CSS solution? – gthuo Mar 20 '13 at 11:46