Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS Responsive Layouts Media Queries Creating Breakpoints

Please check my idea about responsive webdesign.

As I understand Responsive webdesign is always fluid until it reaches the breakpoints for different devices. And then I wrap my CSS in media queries so it only works with fixed screen sizes. Am I getting it right?

1 Answer

Not really. "Responsive web design" is a strategy for sizing and presenting web page elements. Media queries just help you achieve a responsive web design.

The only way to make your design fluid is to use percentages when setting element widths (actually, there are other relative measurements, but percentages are most common). Here's an example:

This is NOT fluid or responsive:

<style>
#myDiv { width: 400px; }
</style>
<div id="myDiv">
</div>

This IS fluid and responsive:

<style>
#myDiv { width: 50%; }
</style>
<div id="myDiv">
</div>

The second div is fluid and responsive because it will always be 50% (half the width) of the browser window.

But now you think, you really don't want that div to be 50% of the browser window if the browser window is small, like on a phone. That's where Media Queries come in. You think like, "if the browser window is less than 600px wide, I want that div to be the whole width of the window." So to do that, you'd add a media query AFTER the original css setting, like this:

<style>
#myDiv { width: 50%; }
@media screen and (max-width: 600px) {
    #myDiv { width: 100%; }
}
</style>
<div id="myDiv">
</div>

With this combination, the browser will make the div 100% of the browser window IF the window is at most 600px, otherwise it will make the div 50% of the browser window.

Voila, you're practicing responsive web design.

Thanks Eric