Why is the div bigger than the font-size?

ubershmekel

See http://jsfiddle.net/6taruf65/1/

The following html appears as 20 pixels tall in Firefox31 and Chrome36 on Windows7. I expected it to be 16 pixels tall.

<style>
* { margin: 0; padding: 0; border: 0; overflow: hidden; vertical-align: baseline; }
</style>
<div style="font-size: 16px;">help 16px</div>

Notice the bottom of the p is cut off when you limit the div's height to 16px. That suggests to me there's unused space above the text. It might be a problem with vertical alignment. But then how would I go about preventing that issue when I want to precisely control the height and alignment of the text?

Hashem Qolami

This is because the default line-height value that is applied by the user agent. Some of web browsers apply a line-height of 1.2em or 1.2 or 120% to the elements while the spec recommends:

We recommend a used value for normal between 1.0 to 1.2.

CSS Level 2 Spec states:

line-height

On a block container element whose content is composed of inline-level elements, line-height specifies the minimal height of line boxes within the element. The minimum height consists of a minimum height above the baseline and a minimum depth below it, exactly as if each line box starts with a zero-width inline box with the element's font and line height properties.

The accepted values are normal | <number> | <length> | <percentage> | inherit

Hence, you could override the applied value by adding a line-height of 16px or simply a value of 100% or 1em or 1 to the element. (Click on each one to see the demo).

<number> - e.g. line-height: 1 - is the preferred value of line-height as it always refers to the element's font size. Therefore you don't have to specify different values for different font sizes.

For further info about the difference between these values, you could refer to my answer here:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is font-size: 16px bigger in Internet Explorer 11 than in FireFox?

Why is the size of npy bigger than csv?

Placeholder font-size bigger than 16px

setting the font-size makes the div bigger in a grid

How to make a div with scrollbar no bigger than 100% of the page size

bootstrap: Why div child is bigger than div parent?

python3 - why is size of string bigger than encode

Why is the received buffer size bigger than originally sent via socket?

Why is a JPEG file size of a PDF bigger than original PDF?

Why int datatype has a bigger size than float in python?

Background bigger than the div

Why is lineHeight of UIFont larger than font size?

why "a" is bigger than "A" in python?

Why div area still bigger more than image css?

Why is "size on disk" much bigger than "size" when I copy data from NTFS to exFAT?

Why is Spark broadcast exchange data size bigger than raw size on join?

Why do my containers size bigger than the screen size that causes the SingleChildScrollView() to scroll when the keyboard is down?

Why is the size of my email about a third bigger than the size of its attached files?

Why is the difference in file size and it's size on disk bigger than 4 KiB?

Placing div2 when the content is bigger than the size of div1

Make a div bigger than wrapper

div bigger than body tag

Frame size of UICollectionView is bigger than size of UIScreen

Why is the page bigger than the screen?

Font-size not getting bigger in CSS

Why does font size change the size and padding of parent div?

Why is 1em shorter than the default font size?

Why camera2 supported preview size width always bigger than height?

Why scale to fill give bigger image than UIImageVIew size? (using swift)