Search code examples
cssflexbox

Is it possible to use flexbox on the body to space everything vertcally on a page?


Im trying to space out three divs on my page to fill up the entire screenwidth using flexbox. When i apply display: flex and flex-direction: column to the body, nothing changes. all of the flex items stay bunched up. adding justify-content: space-between doesn't solve anything either.

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
body {
  height: 100vh;
  margin: 0;
  overflow: hidden;
  font-family: Roboto, sans-serif;
}

img {
  width: 600px;
}

button {
  font-family: Roboto, sans-serif;
  border: none;
  border-radius: 8px;
  background: #eee;
}

input {
  border: 1px solid #ddd;
  border-radius: 16px;
  padding: 8px 24px;
  width: 400px;
  margin-bottom: 16px;
}

li {
  list-style-type: none;
}

a {
  color: inherit;
  text-decoration: none;
}

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

.left-links,
.right-links {
  padding-left: 8px;
  padding-right: 8px;
  display: flex;
  gap: 5px;
}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.footer {
  display: flex;
  justify-content: space-between;
  background-color: #eeeeee;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  justify-content: space-between;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>LAYOUT</title>
  <link rel="stylesheet" href="./style.css">
</head>

<body>
  <div class="page-container">
    <div class="header">
      <ul class="left-links">
        <li><a href="#">About</a></li>
        <li><a href="#">Store</a></li>
      </ul>
      <ul class="right-links">
        <li><a href="#">Profile</a></li>
        <li><a href="#">Settings</a></li>
      </ul>
    </div>
    <div class="content">
      <img src="./logo.png" alt="Project logo. Represents odin with the project name.">
      <div class="input">
        <input type="text">
      </div>
      <div class="buttons">
        <button>Do the thing!</button>
        <button>Do the other thing!</button>
      </div>
    </div>
    <div class="footer">
      <ul class="left-links">
        <li><a href="#">Advertising</a></li>
        <li><a href="#">Business</a></li>
      </ul>
      <ul class="right-links">
        <li><a href="#">Privacy</a></li>
        <li><a href="#">Terms</a></li>
      </ul>
    </div>
  </div>
</body>

</html>


Solution

  • In your case, the body only has one direct child so there is nothing to space out.

    Generally, it's not a good idea to apply flexbox to the body. Since you have an overall container, use flexbox on that.

    @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
    body {
      height: 100vh;
      margin: 0;
      overflow: hidden;
      font-family: Roboto, sans-serif;
    }
    
    .page-container {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      height: 100%;
    }
    
    img {
      width: 600px;
    }
    
    button {
      font-family: Roboto, sans-serif;
      border: none;
      border-radius: 8px;
      background: #eee;
    }
    
    input {
      border: 1px solid #ddd;
      border-radius: 16px;
      padding: 8px 24px;
      width: 400px;
      margin-bottom: 16px;
    }
    
    li {
      list-style-type: none;
    }
    
    a {
      color: inherit;
      text-decoration: none;
    }
    
    .header {
      display: flex;
      justify-content: space-between;
    }
    
    .left-links,
    .right-links {
      padding-left: 8px;
      padding-right: 8px;
      display: flex;
      gap: 5px;
    }
    
    .content {
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    
    .footer {
      display: flex;
      justify-content: space-between;
      background-color: #eeeeee;
    }
    
    body {
      display: flex;
      flex-direction: column;
      height: 100vh;
      justify-content: space-between;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>LAYOUT</title>
      <link rel="stylesheet" href="./style.css">
    </head>
    
    <body>
      <div class="page-container">
        <div class="header">
          <ul class="left-links">
            <li><a href="#">About</a></li>
            <li><a href="#">Store</a></li>
          </ul>
          <ul class="right-links">
            <li><a href="#">Profile</a></li>
            <li><a href="#">Settings</a></li>
          </ul>
        </div>
        <div class="content">
          <img src="./logo.png" alt="Project logo. Represents odin with the project name.">
          <div class="input">
            <input type="text">
          </div>
          <div class="buttons">
            <button>Do the thing!</button>
            <button>Do the other thing!</button>
          </div>
        </div>
        <div class="footer">
          <ul class="left-links">
            <li><a href="#">Advertising</a></li>
            <li><a href="#">Business</a></li>
          </ul>
          <ul class="right-links">
            <li><a href="#">Privacy</a></li>
            <li><a href="#">Terms</a></li>
          </ul>
        </div>
      </div>
    </body>
    
    </html>