0

It's trivial to obtain a centered, max-width website with a single background color by styling up the body element like so:

body {
  max-width: 500px;
  margin: auto;
  background-color: black;
}

header {
  height: 100px; /* Might not be static */
  background-color: red;
}

#main {
  height: 300px; /* Might not be static */
  background-color: yellow;
}

footer {
  height: 200px; /* Might not be static */
  background-color: blue;
}
<header>
</header>
<section id="main">
</section>
<footer>
</footer>

However, if you want the background color for each section of the page to extend infinitely on either side, things can quickly get non-semantic:

header {
  height: 100px; /* Might not be static */
  background-color: red;
}

#main {
  height: 300px; /* Might not be static */
  background-color: yellow;
}

footer {
  height: 200px; /* Might not be static */
  background-color: blue;
}

.container {
  box-sizing: border-box;
  margin: auto;
  max-width: 500px;
  height: 100%;
  border: 2px black dotted;
}
<header>
  <div class="container">
  </div>
</header>
<section id="main">
  <div class="container">
  </div>
</section>
<footer>
  <div class="container">
  </div>
</footer>

Is there a good way to achieve the effect of the second example without adding a container div to every top-level block? Even a CSS trick that generates such containers dynamically would be preferable.

Brian Bauman
  • 668
  • 5
  • 22
  • Can you post the final layout you want to get, e.g. are the background bars meant to be expanded outside of the 500px centered containers? – Stickers Jan 24 '18 at 20:19
  • The second snippet is an approximation of the final layout. The question states that the goal is to have a maximum width for page content, with each top-level block's background extending infinitely to either side - without using containers. – Brian Bauman Jan 24 '18 at 20:27
  • 1
    Are the rows fixed height like in your examples? – Stickers Jan 24 '18 at 20:29
  • Not necessarily. The header is fixed, but the main section and footer are dynamic. – Brian Bauman Jan 24 '18 at 20:30
  • I think what you did in the 2nd example is already a nice solution with a good balance of html/css to meet your goals. – Stickers Jan 24 '18 at 20:35
  • 2
    @Constantin,
    has been deprecated for 20 years. https://www.w3.org/TR/html4/present/graphics.html#edef-CENTER
    – Brian Bauman Jan 24 '18 at 20:37
  • 1
    and i am still using it ;) –  Jan 24 '18 at 20:39
  • yep

    yep

    yep

    –  Jan 24 '18 at 21:37
  • @Brian Bauman lol i forgot backgrounds !

    yep

    yep

    yep

    –  Jan 24 '18 at 21:40

1 Answers1

1

header {
  height: 100px;
  /* Might not be static */
  background-color: red;
  position: relative;
}

#main {
  height: 300px;
  /* Might not be static */
  background-color: yellow;
  position: relative;
}

footer {
  height: 200px;
  /* Might not be static */
  background-color: blue;
  position: relative;
}

footer,
header,
#main {
  box-sizing: border-box;
  margin: auto;
  max-width: 500px;
  border: 2px black dotted;
}

header:before,
#main:before,
footer:before {
  content: '';
  width: 100vw;
  transform: translate(-50%);
  height: 100%;
  left: 50%;
  position: absolute;
  z-index: -1;
}

header:before {
  background-color: red;
}

#main:before {
  background-color: yellow;
}

footer:before {
  background-color: blue;
}
<header>
  My header so nice;
</header>
<section id="main">
  my content so nice
</section>
<footer>
  my footer very low
</footer>
Lars
  • 3,022
  • 1
  • 16
  • 28
  • Very clever! This can be improved further by adding `background-color: inherit;` to the main `:before` selector, allowing the individual `:before` rules to be removed. It's a pity the borders create gaps in the background, but my use case doesn't actually require borders. – Brian Bauman Jan 24 '18 at 22:09