What could be causing the header's height to remain unchanged when using a percentage for its size?

I'm having trouble creating a header that takes up 20% to 40% of the body. When I try using percentages, it doesn't work but it works fine with pixels. Can someone explain why percentages aren't working for this?

*{
  margin:0;
  padding:;
  box-sizing:border-box;
}
html{
  padding:3em;
}

header{
  width:100%;
  background:#e74c3c;
  height:30%;

}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=">
  <title></title>
</head>
<body>
  <header>
    <h1> JavaScript </h1>
  </header>
  <nav>
    <ul>
      <li>Home</li>
    <li>Contact</li>
    <li>Gallery</li>
    <li>About Us</li>
   </ul>
  </nav>
  <div class="content">
    <h1>Learn JavaScript</h1>
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Illo laboriosam, explicabo commodi tempora maiores facere qui <BR></BR>ratione nostrum unde rem placeat, distinctio similique quod quos ullam autem molestiae? Perferendis vero hic temporibus quaerat, aut tempore ipsam expedita fuga ea cupiditate eius sit <BR></BR> unde, dolorem earum enim quos, omnis voluptates deserunt commodi rem veritatis? Quam at ipsum excepturi deserunt quae! Debitis ab fugit error dolore! Maiores sunt hic suscipit? Nihil, quis.
  </div>
  <aside>
    <ul>
      <li>Home</li>
      <li>Contact</li>
      <li>Gallery</li>
      <li>About</li>
    </ul>
  </aside>
  <footer>
      <p class="end-text">Shayan Kanwal @ Copyright 2020</p>
  </footer>
</body>
</html>

Answer №1

Update: Following MaxiGui's advice, it is not recommended to assign a fixed height to your html or body elements. The example has been revised to use a unit for the height, which is more appropriate in this scenario.

Response to your Inquiry: In the current version of CSS, the % value does not function as expected when applied to the height attribute. It only works if the parent element has a fixed height. Therefore, it should only be used within other elements and NOT with the body or html tags, as it will inevitably disrupt your layout at some point. The height of a block-element, for instance, pertains to the height of its contents. For further clarification on why and how this occurs, you can refer to THIS post.

*{
  margin:0;
  padding:;
  box-sizing:border-box;
}
html{
  padding:3em;
}

