This is the 7th day of my participation in Gwen Challenge
Attribute profile
- Apply a portion of a stylesheet based on the results of one or more media queries. You can define different styles for different media types.
- When a page needs a responsive layout,
@media
Is very useful. Because the browser resize, the page will also rerender the page according to the browser width and height, so@media
Recalculation does not load the style.
Sample:
/* Screen when the device is a computer screen, tablet, smartphone, etc. And width > 900px uses the.article style block */
@media screen and (min-width: 900px) {
.article {
padding: 1rem 3rem; }}Copy the code
@media
Rules can be placed on top of your code or anywhere else@ Conditional rule group
Inside.
The media type
- This section describes the types of devices. The media type is optional and the default is
all
Type.
all
Load it on all devices.print
Load paging material and documents viewed on screen in print preview mode.screen
Computer screens, tablets, smart phones, etc.speech
Screen readers and other sound devices loaded.
CSS2.1 and Media Query 3
Several other media types are defined(TTY, TV, Projection, Handheld, Braille, embossed, and Aural)
, is now largely abandoned.
Logical operator
- Logical operator
not
.and
As well asonly
Can be used to compose a complex media query. You can also combine multiple media queries into a single rule by separating them with commas.
not
Used forMedia queriesTake the inverse, ifMedia queriesreturnfalse
, the returntrue
. If it appears in a comma-separated list of queries, it only reverses in the current range. If you are usingnot
Operator, you must also specify the media type.
/* Load on type screen */
@media screen {
.box {
background-color: red; }}/ * * /
@media not screen {
.box1 {
background-color: red; }}Copy the code
and
Used to combine multiple media features into oneMedia queriesIn the. It is also used to bringThe media functionwithThe media typeConnect.
/* Load at screen type greater than 960px */
@media screen and (min-width: 560px) {
.box {
background-color: red; }}/* Load at screen type > 960px < 1200px */
@media screen and (min-width: 560px) and (max-width: 700px) {
.box1 {
background-color: burlywood; }}Copy the code
only
To not add styles to browsers that do not support media queries. Browser processingonly
The leading keywords will be ignoredonly
.
@media only screen{
.box {
background-color: red; }} // the browser equivalent@media screen{
.box {
background-color: red; }}Copy the code
, (comma)
Commas are used to combine multiple media queries into a single rule. Each query in the comma-separated list is processed separately from the others. Therefore, if any query in the list is true, the entire media statement returns true. In other words, the list behaves like a logical OR operator.
/* Load on screen type less than 240px or more than 240px
@media screen and (min-width: 560px), (max-width: 240px) {
.box {
background-color: red; }}/* Load in screen type < 240px or > 360px < 700px */
@media screen and (max-width: 240px), (min-width: 360px) and (max-width: 700px) {
.box1 {
background-color: burlywood; }}Copy the code
The media function
- Determine current usage based on attributes
css
The device or the specific characteristics of the browsing environment. The expression is optional and is responsible for determining whether or not these features or characteristics exist and what their values are. Each media property expression must be enclosed in parentheses.
Commonly used media:
height
Output the height of the page visible area on the device.width
Output the width of the page visible area in the device.max-aspect-ratio
The maximum ratio of the screen width visible to the height of the output device.max-device-aspect-ratio
The maximum ratio of the screen width visible to the height of the output device.max-device-height
The maximum height at which the screen of the output device is visible.max-device-width
Maximum visible screen width of the output device.max-height
Output the maximum height of the visible area of the page on the device.max-width
Maximum visible area width of a page on an output device.min-height
The minimum height of the visible area of the page in the output device.min-width
The minimum visible area width of a page in an output device.
Other loading methods
style
On-label loading
<style media="(min-width: 500px)">
.box {
background-color: red;
}
</style>
<style media="(max-width: 500px">
.box {
background-color: burlywood;
}
</style>
Copy the code
- According to the
Media attribute
The definition of theMedia queriesDetermine which style to load.
@import is loaded when used
@import url(./index.css) (min-width:350px);
@import url(./home.css) (max-width:750px);
Copy the code
- Add at the end of the load
(a)
defineMedia queriesDetermine which style to load.
<picture>
The label
<picture>
<source media="(min-width: 650px)" srcset="demo1.jpg">
<source media="(min-width: 465px)" srcset="demo2.jpg">
<img src="img_girl.jpg">
</picture>
Copy the code
- Display different images according to the screen size, if not matched or the browser does not support
picture
Property uses theimg
Chemical element: