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

Ross Horak
Ross Horak
20,890 Points

Percentage value does not work for media query max/min-width

I tried using a percentage value instead of a pixel value, however my code editor (sublime) linter returned an error and the media query did not work. This was my code:

@media screen and (max-width: 50%) { body { background: green; } }

When and how can one use percentage values in Media Queries properly?

2 Answers

Sean T. Unwin
Sean T. Unwin
28,660 Points

If you want to set the media query to 50% of the screen width I would recommend using vw units, where vw stands for View Width and 100 would be 100% of the view width. This works in all browsers except IE<9 and Opera Mini.

example.css
@media screen and (max-width: 50vw) {

}

I'm not too familiar with using percentages with media queries but I'm guessing that you need a parent element set at 100% percent.

Ross Horak
Ross Horak
20,890 Points

I tried setting parent to 100% - doesn't work either. I'm not entirely sure it is possible to use a percentage value when it comes to break points..