Home > Web Front-end > CSS Tutorial > How Can I Make a Flexbox Element Fill Available Vertical Space?

How Can I Make a Flexbox Element Fill Available Vertical Space?

Linda Hamilton
Release: 2024-12-02 18:05:17
Original
562 people have browsed it

How Can I Make a Flexbox Element Fill Available Vertical Space?

Flexbox: Fill Available Vertical Space

In a horizontal flexbox row, using the "flex" property on an element enables it to grow and fill the available horizontal space. What if you want to achieve a similar effect vertically?

The challenge lies in ensuring that a vertical element expands to fill the remaining space, allowing other elements to stack vertically. To address this, you can employ the following flexbox properties:

  • flex-direction: column on the flex container aligns elements vertically instead of horizontally.
  • flex: 1 on the element that should fill the remaining space allows it to grow and occupy the remaining height.

Consider the following example:

body {
  margin: 0;
}
*{
  box-sizing: border-box;
}
.row {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.flex {
  flex: 1;
}
.row, .row > * {
  border: 1px solid;
}
Copy after login
<div class="row">
  <div>some content</div>
  <div class="flex">This fills the available space</div>
  <div>another content</div>
</div>
Copy after login

In this setup, the .row container has a fixed height of the browser viewport using height: 100vh. The .flex element has flex: 1, causing it to expand and fill the remaining vertical space. As a result, the two other elements stack vertically within the .row container.

The above is the detailed content of How Can I Make a Flexbox Element Fill Available Vertical Space?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template