@media rule
The @media rule allows set style depending from media type and its features. There are CSS libraries like Bootstrap 5 that make this kind of work easier.
<link rel="stylesheet" media="screen and (min-width: 900px)" href="/css/widescreen.css">
<link rel="stylesheet" media="screen and (max-width: 320px)" href="/css/smallscreen.css">
<style>
@media print {
body { font-size: 10pt; }
}
@media screen {
body { font-size: 13px; }
}
@media screen, print {
body { line-height: 1.2; }
}
@media screen
and (min-width: 320px)
and (max-width: 480px)
and (resolution: 150dpi) {
body { line-height: 1.4; }
}
<!-- media query lvl 4 -->
@media (height > 600px) {
body { line-height: 1.4; }
}
@media (400px <= width <= 700px) {
body { line-height: 1.4; }
}
</style>
Media Queries 4:
- The logical operators not, and and or can be used to compose a complex media query. Comma can be used as or operator. Keyword only is deprecated.
- You can use improved syntax for define range of value via relational operators.
media types
Only followed types are allowed in Media Queries 4. And types like tv now are deprecated.
device | description |
---|---|
all | This is default value, means all types of media devices. |
Printing devices. | |
screen | Screen of computer, tablets, smart-phones etc. |
speech | Screen reader that allows you to read the page out loud. |
features
Some features can be prefixed with max- and min-. For example, aspect-ratio, min-aspect-ratio, max-aspect-ratio.
feature | description |
---|---|
height | The viewport height. |
width | The viewport width. |
aspect-ratio | Represents the width-to-height aspect ratio of the viewport.
|
resolution | Test the pixel density of the output device.
|
orientation | Test the orientation of the viewport. Possible values are:
|
any-hover | Checks is device has input mechanism allowing user to hover over elements. Possible values are:
|
any-pointer | Tests whether the user has any pointing device. Possible values are:
|
color | Test the number of bits per color component (red, green, blue) of the output device.
|
color-index | Tests the number of entries in the output device's color lookup table. |