0

What does the 'only screen' code mean in a media query?

//See below the 'only screen' part of the code
@media only screen and (min-device-width: 320px)

I have used code that works without the 'only screen' part of the code. What does it do or how does it help?

YulePale
  • 6,688
  • 16
  • 46
  • 95

2 Answers2

5

From https://www.w3schools.com/cssref/css3_pr_mediaquery.asp

only: The only keyword prevents older browsers that do not support media queries with media features from applying the specified styles. It has no effect on modern browsers.

The screen keyword references that it's a computer, mobile phone or tablet etc. There are two other media types, print and speech, as well as the default all.

Simply: The only keyword is optional and used for backwards compatibility. The screen keyword will default to all.

Xweque
  • 605
  • 1
  • 9
  • 29
1
@media only screen and (min-device-width: 320px)

As you said and it's true that code can run without only screen but when you use it that means you are only targeting screens like desktop, laptop, tablet, mobile etc. screen devices not other media devices like printing media and screen readers etc.

resource: https://www.w3.org/TR/CSS2/media.html

Abhishek Kumar
  • 738
  • 1
  • 9
  • 17