-
Notifications
You must be signed in to change notification settings - Fork 707
/
Copy pathOverview.bs
2420 lines (1941 loc) · 96.4 KB
/
Overview.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class='metadata'>
Title: Media Queries Level 4
Group: csswg
Shortname: mediaqueries
Level: 4
Status: ED
Status Text: There is currently no preliminary interoperability or implementation report.
Work Status: refining
Implementation Report: https://wpt.fyi/results/css/mediaqueries
ED: https://drafts.csswg.org/mediaqueries-4/
TR: https://www.w3.org/TR/mediaqueries-4/
Previous Version: https://www.w3.org/TR/2021/CRD-mediaqueries-4-20211225/
Editor: Florian Rivoal, Invited Expert, https://florian.rivoal.net, w3cid 43241
Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/, w3cid 42199
Former Editor: Håkon Wium Lie, Opera, howcome@opera.com
Former Editor: Tantek Çelik, Mozilla, tantek@cs.standard.edu
Former Editor: Daniel Glazman, Disruptive Innovations, daniel@glazman.org
Former Editor: Anne van Kesteren, Mozilla, annevk@annevk.nl
Abstract: <a>Media Queries</a> allow authors to test and query values or features of the user agent or display device, independent of the document being rendered. They are used in the CSS @media rule to conditionally apply styles to a document, and in various other contexts and languages, such as HTML and JavaScript.
Abstract:
Abstract: Media Queries Level 4 describes the mechanism and syntax of media queries, media types, and media features. It extends and supersedes the features defined in Media Queries Level 3.
Ignored Terms: min-resolution, max-resolution, none, view-mode, mediaText, CSSOMString
Link Defaults: css-break-3 (property) break-inside
Can I Use URL: https://drafts.csswg.org/mediaqueries-4/
Can I Use URL: http://drafts.csswg.org/mediaqueries-4/
Can I Use URL: https://drafts.csswg.org/mediaqueries/
Can I Use URL: http://drafts.csswg.org/mediaqueries/
Can I Use URL: https://www.w3.org/TR/mediaqueries-4/
Can I Use URL: http://www.w3.org/TR/mediaqueries-4/
Ignore Can I Use URL Failure: https://drafts.csswg.org/mediaqueries-4/
Ignore Can I Use URL Failure: http://drafts.csswg.org/mediaqueries-4/
Ignore Can I Use URL Failure: https://drafts.csswg.org/mediaqueries/
Ignore Can I Use URL Failure: http://drafts.csswg.org/mediaqueries/
Ignore Can I Use URL Failure: http://www.w3.org/TR/mediaqueries-4/
</pre>
<pre class=link-defaults>
spec:css-values-3; type:value; text:in
spec:css-values-3; type:value; text:cm
spec:css-values-3; type:value; text:px
spec:css-values-3; type:value; text:em
spec:css-values-3; type:type; text:<dimension>
spec:css-values-3; type:type; text:<ident>
spec:css-values-3; type:grammar; text:|
spec:css-values-3; type:grammar; text:?
spec:css-conditional-3; type:at-rule; text:@media
</pre>
<pre class=biblio>
{
"ITU-R-BT-2020-2": {
"href": "https://www.itu.int/rec/R-REC-BT.2020/en",
"title": "Parameter values for ultra-high definition television systems for production and international programme exchange",
"date": "October 2015"
},
"Display-P3": {
"title": "Display P3",
"authors": "Apple, Inc",
"date": "2022-02",
"href": "https://www.color.org/chardata/rgb/DisplayP3.xalter"
}
}
</pre>
<h2 id="intro">
Introduction</h2>
<em>This section is not normative.</em>
In 1997, HTML4 [[HTML401]] defined a mechanism to support media-dependent style sheets,
tailored for different <a>media types</a>.
For example, a document may use different style sheets for screen and for print.
In HTML, this can be written as:
<div class="example">
<pre>
<link rel="stylesheet" type="text/css" media="screen" href="style.css">
<link rel="stylesheet" type="text/css" media="print" href="print.css">
</pre>
</div>
CSS adapted and extended this functionality with its ''@media'' and ''@import'' rules,
adding the ability to query the value of individual features:
<div class="example">
Inside a CSS style sheet,
one can declare that sections apply to certain <a>media types</a>:
<pre>
@media screen {
* { font-family: sans-serif }
}
</pre>
Similarly, stylesheets can be conditionally imported based on media queries:
<pre>@import "print-styles.css" print;</pre>
</div>
<a>Media queries</a> can be used with HTML, XHTML, XML [[xml-stylesheet]] and the @import and @media rules of CSS.
<div class="example">
Here is the same example written in HTML, XHTML, XML, @import and @media:
<pre>
<link media="screen and (color), projection and (color)"
rel="stylesheet" href="example.css">
<link media="screen and (color), projection and (color)"
rel="stylesheet" href="example.css" />
<?xml-stylesheet media="screen and (color), projection and (color)"
rel="stylesheet" href="example.css" ?>
@import url(example.css) screen and (color), projection and (color);
@media screen and (color), projection and (color) { … }
</pre>
</div>
<h3 id="placement">
Module interactions</h3>
This module replaces and extends the Media Queries, Media Type and Media Features
defined in [[!CSS2]] sections 7 and in [[!MEDIAQUERIES-3]].
<h3 id="values">
Values</h3>
Value types not defined in this specification, such as <<integer>>,
<<number>> or <<resolution>>, are defined in [[!CSS-VALUES-3]]. Other CSS
modules may expand the definitions of these value types.
<h3 id="units">
Units</h3>
The units used in media queries are the same as in other parts of CSS, as
defined in [[!CSS-VALUES-3]]. For example, the pixel unit represents CSS pixels and
not physical pixels.
<a spec="css-values-3">Relative length</a> units in media queries are based on the <a>initial value</a>, which means
that units are never based on results of declarations. <span class=note>For example, in HTML,
the ''em'' unit is relative to the <a>initial value</a> of 'font-size',
defined by the user agent or the user's preferences,
not any styling on the page.</span>
<!--
██ ██ ███████
███ ███ ██ ██
████ ████ ██ ██
██ ███ ██ ██ ██
██ ██ ██ ██ ██
██ ██ ██ ██
██ ██ █████ ██
-->
<h2 id="media">
Media Queries</h2>
A <dfn export>media query</dfn> is a method of testing certain aspects of the user agent
or device that the document is being displayed in.
<a>Media queries</a> are (almost) always independent of the contents of the document,
its styling,
or any other internal aspect;
they're only dependent on “external” information
unless another feature explicitly specifies that it affects the resolution of Media Queries.
The syntax of a <a>media query</a> consists of
an optional <a>media query modifier</a>,
an optional <a>media type</a>,
and zero or more <a>media features</a>:
<pre class='railroad'>
Or:
N: media condition
And:
Or: 1
T: only
S:
T: not
N: media type
Opt:
And:
T: and
N: media condition
</pre>
A <a>media query</a> is a logical expression that is either true or false.
A media query is true if:
* the <a>media type</a>,
if specified,
matches the media type of the device where the user agent is running, and
* the <a>media condition</a> is true.
Statements regarding media queries in this section assume the <a href="#mq-syntax">syntax section</a> is followed.
Media queries that do not conform to the syntax are discussed in [[#error-handling]].
I.e. the syntax takes precedence over requirements in this section.
<div class="example">
Here is a simple example written in HTML:
<pre><link rel="stylesheet" media="screen and (color)" href="example.css" /></pre>
This example expresses that a certain style sheet
(<code>example.css</code>) applies to devices of a certain media type
(''screen'') with certain feature (it must be a color screen).
Here is the same media query written in an @import-rule in CSS:
<pre>@import url(example.css) screen and (color);</pre>
</div>
User agents must re-evaluate <a>media queries</a> in response to changes in the user environment that they're aware of,
for example if the device is tiled from landscape to portrait orientation,
and change the behavior of any constructs dependent on those <a>media queries</a> accordingly.
Unless another feature explicitly specifies that it affects the resolution of Media Queries, it is never necessary to apply a style sheet in order to evaluate expressions.
<h3 id='mq-list'>
Combining Media Queries</h3>
Several <a>media queries</a> can be combined into a comma-separated <dfn export>media query list</dfn>.
<pre class='railroad'>
Star:
N: media query
T: ,
</pre>
A <a>media query list</a> is true if <em>any</em> of its component <a>media queries</a> are true,
and false only if <em>all</em> of its component <a>media queries</a> are false.
<div class="example">
For example, the following <a>media query list</a> is true if either
the <a>media type</a> is ''screen'' and it's a color device,
<strong>or</strong> the <a>media type</a> is ''projection'' and it's a color device:
<pre>
@media screen and (color), projection and (color) { … }
</pre>
</div>
An empty <a>media query list</a> evaluates to true.
<div class="example">
For example, these are equivalent:
<pre>
@media all { … }
@media { … }
</pre>
</div>
<h3 id='mq-prefix'>
Media Query Modifiers</h3>
A <a>media query</a> may optionally be prefixed by a single <dfn export>media query modifier</dfn>,
which is a single keyword which alters the meaning of the following <a>media query</a>.
<h4 id='mq-not'>
Negating a Media Query: the ''not'' keyword</h4>
An individual <a>media query</a> can have its result negated
by prefixing it with the keyword <dfn value for="@media">not</dfn>.
If the <a>media query</a> would normally evaluate to true,
prefixing it with ''not'' makes it evaluate to false,
and vice versa.
<div class="example">
For example, the following will apply to everything except color-capable screens.
Note that the entire media query is negated,
not just the <a>media type</a>.
<pre><link rel="stylesheet" media="not screen and (color)" href="example.css" /></pre>
</div>
<h4 id='mq-only'>
Hiding a Media Query From Legacy user agents: the ''only'' keyword</h4>
The concept of <a>media queries</a> originates from HTML4 [[HTML401]].
That specification only defined <a>media types</a>,
but had a forward-compatible syntax that accommodated the addition of future concepts like <a>media features</a>:
it would consume the characters of a <a>media query</a> up to the first non-alphanumeric character,
and interpret that as a <a>media type</a>,
ignoring the rest.
For example, the <a>media query</a> ''screen and (color)''
would be truncated to just ''screen''.
Unfortunately, this means that legacy user agents using this error-handling behavior
will ignore any <a>media features</a> in a <a>media query</a>,
even if they're far more important than the <a>media type</a> in the query.
This can result in styles accidentally being applied in inappropriate situations.
To hide these <a>media queries</a> from legacy user agents,
the <a>media query</a> can be prefixed with the keyword <dfn value for="@media">only</dfn>.
The ''only'' keyword <strong>has no effect</strong> on the <a>media query</a>’s result,
but will cause the <a>media query</a> to be parsed by legacy user agents
as specifying the unknown <a>media type</a> “only”,
and thus be ignored.
<div class="example">
In this example, the stylesheet specified by the <code><link></code> element
will not be used by legacy user agents,
even if they would normally match the ''screen'' <a>media type</a>.
<pre><link rel="stylesheet" media="only screen and (color)" href="example.css" /></pre>
</div>
Note: Note that the ''only'' keyword can only be used before a <a>media type</a>.
A <a>media query</a> consisting only of <a>media features</a>,
or one with another <a>media query modifier</a> like ''not'',
will be treated as false by legacy user agents automatically.
Note: At the time of publishing this specification,
such legacy user agents are extremely rare,
and so using the ''only'' modifier is rarely, if ever, necessary.
<!--
████████ ██ ██ ████████ ████████ ██████
██ ██ ██ ██ ██ ██ ██ ██
██ ████ ██ ██ ██ ██
██ ██ ████████ ██████ ██████
██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██
██ ██ ██ ████████ ██████
-->
<h3 id='media-types'>
Media Types</h3>
A <dfn export>media type</dfn> is a broad category of user-agent devices
on which a document may be displayed.
The original set of <a>media types</a> were defined in HTML4,
for the <code>media</code> attribute on <code><link></code> elements.
Unfortunately, <a>media types</a> have proven insufficient as a way of discriminating between devices with different styling needs.
Some categories which were originally quite distinct,
such as ''screen'' and ''handheld'',
have blended significantly in the years since their invention.
Others, such as ''tty'' or ''tv'',
expose useful differences from the norm of a full-featured computer monitor,
and so are potentially useful to target with different styling,
but the definition of <a>media types</a> as mutually exclusive
makes it difficult to use them in a reasonable manner;
instead, their exclusive aspects are better expressed as <a>media features</a>
such as '@media/grid' or 'scan'.
As such, the following <a>media types</a> are defined for use in <a>media queries</a>:
<dl dfn-type=value dfn-for="@media">
<dt><dfn>all</dfn>
<dd>Matches all devices.
<dt><dfn>print</dfn>
<dd>Matches printers, and devices intended to reproduce a printed display,
such as a web browser showing a document in “Print Preview”.
<dt><dfn>screen</dfn>
<dd>Matches all devices that aren't matched by ''print''.
</dl>
In addition, the following <strong>deprecated</strong> <a>media types</a> are defined.
Authors must not use these <a>media types</a>;
instead, it is recommended that they select appropriate <a>media features</a>
that better represent the aspect of the device that they are attempting to style against.
User agents must recognize the following <a>media types</a> as valid,
but must make them match nothing.
<ul dfn-type=value dfn-for="@media">
<li><dfn>tty</dfn>
<li><dfn>tv</dfn>
<li><dfn>projection</dfn>
<li><dfn>handheld</dfn>
<li><dfn>braille</dfn>
<li><dfn>embossed</dfn>
<li><dfn>aural</dfn>
<li><dfn>speech</dfn>
</ul>
Note: It is expected that all of the media types will also be deprecated in time,
as appropriate <a>media features</a> are defined which capture their important differences.
<!--
████████ ████████ ███ ████████ ██ ██ ████████ ████████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██████ ██ ██ ██ ██ ██ ████████ ██████ ██████
██ ██ █████████ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ████████ ██ ██ ██ ███████ ██ ██ ████████ ██████
-->
<h3 id='mq-features'>
Media Features</h3>
A <dfn export>media feature</dfn> is a more fine-grained test than <a>media types</a>,
testing a single, specific feature of the user agent or display device.
Syntactically, <a>media features</a> resemble CSS properties:
they consist of a feature name, a colon, and a value to test for.
They may also be written in boolean form as just a feature name,
or in range form with a comparison operator.
<pre class='railroad'>
T: (
Choice:
And:
N: feature name
T: :
N: feature value
N: feature name
And:
N: range form
C: (see below)
T: )
</pre>
There are, however, several important differences between properties and media features:
<ul>
<li>
Properties are used to give information about how to present a document.
Media features are used to describe requirements of the output device.
<li>
Media features are always wrapped in parentheses
and combined with the ''and'' or ''or'' keywords,
like ''(color) and (min-width: 600px)'',
rather than being separated with semicolons.
<li>
A media feature may be given with <em>only</em> its name
(omitting the colon and value)
to evaluate the feature in a <a>boolean context</a>.
This is a convenient shorthand for features that have a reasonable value representing 0 or “none”.
For example, ''(color)'' is true if the '@media/color' <a>media feature</a> is non-zero.
<li>
<a>Media features</a> with “range” type can be written in a <a>range context</a>,
which uses standard mathematical comparison operators rather than a colon,
or have their feature names <a href=#mq-min-max>prefixed with “min-” or “max-”</a>.
<li>
Properties sometimes accept complex values,
e.g., calculations that involve several other values.
<a>Media features</a> only accept single values: one keyword, one number, etc.
</ul>
If a <a>media feature</a> references a concept which does not exist on the device where the UA is running
(for example, speech UAs do not have a concept of “width”),
the <a>media feature</a> must always evaluate to false.
<div class="example">
The media feature ''device-aspect-ratio'' only applies to
visual devices. On an ''speech'' device, expressions involving
''device-aspect-ratio'' will therefore always be false:
<pre>
<link media="speech and (device-aspect-ratio: 16/9)"
rel="stylesheet" href="example.css">
</pre>
</div>
<h4 id='mq-ranges'>
Media Feature Types: “range” and “discrete”</h4>
Every media feature defines its “type” as either “range” or “discrete” in its definition table.
“Discrete” media features,
like 'pointer'
take their values from a set.
The values may be keywords
or boolean numbers (0 and 1),
but the common factor is that there's no intrinsic “order” to them--
none of the values are “less than” or “greater than” each other.
“Range” media features like '@media/width', on the other hand,
take their values from a range.
Any two values can be compared to see which is lesser and which is greater.
The only significant difference between the two types is that “range” <a>media features</a>
can be evaluated in a <a>range context</a>
and accept “min-” and “max-” prefixes on their name.
Doing either of these changes the meaning of the feature--
rather than the <a>media feature</a> being true when the feature exactly matches the given value,
it matches when the feature is greater than/less than/equal to the given value.
<div class='example'>
A ''(width >= 600px)'' <a>media feature</a> is true
when the viewport's width is ''600px'' <em>or more</em>.
On the other hand, ''(width: 600px)'' by itself is only true
when the viewport's width is <em>exactly</em> ''600px''.
If it's less or greater than ''600px'', it'll be false.
</div>
<!--
████████ ███████ ███████ ██ ████████ ███ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
████████ ██ ██ ██ ██ ██ ██████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ █████████ ██ ████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███
████████ ███████ ███████ ████████ ████████ ██ ██ ██ ██
-->
<h4 id='mq-boolean-context'>
Evaluating Media Features in a Boolean Context</h4>
While <a>media features</a> normally have a syntax similar to CSS properties,
they can also be written more simply as just the feature name,
like ''(color)''.
When written like this, the <a>media feature</a> is evaluated in a <dfn export>boolean context</dfn>.
If the feature would be true for any value
<em>other than</em> the number ''0'',
a <<dimension>> with the value ''0'',
or the keyword ''@media/update/none'',
the <a>media feature</a> evaluates to true.
Otherwise, it evaluates to false.
<div class='example'>
Some <a>media features</a> are designed to be written like this.
For example, 'update' is typically written as ''(update)'' to test if any kind of updating is available,
or ''not (update)'' to check for the opposite.
It can still be given an explicit value as well,
with ''(update: fast) or (update: slow)'' equal to ''(update)'',
and ''(update: none)'' equal to ''not (update)''.
</div>
<div class='example'>
Some numeric <a>media features</a>, like '@media/width',
are rarely if ever useful to evaluate in a <a>boolean context</a>,
as their values are almost always greater than zero.
Others, like '@media/color', have meaningful zero values:
''(color)'' is identical to ''(color > 0)'',
indicating that the device is capable of displaying color at all.
</div>
<div class='example'>
Only some of the <a>media features</a> that accept keywords are meaningful in a <a>boolean context</a>.
For example, ''(pointer)'' is useful,
as 'pointer' has a ''pointer/none'' value to indicate there's no pointing device at all on the device.
Similarly, ''not (color-gamut)'' can be useful to detect a very low-quality screen,
as such a device won't match any of the '@media/color-gamut' keywords;
even tho '@media/color-gamut' lacks a <css>none</css> keyword,
it'll still be false in a [=boolean context=]
because none of its values match.
On the other hand, ''(scan)'' is just always true or always false
(depending on whether it applies at all to the device),
as, if it applies at all,
the device is guaranteed to match at least one of its values.
</div>
<!--
████████ ███ ██ ██ ██████ ████████
██ ██ ██ ██ ███ ██ ██ ██ ██
██ ██ ██ ██ ████ ██ ██ ██
████████ ██ ██ ██ ██ ██ ██ ████ ██████
██ ██ █████████ ██ ████ ██ ██ ██
██ ██ ██ ██ ██ ███ ██ ██ ██
██ ██ ██ ██ ██ ██ ██████ ████████
-->
<h4 id="mq-range-context">
Evaluating Media Features in a Range Context</h4>
<a>Media features</a> with a “range” type can be alternately written in a <dfn export>range context</dfn>
that takes advantage of the fact that their values are ordered,
using ordinary mathematical comparison operators:
<pre class='railroad'>
T: (
Choice:
Seq:
N: feature name/value
Choice: 4
T: =
T: <
T: <=
T: >
T: >=
N: feature value/name
Seq:
N: value
Choice:
T: <
T: <=
N: feature name
Choice:
T: <
T: <=
N: value
Seq:
N: value
Choice:
T: >
T: >=
N: feature name
Choice:
T: >
T: >=
N: value
T: )
</pre>
Note: This syntax is new to Level 4 of Mediaqueries,
and thus is not as widely supported at the moment
as the ''min-''/''max-'' prefixes.
The basic form,
consisting of a feature name,
a comparison operator,
and a value,
returns true if the relationship is true.
<div class='example'>
For example, ''(height > 600px)''
(or ''(600px < height)'')
returns true if the viewport height is greater than ''600px''.
</div>
The remaining forms,
with the feature name nested between two value comparisons,
returns true if both comparisons are true.
<div class='example'>
For example, ''(400px < width < 1000px)'' returns true if the viewport width is between ''400px'' and ''1000px''
(but not equal to either).
</div>
Some media features with a "range" type are said to be <dfn>false in the negative range</dfn>.
This means that negative values are valid and must be parsed, and that
querying whether the media feature is equal to, less than, or less or equal than
any such negative value must evaluate to false.
Querying whether the media feature is greater, or greater or equal, than a negative
value evaluates to true if the relationship is true.
Note: If negative values had been rejected at parse time instead,
they would be treated as ''unknown'' based on the error handling rules.
However, in reality,
whether a device's '@media/resolution' is ''-300dpi'' is not unknown, it is known to be false.
Similarly, for any visual device, the '@media/width' of the targeted display area is known to be greater than ''-200px''
The above rule reflects that,
making intuition match what UAs do.
<div class=example>
The following examples result in a green background on all visual devices:
<pre><code class=lang-css>
@media not (width <= -100px) {
body { background: green; }
}
</code></pre>
<pre><code class=lang-css>
@media (height > -100px) {
body { background: green; }
}
</code></pre>
<pre><code class=lang-css>
@media not (resolution: -300dpi) {
body { background: green; }
}
</code></pre>
</div>
<div class=advisement>
This is a behavior change compared to Media Queries Level 3 [[MEDIAQUERIES-3]],
where negative values on these properties caused a syntax error.
In level 3, syntax errors—including forbidden values—resulted in the entire <a>media query</a> being false,
rather than the ''unknown'' treatment defined in this level.
Implementations updating from level 3 should make sure
to change the handling of negative values for the relevant properties
when they add support for the richer syntax defined in [[#media-conditions]],
to avoid introducing unintended semantics.
</div>
<!--
██ ██ ████ ██ ██ ██ ██ ██ ███ ██ ██
███ ███ ██ ███ ██ ██ ███ ███ ██ ██ ██ ██
████ ████ ██ ████ ██ ██ ████ ████ ██ ██ ██ ██
██ ███ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ███
██ ██ ██ ██ ████ ██ ██ ██ █████████ ██ ██
██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██
██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██
-->
<h4 id='mq-min-max'>
Using “min-” and “max-” Prefixes On Range Features</h4>
Rather than evaluating a “range” type <a>media feature</a> in a range context,
as described above,
the feature may be written as a normal <a>media feature</a>,
but with a “min-” or “max-” prefix on the feature name.
This is equivalent to evaluating the feature in a <a>range context</a>,
as follows:
<ul>
<li>
Using a “min-” prefix on a feature name is equivalent to using the “>=” operator.
For example, ''(min-height: 600px)'' is equivalent to ''(height >= 600px)''.
<li>
Using a “max-” prefix on a feature name is equivalent to using the “<=” operator.
For example, ''(max-width: 40em)'' is equivalent to ''(width <= 40em)''.
</ul>
Note: because “min-” and “max-” both equate to range comparisons that <strong>include</strong> the value,
they may be limiting in certain situations.
<div class='example'>
For instance,
authors trying to define different styles based on a breakpoint in the viewport width using “min-” and “max-”
would generally offset the values they're comparing,
to ensure that both queries don't evaluate to true simultaneously.
Assuming the breakpoint is at 320px, authors would conceptually use:
<pre>
@media (max-width: 320px) { /* styles for viewports <= 320px */ }
@media (min-width: 321px) { /* styles for viewports >= 321px */ }
</pre>
While this ensures that the two sets of styles don't apply simultaneously when the viewport width is 320px,
it does not take into account the possibility of fractional viewport sizes which can occur as a result of non-integer pixel densities
(e.g. on high-dpi displays or as a result of zooming/scaling).
Any viewport widths that fall between 320px and 321px will result in none of the styles being applied.
One approach to work around this problem is to increase the precision of the values used for the comparison. Using the example above,
changing the second comparison value to 320.01px significantly reduces the chance that a viewport width on a device would fall
between the cracks.
<pre>
@media (max-width: 320px) { /* styles for viewports <= 320px */ }
@media (min-width: 320.01px) { /* styles for viewports >= 320.01px */ }
</pre>
However, in these situations, <a>range context</a> queries (which are not limited to “>=” and “<=” comparisons) offer a more appropriate solution:
<pre>
@media (width <= 320px) { /* styles for viewports <= 320px */ }
@media (width > 320px) { /* styles for viewports > 320px */ }
</pre>
</div>
“Discrete” type properties do not accept “min-” or “max-” prefixes.
Adding such a prefix to a “discrete” type <a>media feature</a> simply results in an unknown feature name.
<div class='example'>
For example,
''(min-grid: 1)'' is invalid,
because '@media/grid' is a “discrete” <a>media feature</a>,
and so doesn't accept the prefixes.
(Even though the '@media/grid' <a>media feature</a> appears to be numeric,
as it accepts the values ''0'' and ''1''.)
</div>
Attempting to evaluate a min/max prefixed <a>media feature</a> in a <a>boolean context</a>
is invalid and a syntax error.
Combining Media Features {#media-conditions}
--------------------------------------------
Multiple <a>media features</a> can be combined together into a <dfn export>media condition</dfn>
using full boolean algebra
(not, and, or).
* Any media feature can be negated by placing ''not'' before it.
For example, ''not (color)'' inverts the meaning of ''(color)''--
since ''(color)'' matches a device with any kind of color display,
''not (color)'' matches a device <em>without</em> any kind of color display.
* Two or more media features can be chained together,
such that the query is only true if <em>all</em> of the media features are true,
by placing ''and'' between them.
For example, ''(width < 600px) and (height < 600px)''
only matches devices whose screens are smaller than ''600px'' wide in both dimensions.
* Alternately, two or more media features can be chained together,
such that the query is true if <em>any</em> of the media features are true,
by placing ''or'' between them.
For example, ''(update: slow) or (hover: none)''
matches if the device is slow to update the screen (such as an e-reader)
<em>or</em> the primary pointing device has no hover capability,
perhaps indicating that one should use a layout that displays more information
rather than compactly hiding it until the user hovers.
* <a>Media conditions</a> can be grouped by wrapping them in parentheses ''()''
which can then be nested within a condition the same as a single media query.
For example, ''(not (color)) or (hover)''
is true on devices that are monochrome
and/or that have hover capabilities.
If one instead wanted to query for a device that was monochrome and <em>didn't</em> have hover capabilities,
it must instead be written as ''not ((color) or (hover))''
(or, equivalently, as ''(not (color)) and (not (hover))'').
It is <em>invalid</em> to mix ''and'' and ''or'' and ''not'' at the same “level” of a media query.
For example, ''(color) and (pointer) or (hover)'' is illegal,
as it's unclear what was meant.
Instead, parentheses can be used to group things using a particular joining keyword,
yielding either ''(color) and ((pointer) or (hover))''
or ''((color) and (pointer)) or (hover)''.
These two have very different meanings:
if only ''(hover)'' is true,
the first one evaluates to false
but the second evaluates to true.
<!--
██████ ██ ██ ██ ██ ████████ ███ ██ ██
██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██
██ ████ ████ ██ ██ ██ ██ ██ ██
██████ ██ ██ ██ ██ ██ ██ ██ ███
██ ██ ██ ████ ██ █████████ ██ ██
██ ██ ██ ██ ███ ██ ██ ██ ██ ██
██████ ██ ██ ██ ██ ██ ██ ██ ██
-->
<h2 id='mq-syntax'>
Syntax</h2>
Informal descriptions of the media query syntax appear in the prose and railroad diagrams in previous sections.
The formal media query syntax is described in this section,
with the rule/property grammar syntax defined in [[!CSS-SYNTAX-3]] and [[!CSS-VALUES-3]].
To parse a <dfn><media-query-list></dfn> production,
<a>parse a comma-separated list of component values</a>,
then parse each entry in the returned list as a <<media-query>>.
Its value is the list of <<media-query>>s so produced.
Note: This explicit definition of <<media-query-list>> parsing
is necessary to make the error-recovery behavior of <a>media query lists</a> well-defined.
Note: This definition of <<media-query-list>> parsing intentionally accepts an empty list.
Note: As per [[!CSS-SYNTAX-3]], tokens are <a>ASCII case-insensitive</a>.
<pre>
<dfn><media-query></dfn> = <<media-condition>>
| [ not | only ]? <<media-type>> [ and <<media-condition-without-or>> ]?
<dfn><media-type></dfn> = <<ident>>
<dfn><media-condition></dfn> = <<media-not>> | <<media-in-parens>> [ <<media-and>>* | <<media-or>>* ]
<dfn><media-condition-without-or></dfn> = <<media-not>> | <<media-in-parens>> <<media-and>>*
<dfn><media-not></dfn> = not <<media-in-parens>>
<dfn><media-and></dfn> = and <<media-in-parens>>
<dfn><media-or></dfn> = or <<media-in-parens>>
<dfn><media-in-parens></dfn> = ( <<media-condition>> ) | ( <<media-feature>> ) | <<general-enclosed>>
<dfn><media-feature></dfn> = [ <<mf-plain>> | <<mf-boolean>> | <<mf-range>> ]
<dfn><mf-plain></dfn> = <<mf-name>> : <<mf-value>>
<dfn><mf-boolean></dfn> = <<mf-name>>
<dfn><mf-range></dfn> = <<mf-name>> <<mf-comparison>> <<mf-value>>
| <<mf-value>> <<mf-comparison>> <<mf-name>>
| <<mf-value>> <<mf-lt>> <<mf-name>> <<mf-lt>> <<mf-value>>
| <<mf-value>> <<mf-gt>> <<mf-name>> <<mf-gt>> <<mf-value>>
<dfn><mf-name></dfn> = <<ident>>
<dfn><mf-value></dfn> = <<number>> | <<dimension>> | <<ident>> | <<ratio>>
<dfn><mf-lt></dfn> = '<' '='?
<dfn><mf-gt></dfn> = '>' '='?
<dfn><mf-eq></dfn> = '='
<dfn><mf-comparison></dfn> = <<mf-lt>> | <<mf-gt>> | <<mf-eq>>
<dfn><general-enclosed></dfn> = [ <<function-token>> <<any-value>>? ) ] | [ ( <<any-value>>? ) ]
</pre>
The <<media-type>> production does not include the keywords ''only'', ''not'', ''and'', ''or'', and ''layer''.
Note: The exclusion of ''layer'' is because
it would otherwise be ambiguous
when when used in the <code highlight=css>@import url(…) layer;</code> syntax
for the sake of [=cascade layers=].
See [[CSS-CASCADE-5]].
No whitespace is allowed between the “<” or “>” <<delim-token>>s and the following “=” <<delim-token>>,
if it's present.
Note: Whitespace is required between a ''not'', ''and'', or ''or'' keyword
and the following ''('' character,
because without it that would instead parse as a <<function-token>>.
This is not made explicitly invalid because it's already covered by the above grammar.
It's fine to have whitespace between a '')'' and a following keyword,
however.
When parsing the <<media-in-parens>> production,
the <<general-enclosed>> branch must only be chosen if the input does not match either of the preceding branches.
<span class='note'><<general-enclosed>> exists to allow for future expansion of the grammar in a reasonably compatible way.</span>
Evaluating Media Queries {#evaluating}
--------------------------------------
Each of the major subexpression of <<media-condition>> or <<media-condition-without-or>> is associated with a boolean result, as follows:
<dl>
<dt><<media-condition>>
<dt><<media-condition-without-or>>
<dd>
The result is the result of the child subexpression.
<dt><<media-in-parens>>
<dd>
The result is the result of the child term.
<dt><<media-not>>
<dd>
The result is the negation of the <<media-in-parens>> term.
The negation of unknown is unknown.
<dt><<media-in-parens>> <<media-and>>*
<dd>
The result is true if the <<media-in-parens>> child term
and all of the <<media-in-parens>> children of the <<media-and>> child terms are true,
false if at least one of these <<media-in-parens>> terms are false,
and unknown otherwise.
<dt><<media-in-parens>> <<media-or>>*
<dd>
The result is false if the <<media-in-parens>> child term
and all of the <<media-in-parens>> children of the <<media-or>> child terms are false,
true if at least one of these <<media-in-parens>> terms are true,
and unknown otherwise.
<dt><<general-enclosed>>
<dd>
The result is unknown.
Authors must not use <<general-enclosed>> in their stylesheets.
<span class='note'>It exists only for future-compatibility,
so that new syntax additions do not invalidate too much of a <<media-condition>> in older user agents.</span>
<dt><<media-feature>>
<dd>
The result is the result of evaluating the specified media feature.
</dl>
If the result of any of the above productions
is used in any context that expects a two-valued boolean,
“unknown” must be converted to “false”.
Note: This means that,
for example,
when a <a>media query</a> is used in a ''@media'' rule,
if it resolves to “unknown” it's treated as “false”
and fails to match.
<div class="note">
Media Queries use a three-value logic where terms can be “true”, “false”, or “unknown”.
Specifically, it uses the <a href="http://en.wikipedia.org/wiki/Three-valued_logic#Kleene_and_Priest_logics">Kleene 3-valued logic</a>.
In this logic, “unknown” means “either true or false, but we're not sure which yet”.
In general, an unknown value showing up in a formula will cause the formula to be unknown as well,
as substituting “true” for the unknown will give the formula a different result than substituting “false”.
The only way to eliminate an unknown value is to use it in a formula that will give the same result
whether the unknown is replaced with a true or false value.
This occurs when you have “false AND unknown” (evaluates to false regardless)
and “true OR unknown” (evaluates to true regardless).
This logic was adopted because <<general-enclosed>> needs to be assigned a truth value.
In standard boolean logic, the only reasonable value is “false”,
but this means that ''not unknown(function)'' is true,
which can be confusing and unwanted.
Kleene's 3-valued logic ensures that unknown things will prevent a <a>media query</a> from matching,
unless their value is irrelevant to the final result.
</div>
<h3 id="error-handling">
Error Handling</h3>
A media query that does not match the grammar in the previous section must be replaced by ''not all'' during parsing.
Note: Note that a grammar mismatch does <strong>not</strong> wipe out an entire <a>media query list</a>,
just the problematic <a>media query</a>.
The parsing behavior defined above automatically recovers at the next top-level comma.
<div class='example'>
<pre>
@media (example, all,), speech { /* only applicable to speech devices */ }
@media &test, speech { /* only applicable to speech devices */ }
</pre>
Both of the above <a>media query lists</a> are turned into ''not all, speech'' during parsing,
which has the same truth value as just ''speech''.
Note that error-recovery only happens at the top-level of a <a>media query</a>;
anything inside of an invalid parenthesized block
will just get turned into ''not all'' as a group.
For example:
<pre>
@media (example, speech { /* rules for speech devices */ }