How to create responsive UI with styled-components
Tags: webdev, css, javascript, react
Published: 2017-09-25 07:01:33

---
title: "How to create responsive UI with styled-components"
published: true
tags: Web Development,CSS,JavaScript,ReactJS
canonical_url: http://www.andreasreiterer.at/web-development/responsive-ui-with-styled-components/
cover_image: http://www.andreasreiterer.at/wp-content/uploads/2017/08/1-kl8Vpj5-QUrzAVbzvSih3Q-825x510.jpeg
---

_There are several approaches on creating responsive web apps with ReactJS. Apart from using whole frameworks like Bootstrap, I wanted to create a responsive grid view with styled-components, which was amazingly painless._

Now that&nbsp;[I built an actual web app](http://www.andreasreiterer.at/web-development/how-to-build-a-simple-hackernews-feed-with-styled-components/)&nbsp;(admittedly, a tiny one) with ReactJS and styled-components, I wondered how easy it would be to create something more responsive. More precisely, I wondered how one would create a responsive page layout like it is easily done with Bootstrap, just to name an example. And that’s what we are going to build along this article, a simple grid view which allows us to split a web site into columns which will be stacked as soon as it is opened from a phone or tablet.

Until this point, whenever I built a responsive web page layout, I always relied on Bootstrap, or react-bootstrap, when working with ReactJS. Why? Because I found it easy to use and it made development a little bit faster.

But not this time. So I had a look on&nbsp;[w3schools tutorial on building a responsive grid view](https://www.w3schools.com/css/css_rwd_grid.asp)&nbsp;to find out what happens behind the curtains of all those fancy libraries:

#### How does a responsive grid view&nbsp;work?

_First of all, I want to give a short introduction how responsive grid views work. If you already know this, please feel free to skip this section._

Usually such grids have twelve columns which have a total width of 100 percent. Depending on the website’s layout you may not need all of them, so it should be possible to merge some columns together to get a bigger one. What really happens in the background is that there is a bunch of&nbsp;`div`&nbsp;tags with equal width&nbsp;`float`ing to the left, so they appear in a row.

```
.column {
  width: 8.33%;
  float: left;
}
```

_EDIT: As stated by&nbsp;_[_Mauro Lionel Ocorso_](https://medium.com/@ocorsomauro)_&nbsp;and&nbsp;_[_Jean Duthon_](https://medium.com/@duthon.jean)_&nbsp;it is also possible to use&nbsp;_ **_flexbox&nbsp;_** _instead of&nbsp;_`float: left;`_. However there might be some compatibility issues if you want to support older browsers also. Thank you guys for pointing this out!_

![](https://cdn-images-1.medium.com/max/1000/1*6Pld5ZuXMOylEXP_1bSPjA.png)<figcaption>A grid with twelveÂ columns</figcaption>

Since the whole grid has a width of 100 percent, each of the columns are exactly 8.33% wide. Now let’s say we want a bigger column at the beginning of the grid, in the size of three columns or 25 percent of the screen.

![](https://cdn-images-1.medium.com/max/1000/1*LHlTLJZuNcrgOGg0ernv5A.png)<figcaption>The first column spans across the first threeÂ columns</figcaption>

In the picture above, you can see the first column spanning across three columns, which caused the second and third column to disappear. What really happened is not a&nbsp;_real_&nbsp;column span. The first column got a width of 25 percent and columns 2 and 3 were deleted now that they are not needed anymore.

To achieve this, some changes have to be made. Instead of having one CSS-class for each column, there is one for each possible column span. This is also reflected in the class names I used: While col-1 spans across one column, col-3 spans across three and col-12 would have a width of 100 percent.

```
[class*="col-"] {
  float: left;
}

.col-1{
 width: 8.33%;
}

.col-2 {...}

.col-3{
 width: 25%;
}

[...]
```

The only thing missing now, is how we get to stack the columns as soon as we view our page from a smaller device. This can be achieved by using media queries (learn more about them&nbsp;[here](https://www.w3schools.com/css/css_rwd_mediaqueries.asp)).

&nbsp;

![](https://cdn-images-1.medium.com/max/1000/1*ONwpRoHIJA7FRnIErifF9A.png)<figcaption>The columns should stack vertically when viewed from a smallÂ device</figcaption>

Let’s say we want to stack the columns as soon as the width is smaller than 768px.

```
[class*="col-"] {
  /*Mobile first: set the width to 100% by default*/
  width: 100%;
}

@media only screen and (min-width: 768px) {
 /* For everything bigger than 768px */
 .col-1{
   width: 8.33%;
 }

 .col-2 {...}

 .col-3{
  width: 25%;
 }
}

[...]
```

First of all, I set the initial width of all columns to 100 percent. Because that is how it should look like on a smaller screen, it also improves the performance of the page display for them. By setting the width to 100 percent, the columns automatically stack vertically, so yep, there is not much more to do.

The second important thing is the&nbsp;`@media`&nbsp;rule I added: As soon as the width exceeds 768px, the overall widths from the last example are used.

Now having covered the basics, let’s see, how this is done with React and styled-components!

#### Creating the basic grid view with styled-components

As a starter, we will build the exact same grid, like I covered in the last section:

- It will have twelve possible columns
- A column can span across the width of 1–12 columns.
- If the total width is smaller than 768px, all of the columns will stack vertically.

In the following screenshot you will see, that I created a component called&nbsp;_Row_&nbsp;which is needed to clear the float after the columns. Then I added a&nbsp;_Column_&nbsp;component, which has by default&nbsp;`float: left`&nbsp;set, and a width of 100% (mobile first, you remember?).

But have a look on your own:

&nbsp;

![](https://cdn-images-1.medium.com/max/1000/1*S_jt8B668fUQBkW4vVOVUA.png)<figcaption>Our grid built with styled-components</figcaption>

The&nbsp;_Column_&nbsp;component takes a prop&nbsp;`span`&nbsp;which represents the amount of columns to span across. If this prop is set, we calculate the desired width in percent. If no column span is set, we default to 8.33 percent, which is the default width for a column span of 1.

#### Extending the grid: Different breakpoints!

Taking Bootstrap as an example, we want to have the possibility to define different column spans for different viewports. This would give us the possibility to use different column spans on different screen sizes.

To achieve this,&nbsp;_Column_&nbsp;will no longer take a&nbsp;`span`&nbsp;prop. Instead there will be one for each breakpoint:

- **_xs_** _&nbsp;_for screen sizes up to 768px
- **_sm_** &nbsp;for sizes up to 992px
- **_md_** _&nbsp;_for sizes up to 1200
- **_lg_** _&nbsp;_for everything bigger than that

&nbsp;

![](https://cdn-images-1.medium.com/max/1000/1*oQ57567nklR574tNd4o2tA.png)<figcaption>Our new Column component</figcaption>

So what did we do here? Since we do not have a fallback width for column spans bigger than&nbsp; **_xs_** _&nbsp;_we no longer calculate the value of the width, but the whole text instead.

For example: If you set a column span of 6 for&nbsp;_md_, you will get&nbsp;`width: 50%;`on medium devices. if you do not set a column span for&nbsp;_md_&nbsp;and the screen width exceeds 992px, we will just not set any width. This way the column gets always the width for the next smaller set column span, or 100% if nothing is set.

Now let’s see how it works:

&nbsp;

![](https://cdn-images-1.medium.com/max/1000/1*JbcS7nAE-ynTqUGlk9y78Q.png)<figcaption>The above example should render a grid with two columns.</figcaption>

**Extra small screens** &nbsp;(\<768px)  
On extra small screens, the first column takes the whole width, while column number 2 is shifted to the next row.

&nbsp;

![](https://cdn-images-1.medium.com/max/1000/1*r6IfYjLc1K8S7hG1NpTfnQ.png)<figcaption>Our example rendered on an extra smallÂ screen</figcaption>

**Small screens** &nbsp;(\>768px)  
The first column should span across 6 columns and should therefore get a width of 50 percent. In this case the second column does not have a column span for small screens set, so the width of the next smallest break point is assigned: 50 percent.

![](https://cdn-images-1.medium.com/max/1000/1*5xjJWyQNYtgeGxWU9kl1Fg.png)<figcaption>Small screenâ€Š–â€ŠIf no column span for a certain screen size is set, the next smaller one is assigned.</figcaption>

<figcaption></figcaption>

**Medium screens** &nbsp;(\>992px)  
This should be easy: The first column spans across 8 columns (66.66 percent) and the second one spans across 4 columns (33.33 percent)

![](https://cdn-images-1.medium.com/max/1000/1*GfDfNujlo0p94k9N58gy5A.png)<figcaption>Medium screen</figcaption>

Since we did not set a column span for large screens, both columns look the same like they look on medium screens.

#### Conclusion

What we built here was an example of a responsive grid with styled-components with basic functionality. You could possibly extend the components to support more styling like margins, paddings or borders, just to name a few.

I think the most advanced responsive grid view I could find in my research for this article, is&nbsp;[grid-styled](http://jxnblk.com/grid-styled/)&nbsp;from&nbsp;[Brent Jackson](https://medium.com/@jxnblk). It even allows you to set different font sizes for four different breakpoints and much more. For this article however, I tried to cover the basics of responsive grids and how to build one. And what I really liked was the fact how easy it was to consider the different breakpoints since we can use real CSS. I know, there are several alternatives out there and I already had a look on some of them. But in terms of using media queries most blog posts recommend using libraries like&nbsp;[react-responsive](https://github.com/contra/react-responsive)&nbsp;or&nbsp;[react-responsive-mixin](https://github.com/akiran/react-responsive-mixin)&nbsp;which may not be bad, but just did not feel right to me.

_The post [How to create responsive UI with styled-components](http://www.andreasreiterer.at/web-development/responsive-ui-with-styled-components/) appeared first on [my website](http://www.andreasreiterer.at)._

# Call To Action
Do you want to learn more about styled-components, responsive design or other web development tips and tricks? Good news: Subscribe to my [weekly newsletter](www.andreasreiterer.at/weekly-webdev-newsletter/) to learn something new once in a week :)