Question

This is undoubtedly a stupid question but i'm having a bad day and it's confusing me!

If you view http://jsfiddle.net/E6kGP/1/ then you can see 2 simple divs next to each other each of which contains a p tag each with different font sizes and matching line-heights.

There is a small gap between the top of the p container and the top of the contained text which is different depending on the font sizes (and line-heights). This means that the top of the text in each p is not vertically aligned. If the line-heights didn't match the font-sizes then I could understand this but surely if they are the same then the line-heights should match the tallest character and hence the highest point of the first line should be the top of the p container? Obviously this can be hacked using padding/margins or absolute positioning but i would like to understand why this doesn't work by default and what the correct way is to fix it?

As requested by SO the code from jsfiddle is also below:

div {float: left; width: 50%;}
p {margin:0 0 1em;padding:0;}
#left p {line-height:36px;font-size:36px;}
#right p {line-height:16px;font-size:16px;}

<div id="left">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
<div id="right">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>

Thanks very much as ever everyone!

Was it helpful?

Solution

It might be to accommodate accented characters, try putting Ä into the first <p>, the extra space helps accommodate the accent. That said, I'm not 100% convinced that is the definitive reason.

You can always specifically target the first line of a <p> element to reduce it using:

p::first-line {
    line-height: 0.8em;
}

Though granted, that doesnt solve the 'why' issue.

OTHER TIPS

Like ExtPro has said, it's to accommodate accented characters. A simple work around is to have margin-top:-<number>px; so that you manually align it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top