Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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