What is the reason behind the default font size being longer than 1em?

During my experimentation, I encountered an issue. I set the height of a div to 1em, expecting it to contain the entire text within. However, it did not. In my browser, 1em is equivalent to 16px.

On not specifying the height of the div, it successfully contained the full height of the text, measured at 19px when inspected.

I had thought that em represented the default font height of the browser. Was my understanding incorrect?

div {
  margin: 0;
  padding: 0;
}
.first {
  height: 1em;
  background: green;
}
.second {
  background: orange;
}
<div class="first">سلامًا Say I Wantأًّ</div>
<br />
<div class="second">سلامًا Say I Wantأًّ</div>

https://jsfiddle.net/9t43kyu2/1/

Answer №1

When it comes to typography, the line-height of text doesn't always match the actual height of the rendered text in pixels:

https://i.sstatic.net/B7kpq.png

The CSS parameter for line-height only encompasses the distance between "caps height" and the "baseline". Typically, this is 1em, but can vary on different sources online. More detailed explanations are available in @web-tiki's insightful response.

If characters protrude above or below the standard line, the overall height of the text will exceed the line height.

Text may contain subtle details that extend beyond the regular text line, such as the descender of a y or the ascenders of non-ASCII characters like Ű. These intricacies often lead to positioning challenges.

If you leave the div height unspecified, it defaults to auto, accommodating the entire content. However, defining the div height to match the exact line size without padding, border, or margin might result in some text overflowing. This overflow may go unnoticed due to the default value of the overflow CSS property being set to visible.

To test this scenario, create a div with the following specifications:

#divId {
  height: 1em;
  line-height: 1em;
  overflow: hidden;
}

...and insert a (or any characters) into its content. You will observe the text being clipped on both sides.

Answer №2

The reason for the difference in height between the two divs is due to the default line-height property, which has a default value of 'normal'.

normal
User agents set the used value to a "reasonable" value based on the font of the element. The recommended range for the used value of 'normal' is between 1.0 and 1.2.[...] [source w3.org]

This causes the second div to be approximately 1.2em high depending on the user agent. To observe the difference, you can set its line-height to 1em:

div {
  margin: 0;
  padding: 0;
}
.first {
  height: 1em;
  background: green;
}
.second {
  background: orange;
  line-height: 1em;
}
<div class="first">سلامًا Say I Wantأًّ</div>
<br />
<div class="second">سلامًا Say I Wantأًّ</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The carousel spun around, each section moving to the side on its own

One issue I'm facing is that on my page, I have multiple carousel rows. However, when I click on the "next" or "prev" button to navigate through the items in the carousel, it affects all carousels instead of just the one I clicked on. I've attem ...

Determine the height of the input group when a span, input, and button are included

I have been attempting to create a 3 control input-group using Bootstrap 3.3.7, but I am encountering an issue where the height of the first addon does not match the height of the input field or button that follow. <div class="input-group"> < ...

Replace the checkbox display heading with a text box in the designated area

I'm new to using kendo ui Currently, I am attempting to show a text box in the first column header. Instead of the checkboxDisplay heading, I want to replace it with a text box. Could someone please provide guidance on how to resolve this issue? Here ...

Transitioning the implicit width

Using implicit width, which involves adding padding to an element containing text to give the parent container a specific but unstated width, can be quite elegant. However, it poses challenges when it comes to transitions. Is there a way to achieve a trans ...

Issue with editing JavaScript and HTML code

I am facing an issue with modifying my code. I want to make changes so that it not only fulfills its current purpose but also counts from 1 to a specified number. The current code is in Polish and is quite simple. It currently calculates the factorial of ...

Guide on making a Vue.js show/hide button for each element on a webpage

There is a feature to toggle between displaying "more" or "less" text, but currently the @click event affects all elements causing them to show all the text at once. I realize that I need to pass a unique value to distinguish each element, but I am curren ...

Bootstrap 5 error: Tooltip is placed too high when using data-bs-placement="top"

After upgrading a project from Bootstrap 4 to 5, I encountered a tooltip issue. The tooltips were perfectly positioned on the component when using left, right, or bottom placements, but they appeared too high when using top placement. Interestingly, the t ...

Using Paper Checkbox to Toggle the Visibility of a Div in Polymer

Having experience with Meteor, I typically used JQuery to toggle the display of a div along with paper-checkbox: HTML: <paper-checkbox name="remoteLocation" id="remote-chk" checked>Remote Location</paper-checkbox> <div id="autoUpdate" clas ...

Is there a way to determine if the viewport has scrolled to a specific HTML element?

In my React project with Styled Components, I have a div that contains an image. I am looking to add animation to the image when the user scrolls down and reaches the img element. Any suggestions on how to achieve this would be greatly appreciated! Thank ...

What could be causing this Ajax error I'm experiencing?

I am attempting to dynamically load pages on this website using JQuery and Ajax when a menu point is clicked. However, I am encountering errors during the loading process. What does the console message ""GET 'path to css file'" mean? (there ...

Exploring the world with HTML5 Geolocation on Google Maps

I've recently been studying a tutorial on HTML5 geolocation and I'm looking to make some modifications to the code. Specifically, I want to add a text input for the destination address instead of having a fixed destination. Any suggestions or met ...

Deactivating form elements using jQuery

I am attempting to utilize jQuery in order to disable a form element once a checkbox is clicked, but for some reason it's not working properly. Can someone help me identify the issue? $('name="globallyAddTime"').click(function() { if ($ ...

Unable to detect any errors in the CSS file

Using RSpec to write some feature tests, I encountered an error in my application where a pre-purchased template with numerous styles is causing issues. Upon running the specs, the following error occurred: ActionView::Template::Error: Invalid CSS ...

I want to create a custom jQuery slider totally from scratch

Greetings everyone, I have been tasked with creating a slider using only HTML / jQuery code. Here is the template: And here is the HTML code for the template above: <div id="viewport-container"> <section id="sliding-container"> & ...

Issue with resizing keyboard/dropdown popup in Android hybrid app when navigating offscreen

Here is the offscreen navigation menu I have set up. When I open a keyboard or dropdown, the offscreen menu changes and exhibits a small gap on the left side after the popup (highlighted in red). It seems like the menu is shifting further away from the ed ...

Position 2 identical classes side by side on the horizontal axis

Currently enrolled in the MIMO HTML course and facing a challenge where I am unable to align both elements with the class="column" attribute horizontally using display: inline-block; I have attempted using float:right and other CSS properties to achieve a ...

Discover the secrets to replicating the mesmerizing horizontal scrolling menu akin to the

What is the most effective way to create a horizontal menu similar to the innovative Google picture menu? Could someone kindly share their knowledge and provide the code for achieving the same outcome? ...

Deal with Ajax request only once

I am currently working with a table that displays rows, as shown in the image below. When the "order online" link is clicked, a form appears just below the respective row, similar to the second image. FIRST IMAGE SECOND IMAGE My current challenge invo ...

Issue with Angular 2 NgFor Pattern Error Message Display Absence

I am attempting to incorporate inputs with a regex requirement within an ngFor loop, but I am not receiving the expected error message when entering something that does not match the required pattern. Even when I input an incorrect pattern, "Test" remains ...

What is the best way to align the text next to the initial input and expand the text close to the second input field?

I am an avid user of Bootstrap 4. BC stands as a global pioneer in online news and information, striving to educate, engage, and empower individuals worldwide. - this content should be positioned to the right of the top input field. https://i.sstatic.n ...