Breaking up responsive design

Over the last couple of weeks I have been dealing with the fine art of CSS. Although that is not my daily business anymore – because I lead the website review team here at Yoast – I really enjoyed mastering SCSS and using that for an actual design. During this field trip, I encountered several front end discussions about responsive web design that intrigued me. They actually fascinated me enough to write this post and invite you to share your thoughts about these issues in the comments. We will all benefit by sharing experience, right?

I’m not Will Hunting, I’m not the only one being able to solve this issue, so I will just write down the steps I took in styling a responsive website for all devices. And am looking forward to your thoughts on this.

First tracks

The first thing I wanted to overcome was the issue of relative font sizes. What would be the best practice and why? As rem seems to be the standard these days, that decision was quickly made. First declare pixels for older browsers, than rem for current browsers.

Being old school, I have been scaling down the browser base font (16 pixels in most cases) to 62.5% for a couple of years now. It is just so much easier calculating up and down from 10 pixels, right? Of course that would mean using a font size of 16 pixels for paragraphs would lead to this CSS:

p {
font-size: 16px;
font-size: 1.6rem;
}

Oh the simple things. But this actually made my browser (Chrome for Mac) show a 1.6 * 16 = 25,6 pixel font before showing the 16 pixels I intended it to be. I wouldn’t put my finger on why this was happening, so I just decided to see how Jeremy Keith was doing this. Ever since Reboot 8 (Copenhagen) I have been following this man from a distance, as he seems to be the no-nonsense guy that just does things the right way. His website does not use the 62.5% ‘hack’, but simply calculates rem font sizes from the 16 pixels standard. The simplest things are usually the best. And yes, I did change my entire stylesheet to match that method. Luckily for me I used a separate SCSS file for font sizes, so this really was a ten minute change ;)

Padding and margin

Second stop was padding and margin. I have been going over this a couple of times, trying to decide on what to use for this: px, em or rem. I must have read a dozen articles on the topic. Of course there are always purists that tell you to always use em for this, but my conclusion was that, as for now, it really does not matter. You just want your white space to look right on all screen sizes. I decided to stick with pixels, as it is just the clearest, most easy way to do this.

There is just one exception in my book: padding-bottom for things like paragraphs and list, perhaps even headings. I use em for that. That might be a personal legacy, or something I an used to, but it seems to make sense to relatively calculate the whitespace below relatively sized elements, as a factor of that element (so using em).

Design your mobile website

With all element dimensions set, I took up the almost impossible task to decide where my design was going to change into the mobile design it should be. I think that is actually step one in responsive design: design the mobile version of your website. Don’t just take the website, reduce browser width to 320 pixels and see how you can make that look good.

Seriously, sit down and try to paint the mobile picture for your website. And all elements in it you can think of. Using Genesis and building plugins ourselves, this meant adding a gazillion (OK, perhaps a little less than that) widgets to a sidebar, scratch that, to all sidebars and styling these one by one.

It will make sure you forget as little as possible. Even writing this post made me realize I forgot one or two widgets, so keep in mind that this is of course also an ongoing process.

With the mobile and regular site designed, it is time to decide where to break your design for different devices.

Breaking up is hard to do

There are two ways to go:

  1. Breaking up the design using em’s
  2. Breaking up the design using pixels

Now note that this is not an exact science. As designs differ, these breaking points differ. Again, in my book. Looking forwards to your thoughts and ideas.

Relative breakpoints

The main idea behind using em’s is pixel independency. At the moment, that usually means 1024 pixels is replaced by 1024/16 = 64em. Vasilis van Gemert did an interesting article on Smashing Magazine on these breaking points earlier this year. It also deals with questions like what we should call them (I really don’t care about how we call them, to be honest) and how we could add columns to a layout using responsive design.

Responsive does not always mean showing one TwixRegarding that last one: that is a really nice extra, but I’m dealing with a blog design here. There is no left Twix, there is just the entire two-piece Raider inside the wrapper: main content and sidebar.

