How to animate media queries

Default avatar.
June 24, 2013
How to animate media queries.

thumbnailResponsive 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>
</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 {
width:960px;
height:600px;
background:#b34d6f;
margin:auto;
}
.box {
width:300px;
height:200px;
margin-right:25px;
background:#4d77b3;
display:inline-block;
margin-top:50px;
}
.box:last-child {
margin-right:0px;
}

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 {
width:270px;
}
}
@media screen and (max-width: 660px) {
.layout {
width:570px;
}
.box {
width:170px;
}
.box:last-child {
opacity:0;
}
}

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.

Conclusion

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.

Sara Vieira

Sara Vieira is a freelance Web Designer and Developer with a passion for HTML5/CSS3 and jQuery. You can follow her on twitter or check out her website.

Read Next

3 Essential Design Trends, November 2024

Touchable texture, distinct grids, and two-column designs are some of the most trending website design elements of…

20 Best New Websites, October 2024

Something we’re seeing more and more of is the ‘customizable’ site. Most often, this means a button to swap between…

Exciting New Tools for Designers, October 2024

We’ve got goodies for designers, developers, SEO-ers, content managers, and those of you who wear multiple hats. And,…

15 Best New Fonts, September 2024

Welcome to our roundup of the best new fonts we’ve found on the web in the previous four weeks. In this month’s edition…

3 Essential Design Trends, October 2024

This article is brought to you by Constantino, a renowned company offering premium and affordable website design You…

A Beginner’s Guide to Using BlueSky for Business Success

In today’s fast-paced digital world, businesses are always on the lookout for new ways to connect with their audience.…

The Importance of Title Tags: Tips and Tricks to Optimize for SEO

When it comes to on-page SEO, there’s one element that plays a pivotal role in both search engine rankings and user…

20 Best New Websites, September 2024

We have a mixed bag for you with both minimalist and maximalist designs, and single pagers alongside much bigger, but…

Exciting New Tools for Designers, September 2024

This time around we are aiming to simplify life, with some light and fast analytics, an all-in-one productivity…

3 Essential Design Trends, September 2024

September's web design trends have a fun, fall feeling ... and we love it. See what's trending in website design this…

Crafting Personalized Experiences with AI

Picture this: You open Netflix, and it’s like the platform just knows what you’re in the mood for. Or maybe you’re…

15 Best New Fonts, August 2024

Welcome to August’s roundup of the best fonts we’ve found over the last few weeks. 2024’s trend for flowing curves and…