The problem you have is that you're using a border
, not an underline. The border extends the full length of the element, which for a div
is width: 100%
by default.
To change that you should limit the width of the div
explicitly, or by using float
or changing its display
.
Using width
:
div {
width: 10em; /* or whatever... */
}
JS Fiddle demo.
Using float
:
div {
float: left; /* or 'right' */
}
JS Fiddle demo.
Using display
:
div {
display: inline-block; /* or 'inline' */
}
JS Fiddle demo.
Of course, given that you effectively want the underline to be below the text and, presumably, serve to 'underline' the text (see the problem with the demo, using a defined width if the text is longer than the defined width), it'd be easier to simply use an in-line element, such as a span
for this, rather than a div
, since its default behaviour is the same behaviour that you want.