skip to Main Content

I’m trying to follow some advice i’ve seen regarding not using margins for spacing and instead to use a flex container with column direction and the gap property. It’s much nicer, except I can’t work out how to break out of the container when needed for a background color on single flex items.

https://codepen.io/Xe11o/pen/VwVOpPP

.container {
  display: flex;
  flex-direction: column;
  max-width: 1130px;
  margin: 20px auto 0 auto;
  gap: 50px;
}

header {
  display: flex;
  justify-content: space-between;
}

nav {
  display: flex;
}

section {
  font-size: 50px;
}

footer {
  font-size: 25px;
  background: lightgray;
}
<html>
  <body>
    <div class='container'>
      <header>
        <span>title text</span>
        <nav>
          <button>button link 1</button>
          <button>button link 2</button
        </nav>
      </header>
      <section>main content</section>
      <footer>
        Footer content.
        <br />
        How do i get this background color to stretch beyond the flex container to the edge of the screen?
        <br />
        I can remove the max-width+margin from the container and apply them instead to the children (header, section), then maybe do an align-items on the container, but then I can't get the header to look like it does right now.
      </footer>
    </div>
  </body>
</html>

2

Answers


  1. One possible solution for stretching your <footer> container to the edge of the screen is to apply a width: 100vw; to let the <footer> fill the whole screen horizontally and an align-self: center; to position the element accordingly in the center of the screen so you prevent any overflow issues.

    P.S. I adjusted the max-width of .container so one can see that the layout of the header and main content doesn’t change in the snippet.

    .container {
      display: flex;
      flex-direction: column;
      max-width: 500px;
      margin: 20px auto 0 auto;
      gap: 50px;
    }
    
    header {
      display: flex;
      justify-content: space-between;
    }
    
    nav {
      display: flex;
    }
    
    section {
      font-size: 50px;
    }
    
    footer {
      font-size: 25px;
      background: lightgray;
      width: 100vw;
      align-self: center;
    }
    <html>
      <body>
        <div class='container'>
          <header>
            <span>title text</span>
            <nav>
              <button>button link 1</button>
              <button>button link 2</button
            </nav>
          </header>
          <section>main content</section>
          <footer>
            Footer content.
            <br />
            How do i get this background color to stretch beyond the flex container to the edge of the screen?
            <br />
            I can remove the max-width+margin from the container and apply them instead to the children (header, section), then maybe do an align-items on the container, but then I can't get the header to look like it does right now.
          </footer>
        </div>
      </body>
    </html>
    Login or Signup to reply.
  2. The footer is restrained by the max-width of the container. It may be possible to break out of this but I think it would seem more sensible to remove this restriction to begin with.

    In the example below, I’ve moved the max-width so that it applies to just the header and sections, and then the footer can expand to the edge of the page. (I’ve removed the margins on the body just to demonstrate that it goes right to the edge). You mentioned that doing this did not give you the desired header but I think it looks the same as in your example if you set the header width to 100%.

    body {
      margin: 0;
    }
    
    .container {
      display: flex;
      flex-direction: column;
      gap: 50px;
    }
    
    header, section {
      max-width: 1130px;
      margin: 20px auto 0 auto;
      width: 100%;
    }
    
    header {
      display: flex;
      justify-content: space-between;
    }
    
    nav {
      display: flex;
    }
    
    section {
      font-size: 50px;
    }
    
    footer {
      font-size: 25px;
      background: lightgray;
    }
    <html>
      <body>
        <div class='container'>
          <header>
            <span>title text</span>
            <nav>
              <button>button link 1</button>
              <button>button link 2</button
            </nav>
          </header>
          <section>main content</section>
          <footer>
            Footer content.
            <br />
            How do i get this background color to stretch beyond the flex container to the edge of the screen?
            <br />
            I can remove the max-width+margin from the container and apply them instead to the children (header, section), then maybe do an align-items on the container, but then I can't get the header to look like it does right now.
          </footer>
        </div>
      </body>
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search