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

Is there a way to adjust the position of the scrolling bar to be closer to the post area?

I was exploring this page (http://noahsdad.com/state-fair-texas-2011/) and noticed a scrolling sidebar to the left of the content area. I'm looking to adjust it so that it is closer to the content area. Any suggestions on how to achieve this? Thank y ...

Verify the level of opacity in the image

I'm working on a website that allows users to upload PNG images and save them. However, before they can save the image, I need to check if it contains transparency. Is there a way to determine if an image is not 24-bit using JavaScript? <img id= ...

Maintain the alignment of content in the center while expanding background images

I am looking to create a website with a split background effect, similar to the design on this site . I want to maintain a content container width of 980px while achieving this look. I attempted to accomplish this by adding height and background color pro ...

Is it necessary for a click handler to be triggered when clicking on a scrollbar?

Check out these HTML snippets: Jsfiddle <style> div { margin:20px; border: 30px red solid; padding: 20px; background-color:green; overflow-y:scroll; } </style> <div onclick="alert('div clicked');"> ...

Implement various actions when the window is resized

The carousel code I have found returns a different number of items to display based on screen size. You can check it out here. $(function() { var mini = 1000; var big = 1280; if (window.screen.availWidth < mini) { $('#carousel1').jsCar ...

Eliminate spaces within a string using JavaScript

In my quest for knowledge about trimming strings in JavaScript, I came across this intriguing discussion with a regex-based solution. After learning from the thread, I was under the assumption that trim would eliminate the space between "Hello" and "World ...

What is the best way to utilize the features of component A within component B when they exist as separate entities

Component A has all the necessary functionalities, and I want to use it in Component B. The code for ComponentA.ts is extensive, but it's not written in a service. How can I utilize the logic from Component A without using a service, considering both ...

What is the best way to utilize $(target) within a directive?

I created a custom directive for selecting time using two blocks. The challenge is detecting the target event on specific blocks within the directive's template. Directive Template: <div class='time-picker-container'> <div clas ...

What is the best way to show a border-left outside of the list marker in Internet Explorer 8?

I have a numbered list that I would like to add a left border to. <ol class="steps"> <li>item 1</li> <li>item 2</li> <li>item 3</li> <ol> .steps {border-left: 5px double #7ab800;} When viewing in Fir ...

both modules loading at the same time in AngularJS

var moduleA=angular.module("MyModuleX",[]); moduleA.controller("MyControllerX",function($scope){ $scope.name = "John X"; }); var moduleB=angular.module("MyModuleY",[]); moduleB.controller("MyControllerY", function($scope) { $scope.name = "Sarah Y" ...

Two challenges I encountered with my CSS dropdown menu bar

I've been working on a CSS top dropdown navigation bar, but I'm encountering two issues that I can't seem to resolve. The first problem is that my nav div either extends too far down or my 1px right border tabs aren't reaching the botto ...

Scrolling down a jQuery div after each new item is added.OR

I have a div acting as a chat feature, where messages are appended based on user actions. I am trying to make it so that the div automatically scrolls down after each message is added, but I'm encountering some issues with this functionality. You can ...

Utilizing the power of JQuery AJAX and PHP to retrieve information from a MySQL database

Currently, I am facing an issue where my PHP code in the api.php file (under Joomla) is not returning the expected data. Below is the snippet of the code: <?php require_once( 'includes/defines.php' ); require_once( 'includes/framework.ph ...

Extract image file name and date information (day, month, year) from an HTML form using Angular

The content of the register.component.ts file can be found below: import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-register', templateUrl: './register.component.html', styleUrls: [&apo ...

Utilize the data-toggle attribute to retrieve a unique variable name, which can then be integrated into a function for added

I'm in the process of replacing an iframe with an image, and I want to accomplish this with minimal code. The container div I'm working with has a data-toggle attribute: <div id="my-div" data-toggle="video2"> <div id="videocontaine ...

Arrange various numbers in a single row to be in line with

I'm utilizing bootstrap in an attempt to design the layout depicted below: Here is a simplified version of my code: .curr { font-size: 12px; font-weight: 700; letter-spacing: .5px; } .price { -webkit-tap-highlight-color: transparent; fo ...

Guide on creating 2 select inputs with distinct elements?

Imagine you have two select inputs called 'Favorite Fruits' and 'Least Favorite Fruits'. Both of them contain a list of 5 fruits. // The following code is an example to demonstrate the issue <select name='favoriteFruits'& ...

Broken link in navigation bar

I encountered an issue with the navigation bar on this website. When I click on "Leistungsspektrum" in the navigation bar, it returns a message stating that The requested URL /leistunsspektrum.html was not found on this server. However, the actual file nam ...

Looking to troubleshoot the selenium error in a loop, specifically the InvalidSelectorException that states: "invalid selector: Unable to find element with the specified xpath"?

I have been attempting to create a for loop in selenium that checks if certain buttons on a website are clickable, clicks on them, and refreshes the page until they are. However, I am encountering an error with my Xpaths as shown below (InvalidSelectorExce ...

Storing User Information in Cookies using jQuery

My current jQuery script is linked to a button and it functions by toggling font styles on a website by enabling or disabling a stylesheet upon clicking the button. Now, I want to enhance this functionality by saving user preference to a cookie so that the ...