Behaviors of size are specified differently in percentage and em/px units

My page consists of a navigation bar wrapped in a header element and a content frame that should be placed below it, both sharing the same width.

When I specify the header width using percentages, this is how the page appears:

However, when I define the width using ems or pixels, the layout looks like:

I'm curious why the percentage-based width is not working as expected?

HTML: (the "content" class is only used for color styling)

    <header id="page-header">

        <img id="logo" .../>

        <nav class="content" id="page-nav">
            <ul>
                <li><a href="#">...</a></li>
                ...
            </ul>
        </nav>

    </header>


    <section class="content" id="content-container">            
        ...
    </section>

CSS: (highlighting important parts)

    /*CSS reset*/
html, body, div, ul, li,
article, footer, header, nav, section {
    margin: 0;
    padding: 0;
    border: 0;
}

article, footer, header, nav, section {
   display: block;
}
/*end of CSS reset*/

/*header*/


#page-header {
    margin-bottom: 3em;
    height:20%; <----- this is where exactly I have a problem 
}

#logo {
    float:left;
    display:block;
    margin-left: 30px;
    width: 420px;
}
#page-nav
{
    margin-left:30%;
    margin-right:10%;
}

#page-nav a {
    float:left;
    display: block;

    width: 12.5%;
    min-width:100px;
    height: 2em;
    line-height: 2em;

    margin: 3em 1.04%;
    vertical-align: middle;

}

#page-nav li:first-child a {
    margin: 3em 1.04% 3em 0;
}

#page-nav li:last-child a {
    margin: 3em 0 3em 1.04%;
}



/*content frame*/

#content-container {

    margin-left: 30%;
    margin-right: 10%;
    margin-bottom: 5%;

    padding: 2em 6em;
}

Answer №1

When using percentage heights in CSS, it's important to remember that they are relative to the first container with a defined position: relative. If no parent containers have this property set, then the height is based on the html/body tags. If your 20% height isn't appearing as expected, check if the parent containers have specified heights.

To fix this issue, add position: relative to the parent containers and adjust the height of #page-header accordingly. If the parent container is the html or body element, you can set their height to 100% like so:

html, body {
    height: 100%;
}

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

After I click on an operator button, the display on my HTML calculator does not reset

function aClick(){ if(a === undefined){ numberButtons.forEach(button => { button.addEventListener("click", addNumber) }); operatorButtons.forEach(button => { button.addEventListener(' ...

What is the best way to align HTML elements in a single row?

I have the following code snippet... <div class="header"> <div class="mainh"> <div class="table"> <ul> <li><a>smth</a></li> ...

Prevent navigation away from textarea using tab key

In my React editor project, I am trying to prevent the tab button from switching to the next field when used within a textarea. I have attempted various solutions, such as: <textarea className={style.textarea} required tabIndex="-1" ...

Fluid Centered UL with Bullet Points

We have received a unique request from one of our clients. They are asking for the content within a specific <ul> to be centered along with the icons. Centering and adding icons are doable, but the challenge lies in centering the <li> elements ...

Looping through iterations to create circular patterns

How can I fix my code to draw multiple circles in a loop instead of just one? var ss_links_canvas = document.getElementById("ss_links_canvas"); ss_links_canvas.width = images.length * 41; ss_links_canvas.height = 25; var ss_links = ss_links_canvas.getCont ...

Create a dynamic MUI icon that adapts to different screen sizes

Struggling to make the MUI DeleteIcon responsive and clickable. <ListItem> <ListItemAvatar> <Avatar src={sprites.front_default}> <ImageIcon /> </Avatar> </ListItemAvatar> <ListItemText pr ...

The Bootstrap carousel's transition effect is not functioning as expected

The fade transition I added to my Bootstrap 4 carousel isn't working properly. It seems like the effect is not being applied to the carousel. Here is the HTML code snippet: <div id="testimonialsSlides" class="carousel carousel-fade" data-ride="fa ...

What is the best way to stretch an image across the entire screen using Bootstrap?

I attempted to embed the image inside a jumbotron and a container, but unfortunately, my latest attempt did not produce the desired result. Here's the code snippet that I tried: <div class="row"> <div class="full-width-div"> <div ...

What is the best way to align two items where one is centered and the other is positioned to the left?

In an attempt to customize a chat call to action, I am looking to have an icon aligned to the left and some centered text. Here is an example of how I envision it: [ @ Text aligned in center ] Here is what I have tried so far: .icon{ justify-self: ...

What is the best way to customize the appearance of a mat-checkbox in Angular using Angular Material?

Is there a way to customize the appearance of a mat-checkbox that actually works? I've tried various solutions without success. I attempted to disable view encapsulation and use classes like mdc-checkbox__background, but nothing changed. Even applyin ...

Activate the display by utilizing the getElementsByClassName method

In my design, I'd like to have the "Don't have an account?" line trigger the display of the .registrybox class when clicked. However, the script I've written doesn't seem to be working as intended. The script is meant to hide the .login ...

Dynamic image gallery with a flexible layout and interactive scrollbar using Flexbox

I'm looking to incorporate a scrolling image gallery into a flexbox layout so that as the browser page size changes, the gallery will scale according to the flex-grow index. My goal is to ensure all images remain accessible via a scrollbar without usi ...

Ensure that the card header in Bootstrap CSS is consistently displayed at a height of two lines

I am currently developing a webpage using Angular, where bootstrap cards are generated dynamically. The code snippet for this looks like the following: <div class="row"> <div class="card border-primary p-0 mb-2 col-md-4 col-sm-6 colxs-12" *ng ...

The footer that constantly changes

I am looking to dynamically position my footer at the bottom of the page. The footer should be fixed at the bottom. If the page content exceeds the viewport, the footer should automatically adjust its position. html { position: relative; m ...

After migrating the Django website to a new instance, the CSS ceased to function properly

I recently made the switch from hosting my website on Redhat to Amazon Linux on EC2 due to the cost savings. After configuring the Apache server and Django settings, the website seemed to be functioning properly except for the layout of the HTML. The CSS w ...

The href attributes are not being retrieving correctly

I'm currently working on extracting the URLs for each restaurant listed on this page and have developed a Python script for this purpose: import time from selenium import webdriver from selenium.webdriver.common.keys import Keys browser = webdriver ...

Effortlessly retrieving the id attribute from an HTML tag using jQuery

Currently, I am encountering an issue with a code snippet that is designed to extract the value from an HTML tag. While it successfully retrieves a single word like 'desk', it fails when attempting to do so for an ID consisting of two or more wor ...

Incorporating Stripe: Enhancing Online Payments through Redirected Checkout

I am currently in the process of upgrading our checkout system to be SCA compliant. According to the documentation, I must utilize PaymentIntents for this purpose. I have followed the steps outlined in their document found at: https://stripe.com/docs/payme ...

Is there a way to dynamically change the title of a tab when new content is added to a minimized page?

Is anyone familiar with how websites like Facebook and Buzzfeed update their tab titles when there are multiple tabs open? I have noticed that sometimes they add a "(1)" or "(2)" to indicate changes on the page. Do they use JavaScript to achieve this eff ...

Creating a webpage with a left panel and draggable elements using JavaScript/jQuery

As someone new to Javascript/jQuery, I have been given the task of creating a web page with elements in a "pane" on the left side that can be dragged into a "droppable" div area on the right side. The entire right side will act as a droppable zone. I am u ...