I’m under the impression we have been given tools and feel the need to use all. But when hammering one piece of wood to another for a client, you just need a hammer and nails. In most cases, you do not need a power drill. Your client probably has not even thought of doing things this way. Adding extras should not be a billable hour filler without an actual purpose.

In the article, I especially like the sentence: “If the user prefers a bigger font, then the layout would always be optimized in a way that’s relative to the font size.” I like it because I partly agree. And mainly because it is food for thought. It’s a starting point for nice projects like Marko Dugonjić. But would that work in my real world?

I want a design to look nice and readable on all devices. Vasilis also makes a great, valid point about the length of a sentence, that I wanted to take in account. It simply should be an easy read. The eye can’t cope with long lines, in most cases. I don’t agree with the mentioned conclusion “If you start with a small screen and you grow it, every time the width of the main content grows wider than either 75 characters or 10 words, something should happen. Simply said, these are your breakpoints.” I have been thinking about this when styling a full width page and just decided, after a quick word with our designer, not to make that full width actually full width, but just limited the width of an inner <div> within the main content <div>. But this really is your call. This is simply how we decided to do it.

Absolute breakpoints

As there does not seem to be a fixed set for relative breakpoints, I decided to go with absolute breakpoints instead. Using pixels, you can easily determine where you want the design to break. No calculating, just simple measuring and responding to what the design does.

Most designs are not that complex, column wise, so styling them nicely towards a mobile version is not that hard, just make sure to check every variation possible in your page layout.

iOS Resolution Reference by Ben Lew
iOS Resolution Reference by Ben Lew

I decided on three screen widths to start with:

  • 1024 pixels
  • 640 pixels
  • 320 pixels

That’s a regular iPad (landscape), an iPhone 5 and up (landscape) and an iPhone (not retina, portrait). Yes, we like Apple here at Yoast. But we also test on other devices, don’t worry ;) I just needed some starting points, on which I would check what design changes where necessary.

The rest is pretty simple. Set your browser to that width, add a media query like this to your stylesheet:

