Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend background-color of header beyond container with css

Tags:

css

I have been searching the web for a while now for an answer to my question. I would like to extend a div background-color beyond the div (and the container div as well) so it reaches the width of the browser. Like so http://vinnusal.is/ The problem with the example above is I'm using a padding/margin fix which creates an annoying scroll to the right. I have tried overflow without any luck.

I know this could be done with a container div that is 100% and nesting divs that are smaller. However I would like to use another way if possible, because this is my first shot at a fluid site with all complications that follow.

Thanks in advance, Helgi

Here is the HTML markup:

<body>
<div class="gridContainer clearfix"> <!-- Container -->

  <div class="gridContainer clearfix header" id="header"> <!--Header begins--> 
<img src="pics/hvitt.png" alt="VFI Logo" name="logo" id="logo">

<!-- Menu Horizontal -->
... irrelevant markup for menu...

  </div>

  <!-- Header ends -->
<div class="gridContainer clearfix submenu" id="submenu"> <!-- Submenu begins -->
<h1><!-- InstanceBeginEditable name="title" -->Articles<!-- InstanceEndEditable --></h1>

And the CSS:

/* Mobile Layout: 480px and below. */

.gridContainer {
    margin-left: auto;
    margin-right: auto;
    width: 88.626%;
    padding-left: 1.1869%;
    padding-right: 1.1869%;
}
#LayoutDiv1 {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#header {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#submenu {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#article {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#footer {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#leftColumn {
    clear: none;
    float: left;
    margin-left: 2.6785%;
    width: 100%;
    display: block;
}
#rightColumn {
    clear: none;
    float: left;
    margin-left: 2.6785%;
    width: 100%;
    display: block;
}
#header2 {
    clear: none;
    float: left;
    margin-left: 2.6785%;
    width: 100%;
    display: block;
}

/* Tablet Layout: 481px to 768px. Inherits styles from: Mobile Layout. */

@media only screen and (min-width: 481px) {
.gridContainer {
    width: 91.4836%;
    padding-left: 0.7581%;
    padding-right: 0.7581%;
}
#LayoutDiv1 {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#header {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#submenu {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#article {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#footer {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#leftColumn {
    clear: none;
    float: left;
    margin-left: 1.6574%;
    width: 100%;
    display: block;
}
#rightColumn {
    clear: none;
    float: left;
    margin-left: 1.6574%;
    width: 100%;
    display: block;
}
#header2 {
    clear: none;
    float: left;
    margin-left: 1.6574%;
    width: 100%;
    display: block;
}
}

/* Desktop Layout: 769px to a max of 1232px.  Inherits styles from: Mobile Layout and Tablet Layout. */

@media only screen and (min-width: 769px) {
.gridContainer {
    width: 78.9565%;
    max-width: 1232px;
    padding-left: 0.5217%;
    padding-right: 0.5217%;
    margin: auto;
}
#LayoutDiv1 {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#header {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#submenu {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#article {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#footer {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display: block;
}
#leftColumn {
    clear: none;
    float: left;
    margin-left: 1.3215%;
    width: 100%;
    display: block;
}
#rightColumn {
    clear: none;
    float: left;
    margin-left: 1.3215%;
    width: 100%;
    display: block;
}
}
like image 739
Helgi Guðmundsson Avatar asked Feb 27 '14 23:02

Helgi Guðmundsson


People also ask

How do I change the background color of my header in CSS?

To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.

How do I make my background color longer in CSS?

All you have to do is add a class to the element and add a couple of lines of CSS! You're basically making a color the whole background and then clipping it based on the element. Boom, you're done! Save this answer.

How do I fill a whole page with color?

Go to Design > Page Color. Choose the color you want. If you don't see the color you want, select More Colors and then pick a color from any of the options in the Color box.

Can you give a div a background color?

We can add background color to a div by simply adding class “bg-primary”, “bg-success”, “bg-danger”, “bg-info”, and many more as shown in the following examples.


1 Answers

Both solutions will cause an overflow.

try this:

.container{
  background-color:#000;
  padding-bottom:30px;
}
.stripe {
  position: relative;
  display: grid;
}
.stripe:before {
  content:"";
  background-color:#000;
  position: absolute;
  top:0;
  right:0;
  bottom:0;
  left:0;
  width: 100vw;
  margin-left: -50%;
  transform: translateX(-50%);
  z-index: -1;
}
like image 118
user1370719 Avatar answered Sep 29 '22 11:09

user1370719