Making a full-width browser bar by utilizing negative margins

I've been following a tutorial and need help with my header element. I want it to have a max-width of 800px and be centered in the middle of the page. Inside the header, there will be a #filtersBar div that spans the entire width of the page. Here's what I have so far:

https://i.sstatic.net/veFa1.jpg

Check out my code on this fiddle:

https://jsfiddle.net/us2jsmLy/3/

html, body {
  overflow-x: hidden;
}

header {
  position: relative;
  width: 100%;
  margin: 0 auto;
  max-width: 1200px;
  z-index: 250;
  display: flex;
  flex-direction: row;
  align-items: center;
  flex-wrap: wrap;
  background:red;
  justify-content: space-between;
}

#filtersBar {
  position: relative;
  background: green;
  width: 100%;
  max-width: 800px;
  margin: 0 -9999rem;
  padding: 0.25rem 9999rem;
  z-index: 1;
}

#filtersBarInner {
  position: relative;
  width: 100%;
  max-width: 800px;
  background-color: blue;
  z-index: 2;
}
<header>
  foo bar some contents
  <div id="filtersBar">
    <div id="filtersBarInner">
      dddd
    </div>
  </div>
</header>

I feel like I'm overlooking something simple :/

Answer №1

It seems like you can utilize the :before pseudo element to create a full background edge-to-edge effect while also applying margin:0 to the body.

Check out the updated Fiddle here

html,
body {
  overflow-x: hidden;
  margin: 0;
}

div#filtersBar:before {
  content: "";
  position: absolute;
  background: green;
  top: 0;
  bottom: 0;
  left: -1000px;
  right: -1000px;
  z-index: -1;
}

header {
  position: relative;
  width: 100%;
  margin: 0 auto;
  max-width: 800px;
  z-index: 250;
  display: flex;
  flex-direction: row;
  align-items: center;
  flex-wrap: wrap;
  background: red;
  justify-content: space-between;
}

#filtersBar {
  position: relative;
  background: green;
  width: 100%;
  max-width: 800px;
  z-index: 1;
  padding: 15px 0;
}

#filtersBarInner {
  position: relative;
  width: 100%;
  max-width: 800px;
  /*margin: 0 auto;*/
  background-color: blue;
  z-index: 2;
}
<header>
  foo bar some contents
  <div id="filtersBar">
    <div id="filtersBarInner">
      <p>
        ddddd
      </p>
      <p>
        ddddd
      </p>
      <p>
        ddddd
      </p>
      <p>
        ddddd
      </p>
    </div>
  </div>
</header>

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

"Comparison: Utilizing HTML5 Video Player with IE8 Embed Fallback versus Implementing Fixed

