Search code examples
htmlcsswidthpercentage

how could one use percentage for the header when the width for the body hasn't been set


This is the html:

<body>
  <!-- Forked from a template on Tutorialzine: https://tutorialzine.com/2016/06/freebie-landing-page-template-with-flexbox -->
  <header>
    <h2><a href="#">Mountain Travel</a></h2>
    <nav>
      <li class="new"><a href="#">Tours</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact</a></li>
    </nav>
  </header>

  <section class="hero">
    <div class="go_crazy"></div>
    <div class="background-image" style="background-image: url(assets/img/main.jpg);" >
    </div>
    <div class="hero-content-area">
      <h1>Mountain Travel</h1>
      <h3>Unmissable Adventure Tours Around The World</h3>
      <span class="old"><a href="#" class="btn">Contact Us Now</a></span>
    </div>
  </section>

</body>

This is the CSS:

/*Header Styles*/

header {
    position: absolute;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 35px 100px 0;
    animation: 1s fadein 0.5s forwards;
    opacity: 0;
    color: #fff;
}

Inside the CSS file, there is no set for the width of the body, but why the developer could use 100% for the header?? I think it means 100% of the width of the parent. So could anyone explain this for me??


Solution

  • The browser automatically sets the width of the <body> element equal to the width of the browser window.

    Example: If your browser window is 800px wide, you can imagine the browser inserts the following CSS:

    body {
      width: 800px;
    }
    

    You can't see that CSS in your own code, but it's there, hidden inside the browser. So you are correct in thinking that setting the <header> to 100% width means it should be 100% of the width of the parent, and this is why that works in this case.