Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't font-size equal width and height in CSS?

Tags:

html

css

I have the following HTML code:

<p id="main">
    H  
</p>

and the following CSS:

p {
  border-style: solid;
  border-color: black;
  border-width: 1em;
  font-size: 1em;
  height: 1em;
  width: 1em;
  padding: 0em;
}

Why doesn't the height and width of content box equal the font-size. Why is there a gap between the letter H and the border as in the following output.

Output

like image 414
Kartik Anand Avatar asked Mar 19 '15 11:03

Kartik Anand


People also ask

Why height and width is not working in CSS?

Spans are display: inline; by default. To get them to listen to a height and width, you'll need to add display: block; . Save this answer.

How do I automatically adjust font size in CSS?

The font-size-adjust property of CSS tells the browser to adjust the font size of the text to the same size regardless of font family. When the first chosen font is not available, the font-size-adjust property allows you more control over the font size.

Why is my font size not changing in HTML?

To change the font size in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property font-size. HTML5 do not support the <font> tag, so the CSS style is used to add font size.


1 Answers

It's because thats not how fonts are designed. For example, the letter a contains quite a block of whitespace at the top, well, the same thing happens for that H. You can see it when you actually select text. The shape of the H is not the outline of what forms the character. Its an imaginary box defined by the font-designer.

Fonts are not just shapes, they are contained shapes. A font designer spends time thinking about how the font should look on multiple lines, so they calculate some spacing in their fonts as well.

This results in less-predictable type, but more soothing-to-the-eye type. Thats why many fonts will be adjusted to look just right, but mathematically they aren't. The best example is to see how rounded letters actually protrude from the bottom, simply because our eyes otherwise see it as wrong. This means that your box doesn't start at the baseline either.

Check this image if it helps (and yes, there are things that can be adjusted like line-height, but in essence this image show you the box you thought you had and the box that is actually there):

enter image description here

In short, fonts are designed to be legible and not easily computable.

Update: Another image

This link has died. RIP, link

Just the add, @Paulie_D has a great illustration in the comments to this question, and for future reference it might also be good to have a look at it: http://www.smashingmagazine.com/wp-content/uploads/2010/05/type-002.gif

like image 166
somethinghere Avatar answered Nov 14 '22 12:11

somethinghere