Why should I set the width of the header to be larger than the body in order to fully cover it?

I've set my header width to 106% to span the entire page length, while the max-width for my body is kept at 95%.

body {
  background-color: #333;
  max-width: 95%;
  font-family: ivyjournal, sans-serif;
  margin: 0;
  padding: 0;
  overflow-x: hidden;
}

header {
  width: 106%;
  position: relative;
  font-family: ivyjournal, sans-serif;
  background-color: black;
  margin: 0 auto;
}

h1 {
  color: white;
  position: relative;
  font-size: 400%;
  font-decoration: none;
  font-style: italic;
  font-weight: lighter;
  left: 50px;
  top: 10px;
  padding: 0;
  margin: 0 auto;
}
<body>
  <header>
    <div class="text-logo">
      <h1>E.D.D</h1>
    </div>
    <div id="nav">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Services</a>
          <li>
            <li class="Dropdown">More</li>
      </ul>
    </div>
  </header>

  </html>

I've been experimenting with this setup for some time now, but I haven't come across any solutions that address my specific issue. Any insights would be greatly appreciated. Thanks!

Answer №1

Using % to set width refers to the width of its container, not necessarily the entire page. For more information, you can visit this link.

Have you experimented with using rem (root-relative) or vw (viewport-relative width) instead of relying solely on %? Just a thought.

Answer №2

When working with CSS, using percentage units will result in the size of the element being relative to its parent element.

Consider your outermost element, the body, which has a max-width set to 95%. This means it will occupy 95% of the width of the browser window.
Now, imagine the header element within the body. If its width is defined as 106%, then it will actually be 106% of 95% of the browser window's width. 106% * 95% = 100.7 %. Ultimately achieving the desired outcome of occupying 100% of the browser width through this indirect method.

Instead of relying on parent elements for sizing, it's more efficient to base them on the browser viewport itself using the vw (view width) and vh (view height) units.

Here's an example:

header {
  width: 88vw;    /* Setting the header width to 88% of the browser width. */
}

Answer №3

Welcome to the world of coding! It seems like you may be having some issues with your HTML structure. Take a look at my code below for guidance on how to set up your layout correctly. Notice how the width is set to 100%.

 <!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <link rel="stylesheet" href="../styles/style.css">
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
 <title>Title</title>
</head>    
<body>
 <header>
  <div id="nav">
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Services</a>
      <li class="Dropdown"><a href="#">More</a></li>
    </ul>
   </div> 
  </header>
  <div class="text-logo">
    <h1>E.D.D</h1>
  </div>    
</body>
</html>

CSS:

            h1 {
                color: white;
                position: relative;
                font-size: 400%;
                text-decoration: none;
                font-style: italic;
                font-weight: lighter;
                left: 50px;
                top: 10px;
                padding: 0;
                margin: 0 auto;
            }

            body {
                background-color: #333;
                max-width: 100%;
                font-family: ivyjournal, sans-serif;
                padding: 0;
                overflow-x: hidden;
            }

            header {
                width: 100%;
                height: 40%;
                position: relative;
                font-family: ivyjournal, sans-serif;
                background-color: black;
                margin: 0 auto;
            }

If you're interested in learning more about creating dropdown menus and other web elements, I recommend checking out Bootstrap's documentation here: https://getbootstrap.com/docs/4.1/getting-started/introduction/

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

Deciding the source URLs for external iframes

Is it possible to detect the URL displayed in an iframe? How can we ensure that links within an iframe open in a new tab/window? Moreover, is there a way to achieve this even with the same-origin policy for external frames? If not, what causes this violat ...

retrieve the month and year data from the input date

I encountered a situation where I'm working with the following unique HTML code: <input type="date" id="myDate"/> <button type="button" class="btn btn-secondary" id="clickMe">MyButton</ ...

Tips for aligning website content (including the body and navigation bar) at the center in rtd sphinx

Trying to create documentation with the Read the Docs theme for Sphinx documentation. Want to center the entire webpage, including both the body and left navigation bar so that as the window width increases, the margins on both sides increase equally. Righ ...

Please provide the text enclosed within the h1 tags

Is there a way to extract the content between <h2> </h2> tags in PHP or jQuery from each page and use it as the title of the pages to ensure uniqueness? Example: <section class="sub_header"> <h2>Contact</h2> < ...

