Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS layout changes with font-style

Tags:

html

css

Consider the following HTML markup. In most browsers that I have tested, the second list is displayed differently (each list item is indented).

The only difference between the two lists relates to the CSS font-style property, which I would not expect to change the list layout. Is there a explanation for this behavior?

<!DOCTYPE html>
<html>
    <head>
        <style>
            body {font-family: sans-serif}
            span {float: left}
            ul.bad span {font-style: italic}
        </style>
    </head>
    <body>
        <ul>
            <li><span>foo</span></li>
            <li><span>foo</span></li>
            <li><span>foo</span></li>
            <li><span>foo</span></li>
        </ul>
        <ul class="bad">
            <li><span>foo</span></li>
            <li><span>foo</span></li>
            <li><span>foo</span></li>
            <li><span>foo</span></li>
        </ul>
    </body>
</html>
like image 889
Josh Avatar asked Oct 10 '22 13:10

Josh


1 Answers

The span span with the straight text becomes 18 pixels in height, while the italic text forces the span to 19 pixels.

This causes a slightly different behaviour when using float: left.

      span // <-- height: 18px;
 .bad span // <-- height: 19px;

a quick fix would be to set the line height property equal for both span types:

  span {float:left; line-height:15px;}

not regarding the intentions of your code ;)

like image 129
Caspar Kleijne Avatar answered Oct 20 '22 04:10

Caspar Kleijne