How to animate media queries

Responsive design is everywhere, and whether you’re using a framework or writing media queries yourself, some elements on your page are bound to move or scale.

If your media queries are based on browser dimensions and the browser is resized, then your elements will jump into place. Adding a little animation to those properties is a nice touch for any responsive site.

Today, I’m going to show you how easy it is to add that extra touch, by animating the width and opacity of elements between media queries.


The Layout

For this simple example we’ll use a div, containing 3 smaller divs. The divs will scale according to the size of the browser window. The HTML is simple:

<div class='layout'>
    <div class='box'></div>
    <div class='box'></div>
    <div class='box'></div>

Now, our main CSS will place the three boxes inside the main div and also, in line with a margin to the right:

.layout {
.box {
.box:last-child {

This is our main layout, without media queries, this layout will not adapt if the viewport changes. Now that we’ve got the basics in place, let’s add the media queries.


Adding media queries

Media queries are used frequently nowadays. Most web designers understand them, but in case this is your first time, here’s a quick refresher: media queries check the capability of the device you are using (width, orientation and resolution) and they run specific CSS if the conditions specified for the media query are met. In our example, we’ll use two media queries that will check if the browser is smaller that 960px, and whether it’s smaller than 660px. We’ll then set the width of elements accordingly, we’ll also hide the last child div so that the other two have more space:

@media screen and (max-width:960px) {
    .layout {
        width: 870px;
    .box {
@media screen and (max-width: 660px) {
    .layout {
    .box {
    .box:last-child {

That’s all we need for our media queries. Note that it’s important that this code is placed beneath the original CSS code above so that the media queries overwrite the code above them. If you test your file now you’ll see the size of the divs changing and the opacity of the last child div change when you resize the browser window.

You’ll notice that to hide the last child div we’re setting its opacity to 0 instead of setting it to display:none. That is because we want to be able to animate the property; opacity has many different degrees, whereas display is either true or false (and therefore can’t be animated).


Adding the animation

CSS animations have proved to be really handy when performing these little animations that we used to run in jQuery, such as animating color, width and opacity.

Because we want the page to animate as a whole, we use the * CSS selector and set up our animation. CSS animations degrade gracefully, but you’ll want to add the vendor prefixes too, so that there’s as much support as possible:

    -webkit-transition:all 1s ease;
         -moz-transition:all 1s ease;
               -o-transition:all 1s ease;
                     transition:all 1s ease;

You can see the finished result here.



Adding simple animations like these, are a nice finishing touch to any website. A few lines of code adds a really nice polish to your responsive site.

Have you animated media queries in a project? What effects have you achieved? Let us know in the comments.

Featured image/thumbnail, scale image via Shutterstock.

  • jbasoo

    Nice touch, I did this with an elastic-like cubic bezier timing on one of my projects.

    cubic-bezier(0, 0.59, 0.24, 1.33)

  • Onkar

    Very nice Article….

  • Piper

    As adorable as this is when changing your screen size, I think the way it moves upon first load is terrible. It’s pretty much not usable on a site with more than a few boxes or words.

    • Beto Alanís

      That happens in Chrome, it’s one of many things I hate about that browser. The solution is to add a class via js to the loaded page, so the animations happen only when everything has been loaded ;)

  • daire mccann

    nice tute – not too long, compact. I animated the media queries at several breakpoints with feedback messages, here:

    one thing to note – animating the queries does take its’ toll on the browser, it’s quite intensive on the CPU, so choose wisely!


  • HemanthMalli

    Good information has been shared !! this queries are helpful for implementing and enhancing responsive design!!

  • Martyn Taylor

    Good article, simple instructions. Will be sure to try it out on my next project

  • James Curran

    It is a nice touch but not something I would implement into a site unless it was aimed specifically at web designers. After all, as Paulus pointed out who else is likely to resize the window?

  • Calan

    Yep, superfluous code.

  • joshcartme

    I think people enjoy finding “Easter eggs”. The majority of people will probably never see it, but those who do will think, wow that’s cool.

    I personally do resize websites on occasion not within a design website. Sometimes I want to articles open side by side, or I’m trying to find a window underneath my browser window, etc…

  • Aayush

    Hi, there found you an expert so i need your help. hope you can solve it. i have a problem with the images size. actually i am using a plugin WP Gallery Custom Links. i have uploaded lot of images in a post but every images has a different height and width so they are appearing with different different sizes. i want to set them with the same size which i want to set. please tell me any idea to solve this problem.

  • GodSponge

    When trying to view the example I get “Anonymous Pens Can’t Be Viewed in Full Page View” Don’t really want to create a user and fork the code just to see it.

  • Arif Hussain

    Its awesome especially in case when the breakpoint occurs by changing the device orientation. !

  • html5beginners

    cool , this is what i needed for my website. By simply adding transition in my css , my media query animated, Owrkd in bootstrap also. thanks.