The problem is that you are using border , not underline. The border extends the entire length of the element, which by default for a div is width: 100% .
To change this, you must explicitly limit the width of the div with either a float or change its display .
Using width :
div { width: 10em; }
JS Fiddle demo .
Using float :
div { float: left; }
JS Fiddle demo .
Using display :
div { display: inline-block; }
JS Fiddle demo .
Of course, given that you really want the underline to be below the text and apparently serve to βunderlineβ the text (see the problem with the demo using a specific width if the text is longer than a given width ), it would be easier to just use the built-in an element like a span for this, not a div , because its default behavior is the same behavior you want.
David Thomas Nov 28 '12 at 19:28 2012-11-28 19:28
source share