Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the div bigger than the font-size?

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?

like image 206
ubershmekel Avatar asked Sep 01 '14 20:09

ubershmekel


People also ask

How do I reduce the size of a div?

A div's height depends on its inner elements, in your example, the first input is having a height of 100px, so the div will have at least 100px height, ignoring spacing, boarder, padding. Setting max-height on the div will hint the rendering engine to limit the height, but doesn't necessarily work all the time.

How do I change the font-size inside a div?

To change the font size of a div using JavaScript, get reference to the div element, and assign required font size value to the element. style. fontSize property.

What determines the size of a div?

By default a div is a block element, it means its width will be 100% of its container width, and its height will expand to cover all of its children. In case its children has a larger width than the div 's container width, the div itself does not expand horizontally but its children spill outside its boundary instead.

How do I keep my div the same size?

if you set width:500px; and height:300px; (or whatever size you want) the div should stay the same size. Also, you can set overflow:hidden; so that if whatever is in the div goes beyond those bounds, it is not shown, and does not resize the div. Save this answer. Show activity on this post.


2 Answers

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:

  • Calculate line-height with font in rem-value
like image 88
Hashem Qolami Avatar answered Sep 28 '22 08:09

Hashem Qolami


Maybe you need line-height: 16px;

like image 40
Gabriele Prestifilippo Avatar answered Sep 28 '22 08:09

Gabriele Prestifilippo