@media (max-width:1024px) {
div.content {
width:67%;
}
div.sidebar {
width:25%;
}

No, these are not exact numbers. Of course the percentages make sure the <div>s are more flexible. Of course you also play with your browser width when styling your responsive website. It’s just easier like that. But let’s not go into those details. Let’s discuss these breakpoints some more.

Now this might be a personal issue, but I think there is nothing wrong with adding multiple media queries to a stylesheet. If you have read or written an article on how adding multiple media queries affects site speed, I’d love to read that, please drop a link in the comments below.

For me, these three breakpoints work like a charm. I have to make a sidenote though: the design has a max width of 1280 pixels, which spreads the breakpoints nicely over the range desktop to mobile.

I do think that, apart from the three breakpoints I mentioned, you should be specific about what width you are targeting, so using a media query like @media (min-width:855px) AND (max-width:1024px) { ... } is totally alright. There will always be gaps to fill in between these breakpoints, and as mentioned earlier, I want a design to look nice and readable on all devices. Now it does.

In conclusion

So, taking all things in consideration, these are the outcomes of me building a responsive template:

  • use rem for font sizes and calculate these from the browser default, usually 16px,
  • use whatever seems appropriate for padding and margin, which in my case is usually pixels for most block elements and em for whitespace related to text, and
  • use pixels to set breakpoints.

Regarding these breakpoints: you should test that. See what works for your design. That brings me to my final question:

What are your experiences with responsive design? At what width have you set your breakpoints and more important: are these fixed sizes for all designs or do you set them again for each new website?

Please add your experiences in the comments below.

Coming up next!


Discussion (60)

  1. Really very informative article do find percentages do work relatively well if used properly. Thanks..

  • Ever growing number of smart phones and tablet devices needs responsive design elements.
    Thanks for the won derfull article

  • Ever growing number of smart phones and tablet devices needs responsive design elements.
    Thanks for clarifying this aspect in deta

  • great article..more covinient to use n share..great thanks for the share…!!

  • Hi Michiel,
    regarding the performances of the media queries, I just found an interesting answer to a question on Stackoverflow that suggests to download the Chrome Canary or the Webkit Nightly build. These two have included an audit menu that tells you how long each CSS selector takes to run.

  • I have be also struggling with the best thing to use in the places listed above. I find it is quite confusing where you should use px, rem and %. Hopefully some of your hints will make this easier.
    thanks for the share..brilliant post

  • On my website I’m using responsive too… the code is like this
    @media only screen and (min-width:200px) and (max-width:1237px) {
    .container{width:100%;}
    .copyright-wrap{width:100%;}
    .top-nav{width:100%;}
    .slide-caption{width:100%;max-height:12em;position:relative;}
    .slide-wrap img{width:100%}
    }
    for below 869px media screen I recondition the column to 100% so, the column would looks just one row. Making the website to be responsive is not that hard for me.

  • Do these tips have compatibility with HTML5 markups?

  • Responsive design layout is essential part of today’s web standards. Ever growing number of smart phones and tablet devices needs responsive design elements.
    Thanks for clarifying this aspect in detail.

  • I love your article. I have be also struggling with the best thing to use in the places listed above. I find it is quite confusing where you should use px, rem and %. Hopefully some of your hints will make this easier.

    Sightly off topic but I will ask it any way as this article has made me remember the issue. I have designed a website using twenty thirteen and I child theme and was having loads of problems with the break points as there seemed to be quite a few. Is there a way to clear all or some of the break points from the child theme? I know one simple solution is to start with a different theme.

  • I’d love to see a discussion of navigation for responsive themes. I’ve noticed that responsive themes deal with tablets/smartphones by moving the sidebar to the bottom of the page, so you have to scroll down to see it. If your navigation is in the sidebar, then that’s a problem!
    However I can see that if they shrink everything so the sidebar is visible, there’s just too much information to view comfortably.
    Does this mean sidebars are pretty useless these days or am I misunderstanding? Or just using the wrong themes?

  • I have a lot of experience with responsive design/programming.

    You can use universal breakpoints, especially when you want to hide certain elements at different resolutions. For instance…

    /* when it is not for mobile, hide this element */
    @media only screen and (min-width: 650px) {.mobile{display: none;}}
    /* when it is for mobile, hide this element */
    @media only screen and (max-width: 650px) {.hide-mobile{display: none;}}
    /* when it is not for tablet, hide this element */
    @media only screen and (min-width: 900px) {.tablet{display: none;}}
    /* when it is for tablet, hide this element */
    @media only screen and (max-width: 900px) {.hide-tablet{display: none;}}

    However, there may be specific media queries that have to be created based on elements that will behave differently than the standard.

    Also, please note that IE 8 and earlier do not detect media queries so it is best to make a css file that will be only loaded when IE 8 or earlier is detected that has no media queries.

  • isnt there a math formula that makes converting px to em to make responsive design easier?

  • Another long and interesting post. Learn several new things from your post about responsive design. Thanks for the share.

  • isnt there a math calculation for facilitating the ease of converting px to em for responsive designs

  • Responsive layout is must for every website in 2014 and coming years. Google gives weight-age for responsive websites in search and implemented new Ad sizes for AdSense.
    Thanks for these tips.

  • Designing a mobile responsive website is a hard task for me! Let me try it following the tips. Thanks for the article! :)

  • I use a second-hand solution, WP touch plugin for WordPress but the insight I’ve got from Google speed test it said that the links are to small to be selected on a touchscreen device so…
    also this is an important post mostly because that in 2014 mobile advertising will increase with 54% as mashable said.

  • How about Bootstrap? Your opinion?

  • Nice post, but I would like to add something. A lot of the difficult decisions about break points etc. can be made easier by looking at pre-built templates. For instance the 2013 theme had a decent responsive design and can give some insight on where to make adjustments at.

  • Thanks for sharing the useful articles…

  • Hey, Currently all are working on how to optimize your Website that can run smoothly and efficiently on any of the machine. I have also started to take my blog to the next level that has responsive design and many other features.

    Any yeah, thanks for your article too, it’s great man! Keep it up :D

  • I’ve got still so much to learn…

  • Really good article, I have started using rem recently, before I was using percentage for most elements. I do find percentages do work relatively well if used properly.

  • great and usefull article..you always come with great help to newbie…!!!

  • Nice post..thanks
    Really good article, I have started using rem recently, before I was using percentage for most elements. I do find percentages do work relatively well if used properly.

  • Really love this tutorial as a newbie and I hope to improve with time. Thanks

  • Do you know of the code which I can put to zoom in zoom out web sites post and pages in mobile devices ?

  • thanks for sharing your article..it will definelly help newbie…!!!great and usefull article..you always come with great help to newbie…!!!

  • Great article, there is just one thing that’s not correct as far as I know.
    You are right that the retina iPhone has 640 physical pixels in width. But that’s only half of the truth. Since the introduction of retina an other high resolution devices there is a distinction between logical and physical pixels.
    Meaning that the iPhone, no matter if retina or not has 320 pixels in width, and the retina ones have 2 dpp (dots per pixel). Lea Verou has posted a nice website on that issue. I will add it later on.

    I had a discussion with a friend on the issue break points. His opinion was quite simple and made sense to me. He said starting point of mobile is 1px less than iPad width leading to 767px. As all smartphones with higher pixel densities have these logical pixels with physical dots inside, this normally works out fine for me.

  • Nice post Michiel! But I got a suggestion for the relative font-sizes. You can use vw or vw to get a font-size related to the view-port size. Works perfect for me!

    1vw = 1% of viewport width
    1vh = 1% of viewport height
    1vmin = 1vw or 1vh, whichever is smaller
    1vmax = 1vw or 1vh, whichever is larger

  • Hi, this is a great and useful read. Can you pinpoint for me though the Jeremy Keith article you used as reference for rem font sizing as a simple search of his site didn’t yield anything? Thank you!

  • Using:
    box-sizing: border-box;
    could let you spend none time for calculating padding from the with of an element!

  • Really good article, I have started using rem recently, before I was using percentage for most elements. I do find percentages do work relatively well if used properly.

  • I’m basing the breakpoints based on ideal line length for reading in the next version of my wp theme, Swift.
    Ideal line length for reading is 50 to 75 characters.
    For tablets we can accommodate a sidebar (30%wide), break point for it would be approximately 35 to 50em.

    50*0.7em = 80 Chars
    35*0.7em = 55 Chars

    Not all fonts/languages have the same number of character for a give length. Here is a simple tool to find the number of chars..
    http://nerd.vasilis.nl/code/measure-help/

  • Hey Michiel,
    Cool post and an interesting discussion! I would like to add 1 thing though. I do not agree with your statement that “the eye can’t cope with long lines”. This is a misconception that I have tried to explain on my blog: http://www.yourivandijk.nl/en/typography-improves-site-truth-shorter-lines/

  • Thanks Yoast for this interesting information. As a newbie to the blogosphere focussing just on the contents of my blogs your analysis was really depressing for me. I will never reach this level of perfection when creating articles.
    On the other hand I see many articles in magazines and even on websites of bloggers that use a perfect design but the contents weakens slightly.
    Thanks again. Your contribution raises my awareness for a more intense engagement with layouts. However I see a big problem in presenting the same article on a PC, a Tablet, and a Smartphone.

    I’m sorry. I tried to comment on my iPhone but that didn’t work.
    So I moved over to my iPad. Maybe there is a duplicate copy now.

  • Great to see you writing about this Michiel. It’s a hard process to go from not doing anything in CSS to creating a responsive layout.

    First, about font sizes: I like to use Compass’ Vertical Rhythm. This makes sure the em values are used correctly when adding paddings and margins.

    For padding and margin (and widths for breakpoints) you can also use a framework like Neat to do this for you. You can easily control the grids and columns the elements need to adhere to without messing with your CSS that much.

    I also think that for responsive layouts a mobile first approach is much easier that going from big to small. This is because the basics should only be expanded on bigger screens and not scaled down to small screens. This makes is a lot faster to go to different grid formats and column spans.

    Good writeup and hope you’ll share more findings in the future. It’s a constant struggle to create the perfect workflow, but tools like SASS and their frameworks really help us on the way.