Home>Article>Web Front-end> Several new CSS features you deserve to know in 2022 (collect to learn)
Several new CSS features you deserve to know in 2022 (collect to learn)
青灯夜游forward
2022-03-17 10:50:293478browse
This article will share with you someCSSnew features that are worth looking forward to in 2022 and shouldn’t be missed. Let’s collect and learn together!
For CSS, 2022 is a year worth looking forward to. A large number of new features are about to appear, some have already begun to log into browsers, and some may be launched in 2022 Get broad browser support. Let’s take a look at the new CSS features worth looking forward to in 2022! (Recommended learning:css video tutorial)
1. Container query
1. Basic concepts
Container query enables us Able to style an element based on the size of its parent, it's similar to a @media query, except it bases it on the size of the container rather than the size of the viewport. This has always been an issue with responsive design, as we sometimes want a component to adapt to its context.
2. Usage method
For container query, you need to use the container attribute (which is the abbreviation of container-type and container-name) to specify an element as a container. container-type can be width, height, inline-size, block-size. inline-size and block-size are logical properties that may produce different results depending on the document's writing mode.
main, aside { container: inline-size; }
@container can be used in a similar way to media queries. Note that there is a difference in the way the rules are expressed in parentheses (inline-size > 30em should be used instead of min-width: 30em in container queries). This is part of the Media Inquiry Level 4 specification. Switch to flex layout when container width is larger than 30rem:
:has()is often called the "parent selector". This pseudo-class allows us to select Select an element whose descendants are selected. It has some very interesting use cases. For example, you can style an image differently in a614eb9dc63b3fb809437a716aa228d24depending on whether it contains614eb9dc63b3fb809437a716aa228d24.
2. Usage method
To set the style of the2f8332c8dcfd5c7dec030a070bf652c3element containingc1a436a314ed609750bd7c7d319db4da, You can do the following:
section:has(h2) { background: lightgray; }
Set whena1f02c36ba31691bcfe87b2722de723b's parent2f8332c8dcfd5c7dec030a070bf652c3containsc1a436a314ed609750bd7c7d319db4daa1f02c36ba31691bcfe87b2722de723bStyle:
section:has(h2) img { border: 5px solid lime; }
3 Current status
Currently no mainstream browser supports this attribute, but it can be used in Safari technology Use it in preview version.
@ when/@else are conditional rules in CSS, similar to if/else logic in other programming languages. It can make writing complex media queries more logical. I chose to use @when instead of @if here to avoid conflicts with Sass.
2. Usage method
You can query multiple media conditions or supported functions, such as whether the user's viewport exceeds a certain width and whether the user's browser supports subgrid.
Currently changing attributes is not supported in browsers. It's early days and still under discussion. It's not expected to be widely supported by browsers this year, but it's certainly a very useful property worth paying attention to.
The color-scheme attribute allows an element to indicate the color scheme it can easily render. Common choices for operating system color schemes are "light" and "dark", or "day mode" and "night mode." When the user selects one of the color schemes, the operating system adjusts the user interface. This includes usage values for form controls, scroll bars, and CSS system colors.
2. Usage method
The usage is very simple, and this property is inheritable. So it can be set at the root level to apply it everywhere:
:root { accent-color: lime; }
可以在单个元素上使用:
form { accent-color: lime; } input[type="checkbox"] { accent-color: hotpink; }
A colourful mix of blue, yellow, white and green makes the blue tit one of our most attractive and most recognisable garden visitors.Robins sing nearly all year round and despite their cute appearance, they are aggressively territorial and are quick to drive away intruders.The chaffinch is one of the most widespread and abundant bird in Britian and Ireland.
The above is the detailed content of Several new CSS features you deserve to know in 2022 (collect to learn). For more information, please follow other related articles on the PHP Chinese website!