![]() |
What You'll Be Creating |
The reason we’re using Flexbox is that it provides very powerful features:
- we can easily make responsive columns
- we can make columns of equal height
- we can push content to the bottom of a container
1. Start with Two Columns
Creating columns in CSS has always been a challenge. For a long time, the only options were to use floats or tables, but they both had their own issues.
Flexbox makes the process easier, giving us:
- cleaner code: we only need a container with display: flex
- no need to clear floats, preventing unexpected layout behavior
- semantic markup
- flexibility: we can resize, stretch, align the columns in a few lines of CSS
- <div class="columns">
- <div class="column main-column">
- 2/3 column
- </div>
- <div class="column">
- 1/3 column
- </div>
- </div>
the columns container
two column children, one with an additional class of main-column which we’ll use to make it wider
- .columns {
- display: flex;
- }
- column {
- flex: 1;
- }
- .main-column {
- flex: 2;
- }
By adding some additional visual styles, here’s what we get:
![]() |
Each of these two columns will contain several articles stacked vertically, so we’re going to turn the column elements into Flexbox containers too. We want:
- the articles to be stacked vertically
- the articles to stretch and fill the available space
- .column {
- display: flex;
- flex-direction: column; /* Makes the articles stacked vertically */
- }
- .article {
- flex: 1; /* Stretches the articles to fill up the remaining space */
- }
The flex-direction: column rule on the container, combined with the flex: 1 rule on the children ensures that the articles will fill up the whole vertical space, keeping our first two columns the same height.
![]() |
3. Make Each Article a Flexbox Container
Now, to give us extra control, let’s turn each article into a Flexbox container too. Each of them will contain:
- a title
- a paragraph
- an information bar with the author and the number of comments
- an optional responsive image
We’re using Flexbox here in order to “push” the information bar to the bottom. As a reminder, this is the article layout we’re aiming for:
![]() |
Here’s the code:
- <a class="article first-article">
- <figure class="article-image">
- <img src="">
- </figure>
- <div class="article-body">
- <h2 class="article-title">
- <!-- title -->
- </h2>
- <p class="article-content">
- <!-- content -->
- </p>
- <footer class="article-info">
- <!-- information -->
- </footer>
- </div>
- </a>
- .article {
- display: flex;
- flex-direction: column;
- flex-basis: auto; /* sets initial element size based on its contents */
- }
- .article-body {
- display: flex;
- flex: 1;
- flex-direction: column;
- }
- .article-content {
- flex: 1; /* This will make the content fill up the remaining space, and thus push the information bar at the bottom */
- }
The article’s elements are laid out vertically thanks to the flex-direction: column; rule.
We apply flex: 1 to the article-content element so that it fills up the empty space, and “pushes” the article-info to the bottom, no matter the height of the columns.
![]() |
4. Add Some Nested Columns
In the left column, what we actually want is another set of columns. So we’re going to replace the second article with the same columns container we’ve already used.
- <div class="columns">
- <div class="column nested-column">
- <a class="article">
- <!-- Article content -->
- </a>
- </div>
- <div class="column">
- <a class="article">
- <!-- Article content -->
- </a>
- <a class="article">
- <!-- Article content -->
- </a>
- <a class="article">
- <!-- Article content -->
- </a>
- </div>
- </div>
As we want the first nested column to be wider, we’re adding a nested-column class with the additional style:
- .nested-column {
- flex: 2;
- }
This will make our new column twice as wide as the other.
![]() |
If you found this post interesting, follow and support us.
Suggest for you:
CSS: Foundation classes on CSS
Coding Made Easy: HTML & CSS For Beginners
Bootstrap 4 Rapid web development framework HTML CSS JS
No comments:
Post a Comment