header{
  width:100%;
  background:#e74c3c;
  /*vh is a unit that uses the current height that is available on the viewport, it basically is:
relative to 1% of the height of the viewport, so 30vh = 30% of the viewports available height*/
  height:30vh;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=">
  <title></title>
</head>
<body>
  <header>
    <h1> Javascript </h1>
  </header>
  <nav>
    <ul>
      <li>Home</li>
      <li>contact </li>
      <li>gallery</li>
      <li>about us</li>
   </ul>
  </nav>
  <div class="content">
    <h1>learn javascript</h1>
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Illo laboriosam, explicabo commodi tempora maiores facere qui <BR></BR>ratione nostrum unde rem placeat, distinctio similique quod quos ullam autem molestiae? Perferendis vero hic temporibus quaerat, aut tempore ipsam expedita fuga ea cupiditate eius sit <BR></BR> unde, dolorem earum enim quos, omnis voluptates deserunt commodi rem veritatis? Quam at ipsum excepturi deserunt quae! Debitis ab fugit error dolore! Maiores sunt hic suscipit? Nihil, quis.
  </div>
  <aside>
    <ul>
      <li>home </li>
      <li>contact</li>
      <li>galley</li>
      <li>about</li>
    </ul>
  </aside>
  <footer>
      <p class=end-text>shayan kanwal@copyright 2020</p>
  </footer>
</body>
</html>

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

Is it necessary to use the strict doctype if our code is already valid with the transitional doctype?

Are there any drawbacks to using a transitional doctype with presentational or deprecated elements, even if our code is valid in W3C validation with a transitional doctype? Will we need to rewrite or edit the code of websites using XHTML/HTML transitional ...

What are some effective ways of using the parent, before, and children selectors?

<table> <tr><td><input type="text" value="123"></td><td><input class="here" type="text"></td></tr> <tr><td><input type="text" value="333"></td><td><input class=" ...

Image displayed in tooltip when hovering over

I have been experimenting with displaying an image in a tooltip when hovering over it. I came across a fantastic solution that suits my needs: Codepen CSS: @import "compass"; * { box-sizing: border-box; } .tooltip { position: relative; border-bo ...

Black textures with images sourced from the same domain - Cross-origin

Despite trying numerous solutions and tips to resolve the cross-origin issue, I still can't seem to fix it. There are no errors, the images are hosted on the same domain as the webgl test, but the textures appear black. Occasionally when I refresh rep ...

Various JSON Tag values occurring more than once

Upon receiving a JSON Object in HTML, I am passing it to a JavaScript function defined below. The issue arises when the same folderName appears repeatedly on the HTML page. JSON Object: { "folderName": "arjunram-test", "objects": [ { ...

encountering difficulty incorporating menu.php within main.php

I'm currently working on integrating the menu.php file into my main.php. Here is what I have accomplished so far: Main.php <html> <body> <?php include("menu.php"); ?> <p>This is an example to demonstrate how ...

What is the best way to display the panel during a postback?

I have a group with two radio buttons labeled purchase and expenses. When the purchase radio button is clicked, the panelpurchase will be displayed, and similarly, the panelexpense will show for the expenses radio button. Check out the image of the output ...

Launching a URL in a pop-up window with customized title and address bar

I am seeking to implement a feature similar to the following: <a href="post/23">post 23</a> When a user clicks on this element, I want a popup div to fade in and load the HTML from page post/23 into it. Additionally, I would like the title an ...

Update the text on the form submit button after it has been submitted

Is there a way to change the text on a submit button after it has been clicked? I have a form with a button and I want to switch the text from "click" to "Next" once the form has been submitted. <form> <div class="form-grou ...

Achieving a consistent fixed width for tbody even when scrollbar is present

thead{ width: calc(100% - 17px); display:block; } tbody{ display:block; overflow-y: auto; height:100px; } http://jsfiddle.net/mkgBx/ Is there a way to adjust the width of the tbody element to match that of the thead element plus the scrollbar? Currently ...

Disable the shadow on the focused state of Safari and Chrome dropdown selects

I'm attempting to eliminate the shadow that appears on a select element when it is in focus: https://i.sstatic.net/5kDKE.png I have tried the following CSS styles: select { border: none; box-shadow: none; -webkit-box-shadow: none; o ...

Struggling to find your way around the header on my website? Let me give

I'm looking to display certain links prominently at the top of a page, similar to this example: "home > page1 > link1 > article 1." Can someone advise on how to achieve this using HTML, PHP, or jQuery? ...

Display data when clicking on Tailwind

I am currently displaying a sub menu on hover using Tailwind CSS. However, I am wondering how I can achieve the exact same functionality by triggering an onclick event instead of hovering over the menu. Here is a DEMO showcasing the current setup. CODE: ...

Is there a way to automatically update the child option when the parent option is altered?

I am attempting to modify the child option based on the parent option selection, similar to how it works in Bootstrap. ` <div class="wrap-input100 input100-select bg1"> <span class="label-input100">Indus ...

Tips for avoiding re-rendering during onclick event handling

let content=useRef(); let toHide=useRef(); useEffect(()=>{ content.current=document.querySelectorAll('.content'); toHide.current=document.querySelectorAll('.hide'); toHide.current?.[0].addEventListener("cli ...

Ways to retrieve the image URL from a div element?

How can I use PHP to extract the background image URL from a div? Specifically, I need to search for a specific class within the string and then extract the background-image URL. For instance: <div class="single-post-image" style="backgr ...

Filter Products with Magento Layered Navigation - Checkbox Options

Is there a method to transform the layered navigation into checkboxes similar to Asos? I have noticed that there are modules available for purchase claiming to offer this functionality. I may consider using one of those, but I wanted to explore any other ...

How to handle Component binding change events in AngularJS

I have developed a component in AngularJS that displays data. However, when the customer binding changes, I need to call a service in the component controller, but it is not updating. Below is the code snippet: In my Test1.html file: <tab-customer tit ...

The cascading menu causes interference with the function of other elements in the

I am currently designing a navigation bar that includes a drop-down menu (with plans to add another one in the future). The issue I'm facing is that when the user clicks on the drop-down menu, it shifts all of the navigation links to the right. What I ...

The text input field is unresponsive and unable to be selected in the mobile layout

I have implemented this <textarea> component within my web application <div class="col-xs-11 lessSpaceBetweenLine"> <textarea class="form-control actionitems" id="actionitems" name="actionitems" rows="2" pl ...