Issue with the height of Bootstrap 4 navigation bar

After deciding to use Bootstrap 4 for my current project, I encountered an issue with the navbar. It seems to expand too much, which is only happening once I deploy the website. Can anyone help me figure out what's causing this problem? Below is the c ...

deactivating image mapping on mobile media screens

I'm looking to disable my image map on mobile screens using media queries. I've attempted to include some javascript in the head of my html file, but I encountered an error: <script src="http://code.jquery.com/jquery-1.11.3.min.js"></s ...

Load additional content seamlessly using Ajax in Django without needing to reload the entire navigation bar

I recently started working with Django as I develop my own website. I have created a base.html file which includes the main body, CSS, js, fonts, and navbar that will be present on all pages of my site. The navbar is located in another HTML file called nav ...

Unable to hear sound - JavaScript glitch

Spent countless hours trying to figure this out - Can't get the audio file to play when clicking an HTML element. var sound = document.querySelector(".soundfile"); function playAudio() { sound.play(); } document.querySelector(".btn-hold").addE ...

Trouble Aligning Bootstrap Dropdown Menu Link with Other Navbar Links

Issue with dropdown links not aligning properly in the Bootstrap navbar. The positioning seems off and can be seen in the screenshot provided where the blue outline represents the ".dropdown" https://i.stack.imgur.com/QmG7v.png Facing a similar problem a ...

In Chrome, a horizontally floating item is pushed down, whereas in Firefox it remains in place

What is causing the last item in the submenu to be lower than the rest in Chrome, but aligned in Firefox? Check out this JSFIDDLE with font turned red for visibility View the ACTUAL SITE here I've experimented with padding, margin, vertical align, ...

What is the easiest way to display index.html using Django?

Hi there, After setting up Django 1.9 on my Ubuntu machine, I am looking to deploy a lightweight Django app. Here is what I have done so far: cd ~ django-admin startproject dj10 cd ~/dj10/dj10/ mkdir templates echo hello > templates/index.html My qu ...

Comparing inline-block and block display options for displaying images

In this HTML snippet, there is a main div with the id of "main_div" that contains two sub-divs. The first sub-div has an id of "logo" and includes a link to a picture. The second sub-div has an id of "intro" and contains text. For reference, here is the o ...

I'm having trouble getting my if statement to function properly with a random number

I'm currently working on creating a natural disaster sequence for my game, and I'm using Math.random to simulate different outbreak scenarios. However, I've encountered an issue at the end of my code where an if statement is supposed to trig ...

Stylish CSS: Jagged 3-dimensional rotated text

Have a look at this Codepen to see the issue. As the text moves into the background, more aliasing appears. A screenshot taken in Chrome on macOS (similar appearance in FF & Safari): https://i.sstatic.net/n746I.png I've experimented with different ...

What is the method for stretching the navbar and content to fill the entire viewport in Bootstrap?

Can anyone help me with an issue regarding a navigation bar and content created in Bootstrap 5? I'm trying to use vh-100 to make both the navigation bar and content stay on the view page without a scrollbar. However, the size of the navigation bar is ...

Ways to extract information from JSON files

Currently, I am working on a script to extract viewer count and follower count data from Twitch. While I have successfully retrieved the viewer count information, I am encountering issues with extracting the follower count. The essential information can be ...

Click on the hyperlink to adjust the background size of an inline list item

I created a navigation bar using inline display for the li elements. I want my last column to have a defined height background. However, it is only displaying a background behind the name of the column. Here is the relevant section from style.css: #navi ...

Chrome Canary's behavior with CSS border-radius on tiny objects

Is it possible to achieve CSS border-radius on really small objects in Chrome Canary? This experiment with a 3px high line and a 2px border radius, when zoomed in at 600%, doesn't show much difference: It works perfectly fine in regular Google Chrom ...

Discover the Magic of Jquery Hover Effect Unleashed

$('.outerBox').hover( function() { $(this) > $('.innerBox').stop(); $(this) > $('.innerBox').slideDown(750); }, function() { $(this) > $('.innerBox').stop(); $(this) > $(&apos ...

The video is not displaying on my website

I recently added a video to my webpage that is 3:48 minutes long. However, even though I have the navigation bar and sound in place, the video does not seem to be displaying properly. Here is an image of how it appears: https://i.stack.imgur.com/HeN41.png ...