-1

I'm very confused about the syntax of media queries, I saw many examples and each one has a different syntax, for example:

  1. @media only screen and (max-width: 420px) {...}
  2. @media screen and (max-width: 420px) {...}
  3. @media (max-width: 420px) {...}

What is the different between these three examples?

Are all of them legal?

And another question, I understand that you can write all instead of screen, but I never saw it in any example, why? is there something wrong with writing like this?

@media all and (max-width: 750px) {...} 

I know that sometimes people connect their mobile or PC to the TV and watch the things there, so if I want that my website will display correctly also on TV, isn't it better to use all instead of screen? (as I understood the option 'tv' was Deprecated).

Thanks.

kinny94
  • 376
  • 1
  • 5
  • 22
Robert
  • 1
  • 1
  • read [here](https://www.w3schools.com/cssref/css3_pr_mediaquery.asp) and [here](https://www.w3schools.com/css/css3_mediaqueries_ex.asp) – N. Ivanov Aug 22 '17 at 15:06
  • I read there, it's not answering my questions. – Robert Aug 22 '17 at 15:09
  • When you connect your mobile or PC to your TV, it is still the device that is rendering the content - not the TV. The TV value for @media queries was suppose to be used for something like [WebTV](https://motherboard.vice.com/en_us/article/4xaqe9/why-webtvs-remote-controlled-internet-failed-to-take-off) which is a dead technology and is why that value is deprecated. – zgood Aug 22 '17 at 15:16
  • @Robert you got negative because of duplicate - https://stackoverflow.com/questions/8549529/what-is-the-difference-between-screen-and-only-screen-in-media-queries – Shafikul Islam Aug 22 '17 at 15:21
  • @zgood So, will my website looks OK on TV if I use the screen option? or is it better to use all? when should I use the all mode? – Robert Aug 22 '17 at 15:21
  • Possible duplicate of [What is the difference between "screen" and "only screen" in media queries?](https://stackoverflow.com/questions/8549529/what-is-the-difference-between-screen-and-only-screen-in-media-queries) – disinfor Aug 22 '17 at 15:28
  • @Robert Yes it will look fine on a TV if you use `screen`. Screen is for anything with a screen - PC, mobile, TV, tablet. The only other 2 you should be aware of are `print` - styles for a printed piece of paper, and `speech`, which is used for screen readers for people with disabilities. And of course `all` which is screen, print and speech – zgood Aug 22 '17 at 15:48

3 Answers3

1

Yes, they are valid syntaxes. Generally, in web development, people write CSS to make webpage display a certain generic way on mobile or web. Those pages are not designed to look same way on every output medium, say on mobiles with different screen sizes, handheld tablets or you may want printed output of your page to look slightly different from your original page. May be, you want to hide certain details from print document or show some additional details to tablet users.

You can avoid designing pages for screens of every size separately and use media queries instead. That way, you can design your web page once and it will work and fit fine for all media (screen, print, speech, tv, mobile in portrait or landscape orientation with different screen sizes etc.).

Previously, if you would know, companies had different teams to design pages for website, mobile, table, iPhone retina display. People used to create different stylesheets for different stuff like print and mobile. May be HTML changes would also be required for display. Media query brings all these things under one umbrella. so, now you can design page once and it by styling it with media queries for different media or screen sizes, you can get different output on different devices. Same HTML 5 and CSS 3 code working in different look on different output medium.

Check out more at http://cssmediaqueries.com/

https://www.tutorialrepublic.com/css-tutorial/css3-media-queries.php

Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
0
@media only screen and (max-width: 420px) {...}

For a window with a max-width of 420px that you want to apply these styles.

@media screen and (max-width: 420px) {...}

For a device with a screen and a window with max-width of 420px apply the style.

@media (max-width: 420px) {...}
@media all and (max-width: 420px) {...}

For @media and can also be used for print interfaces not only screen.

All are legal no problem to use. Sometimes we need to do different stuff in print mode than Screen mode. So Use Properly when you need what.

Shafikul Islam
  • 359
  • 1
  • 10
-1

Everything works, it is just different if you want to target something like devices only. I use 2nd example from your question always, but everything will work.

Nemanja G
  • 1,760
  • 6
  • 29
  • 48
  • Thanks, and what about the all mode, if I want my website to work properly also when watching on tv, should I use all instead of screen? – Robert Aug 22 '17 at 15:18
  • @Robert you just specify the resolution, not TV, Mobile Phone, calculator.. Just the screen resolution, max width or max height – Nemanja G Aug 22 '17 at 15:19
  • OK, but when DO I need to use the all mode? – Robert Aug 22 '17 at 15:26