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 CSS Basics (2014) Enhancing the Design With CSS Media Query Basics

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

Why the difference between @media screen and () {} and @media () {} ?

In the "How to Make a Website" we were taught to write media queries for the Portfolio website, like this:

@media screen and () {}

while in the "CSS Basics" for the Lake Tahoe webiste, like this:

@media () {}

Why the difference between the two?

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Kabir,

@media can also be used for print interfaces, so @media screen is simply specifying that these rules are to be implemented when displaying on a screen, but not when printing. @media by itself is more broad, and would apply to print.

There are probably other media that can be targeted, but screen and print are the two I know about.

Best,

-Greg

kabir k
kabir k
Courses Plus Student 18,036 Points

Thanks, Greg. So why didn't we use @media for the Portflio website then?

Also, Nick wrote the media queries as:

@media screen and () {} (with the "and" operator)

while Guil wrote them as:

@media () {} (without the "and" operator)

Is the "and" operator not necessary in the second case?

Greg Kaleka
Greg Kaleka
39,021 Points

Sometimes you want to target just screens, other times you want to target everything. It depends on the project.

Nick needed to use "and" because of the "screen" that comes before it. You read a media query like this:

@media screen and (max-width: 900px) {} "For media that is a screen and with a max width of 900px, do this stuff"

@media (max-width: 900px) {} "For media that has a max-width of 900px, do this stuff"

So just like in plain English, there's no need for and in the second media query. Make sense?