Advertisement
  1. Web Design
  2. UX/UI
  3. Responsive Design

How to Build a News Website Layout with Flexbox

Scroll to top
Final product imageFinal product imageFinal product image
What You'll Be Creating

It’s not necessary to understand every aspect of Flexbox before you can jump in and get started. In this tutorial, we’re going to introduce a few features of Flexbox whilst designing a “news layout” like the one you can find on The Guardian.

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

So let’s get started!

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

Let’s start by making two columns; one that’s 2/3 of the width of our container, and one that’s 1/3.

1
<div class="columns">
2
  <div class="column main-column">
3
    2/3 column
4
  </div>
5
  <div class="column">
6
    1/3 column
7
  </div>
8
</div>

There are two elements here:

  1. the columns container
  2. two column children, one with an additional class of main-column which we’ll use to make it wider
1
.columns {
2
  display: flex;
3
}
4
5
.column {
6
  flex: 1;
7
}
8
9
.main-column {
10
  flex: 2;
11
}

As the main column has a flex value of 2, it will take up twice as much space as the other column.

By adding some additional visual styles, here’s what we get:

2. Make Each Column a Flexbox Container

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
1
.column {
2
  display: flex;
3
  flex-direction: column; /* Makes the articles stacked vertically */
4
}
5
6
.article {
7
  flex: 1; /* Stretches the articles to fill up the remaining space */
8
}

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:

1
<a class="article first-article">
2
  <figure class="article-image">
3
    <img src="">
4
  </figure>
5
  <div class="article-body">
6
    <h2 class="article-title">
7
      <!-- title -->
8
    </h2>
9
    <p class="article-content">
10
      <!-- content -->
11
    </p>
12
    <footer class="article-info">
13
      <!-- information -->
14
    </footer>
15
  </div>
16
</a>
1
.article {
2
  display: flex;
3
  flex-direction: column;
4
  flex-basis: auto; /* sets initial element size based on its contents  */
5
}
6
7
.article-body {
8
  display: flex;
9
  flex: 1;
10
  flex-direction: column;
11
}
12
13
.article-content {
14
  flex: 1; /* This will make the content fill up the remaining space, and thus push the information bar at the bottom */
15
}

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.

1
<div class="columns">
2
  <div class="column nested-column">
3
    <a class="article">
4
      <!-- Article content -->
5
    </a>
6
  </div>
7
8
  <div class="column">
9
    <a class="article">
10
      <!-- Article content -->
11
    </a>
12
    <a class="article">
13
      <!-- Article content -->
14
    </a>
15
    <a class="article">
16
      <!-- Article content -->
17
    </a>
18
  </div>
19
</div>

As we want the first nested column to be wider, we’re adding a nested-column class with the additional style:

1
.nested-column {
2
  flex: 2;
3
}

This will make our new column twice as wide as the other.

5. Give the First Article a Horizontal Layout

The first article is really big. To optimize the use of space, let’s switch its layout to be horizontal.

1
.first-article {
2
  flex-direction: row;
3
}
4
5
.first-article .article-body {
6
  flex: 1;
7
}
8
9
.first-article .article-image {
10
  height: 300px;
11
  order: 2;
12
  padding-top: 0;
13
  width: 400px;
14
}

The order property is very useful here, as it allows us to alter the order of HTML elements without affecting the HTML markup. The article-image actually comes before the article-body in the markup, but it will behave as if it comes after.

6. Make the Layout Responsive

This is all looking just as we want, though it’s a bit squished. Let’s fix that by going responsive.

One great feature of Flexbox is that you need only remove the display: flex rule on the container to disable Flexbox completely, while keeping all the other Flexbox properties (such as align-items or flex) valid.

As a result, you can trigger a “responsive” layout by enabling Flexbox only above a certain breakpoint.

We’re going to remove display: flex from both the .columns and .column selectors, instead wrapping them in a media query:

1
@media screen and (min-width: 800px) {
2
  .columns,
3
  .column {
4
    display: flex;
5
  }
6
}

That’s it! On smaller screens, all the articles will be on top of each other. Above 800px, they will be laid out in two columns.

7. Add Finishing Touches

To make the layout more appealing on larger screens, let’s add some CSS tweaks:

1
@media screen and (min-width: 1000px) {
2
  .first-article {
3
    flex-direction: row;
4
  }
5
6
  .first-article .article-body {
7
    flex: 1;
8
  }
9
10
  .first-article .article-image {
11
    height: 300px;
12
    order: 2;
13
    padding-top: 0;
14
    width: 400px;
15
  }
16
17
  .main-column {
18
    flex: 3;
19
  }
20
21
  .nested-column {
22
    flex: 2;
23
  }
24
}

The first article has its content laid out horizontally, with the text on the left and the image on the right. Also, the main column is now wider (75%) and the nested column too (66%). Here’s the final result!

Conclusion

I hope I’ve shown you that you needn’t understand every aspect of Flexbox to jump in and start using it! This responsive news layout is a really useful pattern; pull it apart, play with it, let us know how you get on!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.