What is the functionality of the 'em' CSS unit when used with positioned elements?

Here is an example of HTML code featuring nested div tags and a p tag as the innermost element:

<!DOCTYPE html>
<html>
    <head>
        <title>How em unit works?</title>
        <link href="style.css" rel="stylesheet" type="text/css" media="all">
    </head>
    <body>
        <div>
            <div>
                <p> Some text</p>           
            </div>
        </div>
    </body>
</html>

If we apply the following styles to the elements:

body{
    font-size: 10px;  
}

body > div{
    font-size: 20px;

}

body > div > div{
    font-size: 50px;
}

p{
    font-size: 0.5em;   
    width: 6em;         
    height: 6em;        
    padding-right: 3em; 
    margin-left: 3em;   
    background-color: red;
}

The paragraph's text will have a font-size of 0.5 * 50px, which is calculated based on the parent's font size.

Now, imagine if we add position: absolute to the p element and change the outermost div to position: relative. How would this affect the behavior of the em unit for the p element?

Answer №1

When the p element is set to position: absolute, does the behavior of the em unit change?

No, the behavior remains the same.

The parent element will still be considered as the parent element.

Answer №2

When it comes to CSS sizing, the position of an element does not affect the em unit. This means that no matter where a paragraph is positioned, its font size in em units will remain constant. Personally, I prefer using rem units over em because em can sometimes lead to unpredictable results, especially if you are not meticulously managing all aspects of your design.

UPDATE: It seems like two other individuals already provided answers before me!

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

Enhancing the appearance of HTML code within x-template script tags in Sublime Text 3 with syntax highlighting

I recently updated to the latest version of sublime text (Version 3.1.1 Build 3176) and have encountered a problem with syntax highlighting for HTML code inside script tags. Just to provide some context, I'm using x-template scripts to build Vue.js c ...

The CSS transition timing seems to be malfunctioning as not all elements are smoothly transitioning, specifically the text within the li and anchor tags is not

I'm attempting to achieve a smooth transition effect on a navbar when hovering over it, changing the color from one to another. The navbar contains a list of words, but I am encountering an issue where during the transition, there is a quick flash (ap ...

Navigating a dynamic table by looping through its generated tr elements

I am currently working with a dynamically created tr table that includes individual rows of data and a fixed total sum at the bottom. The total sum does not change dynamically. var tmp = '<tr id="mytable"> <td id="warenid">'+data1.id ...

Show the initial instance following a nested class

I need the first .tab class that directly follows the .tab-wrapper class to be visible, while the others should remain hidden <ul class="tabs"> <li>1</li> <li>2</li> </ul> <div class="tab-wrapper"> &l ...

Attempting to ensure that my website is fully optimized for all devices and

I am currently facing an issue with my CSS code on iPad. I want to change the background-image displayed based on whether the user is using an iPad or not. Specifically, I want to use headerold.jpg as the background-image for iPads while keeping headernew. ...

Generating a JavaScript variable linked to a Rails Active Record relationship

I'm trying to implement an active record association in the DOM, but I'm encountering some difficulties. Here is my editor class: .editing .row .col-sm-3 .col-sm-8 .text-left #font-20 .title-font . ...

What is the best way to trigger a function with a bootstrap toggle?

A toggle switch has been implemented using bootstrap with the following code snippet: <label > Automatic Refresh: </label> <input class="pull-right" data-size="mini" type="checkbox" data- toggle="toggle"> When the toggle button is in ...

Updated navigation bar and optimized for mobile devices

I'm struggling to get my navigation menu to stick at the top and be responsive on mobile with a toggle bar icon. I'm using Bootstrap 4, and I've tried using sticky-top and fixed-top in the div classes, as well as position fixed in my header ...

Alignment issues with menus

Could someone help me with aligning my register and login buttons to the right? I will provide any necessary information upon request. Please note that this is just a small part of a larger menu with additional CSS styling. PHP/ HTML: <a href="<?ph ...

Tips for adjusting the size of a CSS radio button while ensuring the text remains centered

Uncertain about which part to modify in this radio button code to decrease its size while keeping the text aligned with the inner button. Changes made have shifted the text to the top of the button instead. Can you provide guidance on the necessary adjustm ...

How can a material progress spinner be inserted into the Angular Ag-Grid overlayNoRowsTemplate?

I'm attempting to include a mat-progress-spinner within my agGrid when there are no rows to display. Here's an example that works: private overlayNoRowsTemplate = '<p>No rows to show.</p>'; However, when I attempt to add ...

Guide on attaching an event to every dynamically created element in JavaScript

I'm currently generating 'li' elements dynamically using a loop and running into issues when it comes to assigning events to each generated element. My goal is to assign an onclick event to every li element that is created. Check out the co ...

Display the HTML element prior to initiating the synchronous AJAX request

I'm looking to display a <div> upon clicking submit before triggering $.ajax() Here's my HTML: <div id="waiting_div"></div> This is the CSS: #waiting_div { position: fixed; top: 0px; left: 0px; height: 100%; ...

Determine the state of the checkbox property in HTML

Is there a way to conditionally set the checked property of a checkbox? In my application, I have checkboxes for each row. The database includes a column 'IsSaved' with values of either 0 or 1. If 'IsSaved' is 1 for a particular row, I ...

What measures can be taken to restrict users from inputting decimal values?

My website includes an order page where users can input quantities for various items. While some items allow for decimal quantities, others do not. What is the most effective method to restrict users from entering decimal quantities? (Besides using an ale ...

Issue with extra spacing within a div element in Bootstrap 5

Why is there extra spacing on the right side of my div? The image and text are supposed to take up 50% of the screen each. However, for some reason, there is additional spacing on the right that prevents this from happening. I am using Bootstrap 5. Click ...

How can I include a ?ref query parameter in a URL when linking from external websites?

I've always wondered about how websites are able to include the ?ref parameter in their URLs when they are referred from another website. Do both sites need to incorporate this into their code? How does this function exactly? ...

Utilizing JavaScript XHR to Organize Data Retrieved from an API

I have successfully connected FDA's Drug API with XMLHttpRequest() and now I need help sorting the fetched data in ascending order based on its GENERIC NAME (results["open_fda"].generic_name). Importing all arrays from the API seems impractical, even ...

Guide to highlighting input field text using Angular

I've set up an angular page that includes an input field within its template. My goal is to highlight the text in the input field when a specific function is triggered. When I refer to "highlighting the text," I mean (check out the image below) https ...

When adjusting the month/year, the Material Date Picker extends beyond its container

Currently, I have an Angular 18 application with a page that includes a material date picker component. When I open the Date Picker, everything displays correctly. However, when I try to change the month using the left/right arrow or the year, the data co ...