This dilemma has me at my wit's end. In an HTML5 Video element, I've included an mp4, ogg, and an embedded mp4 fallback. However, on the embed fallback in IE8, all my attempts to position the fixed element (#fixed) above it with z-indexing have f ...

Is there a way to modify the header color in LESS? I attempted to do so, but unfortunately, my changes were unsuccessful

I have encountered an issue with my HTML code. Here's a snippet of it: <html> <head> <link rel="less/less.js" href="css/style.less"> </head> <header> This is a header. </header> </html> Within style.less ...

Prevent the display of HTML tags in ASP.NET

Looking for a way to prevent a specific HTML tag from being printed in my ASP.NET project view file, whether through a printer or to a PDF file. Are there any HTML or CSS attributes that can help achieve this? How should they be configured? I have alread ...

Mastering the art of transitioning between DIV elements

I am looking to implement a rotating three-card display on click, and have come up with the following code: $('.box1').click(function(){ $('.box1').toggleClass('removeanimate'); $(this).toggleClass('go'); ...

Is there a more optimized CSS approach for this layout?

I attempted to create this layout using only CSS (no JavaScript, etc.) within an existing HTML page (without altering the HTML code). Unfortunately, I couldn't achieve all the positions that I needed. Let's see if anyone has a better idea. Thanks ...

The process of toggling a div to slide up and down explained

I'm attempting to create a slide toggle effect on a hidden DIV when a user hovers over specific link buttons. JavaScript: $(function () { // DOM ready shorthand var $content = $(".sliderText"); var $contentdiv = $(".sliderCo ...

concealing the upper header while scrolling and shifting the primary header upwards

Is there a way to use CSS to move the main header navigation, including the logo and links, on my website up when scrolling down in order to hide the top black bar header that contains contact information? The website in question is atm.truenorthmediasol ...

Styling Input elements with a unified border in Bootstrap

[Issue Resolved] I have been working on setting a single border between multiple inputs inside a form-group in Bootstrap. Currently, the border is only visible when the input is not focused and it is the last one. However, my expectation is for the bo ...

Adaptable design tailored for smartphones

When it comes to developing mobile websites for various platforms such as iPhone 3 and 4, Android, Blackberry Torch, etc., I usually find myself needing to slice images based on the specific platform. The challenge arises from constantly having to slice im ...

Toggle between pausing and running CSS animations with this handy button!

My awesome graphic animation is in CSS and SVG format. I've been struggling to add a play/pause button to control the animation on click. Here is what I have so far: .animation1 { --animation-delay: 0.1s; animation: ani_keyframes 1 ...

Is there a way to use HTML, JavaScript, and CSS to manipulate an object's opacity when a button is clicked?

I am looking to add a button on my website landing page that can hide the weather section, but I am unsure how to go about it! https://i.sstatic.net/COA82.jpg Currently, I am using a "selector" where I can choose either 0 or 1 from the bottom right corner ...

Incorporate spacing between columns in Bootstrap 5 by adding gutters

I've been exploring ways to incorporate spacing between my columns in Bootstrap 5. I diligently followed the guidelines provided in the documentation, but I'm facing difficulties in adding gutters between my columns. Currently, I am utilizing g- ...

Building a Sleek Hover Effect Dropdown Menu Using HTML and CSS

I've been following a tutorial on YouTube that showcases how to create a hoverable dropdown menu using HTML and CSS. The video link can be found below: Youtube Video Tutorial: https://www.youtube.com/watch?v=9Qrs8p7WgCc After replicating the code an ...

"Troubleshoot: Why isn't my CSS media query functioning

I need help with hiding a div on desktop and showing it only on mobile devices. The code can be found here Specifically, I want the following code snippet to execute only in the mobile version: <div class="mobiupdbox"> <div clas ...

Media query malfunctioning and causing issues

Project link: The media queries provided cannot be altered and must remain as they are. However, an issue has arisen where devices are selecting the styles from smaller resolutions than intended. For example, instead of picking styles from (min-width: 992 ...

Cannot close the Bootstrap dropdown after selecting an option

I am encountering an issue with a dropdown list that has a select feature using Bootstrap 3.4.1. The problem is that the dropdown remains open after selection and does not close unless I click outside of the list. Here is the HTML code: <div id="c ...

Incorporating HTML content in email messages using a PHP mailer script

I have been exploring a way to incorporate HTML code into the email body using an AJAX PHP Form Mailer I discovered on this website: However, whenever I try to add my own HTML coding into the email body, it just displays the tags instead of rendering them ...

How can we add hover effects to Ionic / Angular components when touching or clicking?

For my app, I need to replicate the hover effect from CSS on mobile devices. Users can click and drag elements, and when they drag over certain elements, I want those elements to change z-index and other styles. Is there a way in Angular / Ionic or even ju ...

What is the best way to align several columns at the center in Bootstrap?

I'm struggling to center-align Card 1 so that it perfectly aligns in the middle of the page alongside the other cards (Card 2, Card 3, Card 4, Card 5). Despite numerous attempts, I can't seem to find a solution for this issue. Does anyone know ...

What are alternative methods for creating a two-column form layout that do not involve the use of

When generating the html form, I am looping through the form variables as shown below: {% for field in form %} {{ LABEL }}{{ INPUT FIELD }} The labels and fields are going through a loop. A simple one-column layout can be generated using: {% for field ...