Starting To Write CSS in 2023 Will Be Different - by W3cplus - Medium-1
Starting To Write CSS in 2023 Will Be Different - by W3cplus - Medium-1
different
w3cplus · Follow
57 min read · Jan 5, 2024
189 4
CSS is evolving faster than ever. After Flexbox and Grid , CSS seems to have
gone through a long period of stagnation, but in recent years, CSS has added
many new features available , and there will be more new features coming
soon. This development speed is exciting, but also overwhelming.
Although CSS has added many new features , many web developers believe
that these fancy things have not brought substantial changes to themselves.
In other words, have all these fancy things (CSS new features) actually
changed the way you write CSS? For most web developers, the new features
of CSS have indeed influenced the way CSS is written today, but perhaps not
as thoroughly as I expected.
Although I have seen many blog posts as well as introductions and examples
of these trendy things in my own booklet “ Modern CSS “, I have not yet seen
these practical applications used in production or daily life. This is not a
complaint about anyone or anything. Personally, I am very excited about the
evolution of CSS. Many of the latest features are what we have been longing
for for years. Indeed, some of these features are gradually being
incorporated into my CSS. Although not a complete change, it is enough to
make me enjoy writing CSS more than ever.
From the 2023 Google I/O conference at the beginning of the year to the
WWDC23 conference at the end of the year, as well as @Bramus’ sharing at
the @Frontmania conference in Utrecht and the recent “ CSS Wrapped: 2023!
“ shared by Chrome team, they are all talking about the latest features of
CSS. I am no exception I spent half a year explaining modern features of CSS
in the form of a booklet Fortunately, the latest features of CSS introduced in
my booklet Basically (more than 95% of the features) have appeared in the
topics of these conferences.
It can be said that there are always dozens of new CSS features supported by
mainstream web platforms. It can be said that 2023 is a very important year
for CSS. These developments have made features that developers once
thought were impossible to achieve on web platforms a reality. Nowadays,
mainstream modern web browsers support CSS container queries ( size
queries , style queries and status queries , etc.), subgrids ( subgrid ),
relational selectors : has () (also often referred to as parent selectors),
complex n- * selectors, and a series of new color spaces and functions ,
such as color () and color-mix () . Chrome browser also supports scroll-
driven animations implemented with CSS only, as well as view transition
effects that smoothly transition between web views (if you are interested in
web animations, you can read " Web Animation Journey "). Most importantly,
many new basic features have emerged, such as CSS layers ( @layer ) ,
nesting and scopes , etc., which can better improve the experience of web
developers writing CSS, and even change the way web developers write,
maintain and manage CSS.
It’s been a wonderful year! Seeing so many new features, are you a little
hesitant? There are so many new things in CSS? It may even confuse you, is
this still the CSS you know?
Ask yourself how much you know and how much you have used. What’s
more important is whether these new features have really brought changes
to you!
Give up your hesitation
Under normal circumstances, people’s behavior towards new things varies
due to Individual Differences. Some people may show curiosity, excitement,
and positivity, while others may show fear and resistance. Similarly, when
web developers face new features of CSS, they may also show various
reactions and behaviors, which depend on their technical level, project
requirements, personal preferences, and acceptance of new technologies.
For example, some colleagues may be curious and excited about new
features of CSS (like me). For these colleagues, they will immediately take
action to learn and practice these new features, and even actively participate
in community building to make these new features more perfect. Of course,
there are also some colleagues who will show resistance, conservatism, or
even rejection. Moreover, in the Chinese community, this group of people
accounts for the majority because I often hear my friends around me
hesitate about new features. Can these fancy things be used? Is compatibility
good? And so on!
Here, what I want to say is that new features will continue to evolve and
improve over time. Just like when promoting CSS3 features, the most
frequently asked question was “Are new features compatible with IE?”
Version release
We can get new features and bug fixes introduced by various Web platforms
in the release of mainstream browsers such as Chrome , Safari and Firefox .
Release notes
Web platforms ( Chrome , Safari , and Firefox ) provide documentation with
detailed information about new features, improvements, and bug fixes each
time a new version is released. Web developers can learn about relevant
information by reading the release notes. Similarly, we can also obtain CSS-
related information in the corresponding documentation, including the
latest features, feature improvements, and bug fixes.
Compatibility data
The key factor for web development to stop new CSS features is cross-
platform compatibility. If you want to know the compatibility data of a new
feature, you can get it through platforms such as Can I Use , Browser Compat
Data and Time to Stable .
Interoperability
Interop is a cross-browser effort aimed at improving the interoperability of
the Web. Interoperability refers to the effort to improve the interoperability
of Web technologies in different browsers. Interoperability refers to
ensuring consistent behavior and effects when using the same Web
technology in different environments (especially different browsers). Simply
put, each technology achieves exactly the same state in all browsers.
Interop started in 2021, and modern browsers have all participated. So far,
there are Interop2021 , Interop2022 and Interop2023 :
From the above picture, you can see the efforts made by various web
platforms for CSS in 2023, which is why so many new CSS features will be
supported by mainstream web browsers in 2023.
Platform News
In web.dev blog , you will get the latest news about the Web platform every
month. This may include information about updates, improvements or
changes in Web technologies:
As shown in the above figure, it states the new features on the web platform
in December 2023.
Baseline
The benchmark was introduced at the 2023 Google I/O conference to clarify
whether web platform features are available for use. The original definition
of Baseline is that when current and previous versions of all major browsers
(Chrome, Edge, Firefox, and Safari) support these features, these features
are included in Baseline.
Baseline is coming to caniuse.com soon! This blog post will introduce this
integration and explore some of the features included in Baseline 2023.
According to the new definition of benchmark , the feature lifecycle is
divided into two stages. The first option is newly launched , and then fully
launched after 30 months. If a feature is interoperable in the following
browsers, it will become part of the new features provided by Baseline:
In the future, when you caniuse.com check compatibility data, you will see a
flag informing you whether the corresponding function is widely available in
Baseline. In other words, if you see this flag, you can use it boldly without
worrying about cross-browser compatibility issues. This also solves your
biggest doubt about CSS new features — compatibility:
Please note the emoji before the CSS feature title introduced next, which
represents the new Baseline badge! Click here for details:
💯 cascade layer
Cascading layers are the most important concept in CSS. Many web
developers fear them, one of the reasons being that the CSS they write can
easily cause conflicts or be overwritten. For example:
ul[class] { /* (0,1,1) */
margin: 0;
padding: 0;
list-style-type: none;
}
.nav { /* (0,1,0) */
margin: 0 40px; /* 被 ul[class] 中的 margin 覆盖 */
}
To determine which declaration (CSS style rule) will “win” (and be applied to
elements), cascading provides corresponding algorithms. Understanding
cascading algorithms helps us understand how browsers resolve style rule
conflicts, that is, browsers decide which style rule to apply to elements.
However, it should be noted that cascading algorithms have different
descriptions in different specifications, and six different levels are provided
in Level 5. Without considering cascading layers, their standards are as
follows:
Colleagues familiar with CSS should know that the priority of these
standards is arranged from high to low, and check them one by one until a
obtained declaration is determined. If it cannot be determined which
attribute declaration will win at the higher standard, the cascade will move
to the next standard. For example, as shown in the figure below.
To solve problems such as cascading and weights, CSS adds a cascading layer
rule, @layer .
With @layer , we can transform the code shown earlier like this:
With the CSS cascading layer @layer feature, you can abandon some of the
previous CSS methodologies (such as ITCSS), because @layer can better help
you manage CSS cascades.
Simply put, cascading layers @layer is a new feature of CSS that affects the
application and priority of style rules. Here are some key details about
cascading layers:
Anonymous Cascade Layer: If the @layer rule is not used, the style will
be placed into the default anonymous cascade layer. This ensures that
styles that do not specify a cascade layer will not affect the styles of the
specified cascade layer.
Load external CSS files to the cascade layer: You can load external CSS
files into the specified cascade layer through the @layer rule, which can
organize and load styles more flexibly.
Cascade-less styles: You can use the @layer unlayered rule to add styles to
the style layer of a cascade-less layer, and such styles are not inherited by
any cascade layer by default.
Cascade layer nesting: You can nest one cascade layer within another to
create a more complex style hierarchy.
Rollback Cascade Layer: You can use the @layer rule to roll back to the
previous cascade layer state to undo certain style changes.
Cascading layers provide developers with the ability to manage and organize
styles more finely, making it easier to maintain and extend styles in large
projects.
For a more detailed introduction to the CSS cascading layer @layer feature,
please read the CSS Layering: @layer course in Modern CSS !
💯 nesting
Usually, if web developers do not use CSS processors such as Sass (or SCSS),
LESS, and Stylus, each CSS selector needs to be explicitly declared and
separated from each other. For example:
table.colortable td {
text-align: center;
}
table.colortable td .c {
text-transform: uppercase;
}
table.colortable td:first-child,
table.colortable td:first-child + td {
border: 1px solid black;
}
table.colortable th {
text-align: center;
background: black;
color: white;
}
This will make the style code repetitive, redundant, and scattered. Therefore,
many web developers will use nested methods to write CSS selectors based
on the characteristics of CSS processors. For example, if the above code is
written using the nested syntax of SCSS, it will look like this:
table.colortable {
td {
text-align: center;
.c {
text-transform: uppercase;
}
&:first-child {
border: 1px solid black;
+ td {
border: 1px solid black;
}
}
}
th {
text-align: center;
background: black;
color: white;
}
}
Now, CSS also has nested features similar to SCSS, and relevant style rules
can be grouped into selectors to continue creating selectors.
table.colortable {
& td {
text-align: center;
.c {
text-transform: uppercase;
}
&:first-child,
&:first-child + td {
border: 1px solid black;
}
}
& th {
text-align: center;
background: black;
color: white;
}
}
Nesting can reduce the size of stylesheets, reduce the overhead of duplicate
selectors, and centralize component styles. There was a limitation when this
syntax was originally released: “ Any selector of CSS can be nested into
another selector, but it must start with & , . (class name), # (ID), @ ( @
table.colortable {
td {
text-align: center;
.c {
text-transform: uppercase;
}
&:first-child,
&:first-child + td {
border: 1px solid black;
}
}
th {
text-align: center;
background: black;
color: white;
}
}
h1 {
font-size: 2em;
For a more detailed introduction to CSS nesting features, please read the
course “ Nesting and Scope of CSS: & and @scope “ in “ Modern CSS “!
👁️ scope
One set of styles can override another set of styles based on their
proximity in the DOM
This means that the @scope rule lets you scope selectors to a specific subtree
of the document. With the scope style, you can select elements very
specifically without having to write overly specific selectors or tightly couple
them to the DOM structure.
/* 根作用域 */
@scope (.card) {
img {
border-color: green;
}
}
Scoped style rules img { … } can actually only select < img > elements
within the scope of the matched .card element. To prevent < img >
This range-bound style rule targets only < img > elements in the ancestor
tree between .card and .card__content elements. Such ranges with upper
and lower boundaries are often referred to as "donut ranges".
In the following example, the < img > element in the carousel component
does not match due to scope restrictions:
Demo address: https://codepen.io/web-dot-dev/pen/YzBLdjG
Within the CSS cascade , @scope also adds a new condition: determine
adjacent regions . This step comes after specificity but before the rendering
order.
By specification : “When comparing declarations that appear in style rules
with different range roots, the declaration with the fewest algebraic or
sibling element hops between the range root and the style rule subject that
qualifies the range wins”.
For a more detailed introduction to CSS nesting features, please read the
course “ Nesting and Scope in CSS: & and @scope “ in “ Modern CSS “, or see
“ How to Use @scope to restrict the coverage of selectors “ and @Miriam
Suzanne’s “ CSS @scope “.
selector weight count is equal to the value of the highest weight in the
selector list .
Simply put, :is() and :where() can help us better manage CSS selector
weights. The most effective way to use them is:
When using boxes or libraries, you can use :is() to increase the selector
weight, and without changing the HTML code, the selector weight can be
increased to the highest level
Given the difference between :is() and :where() , which selector to use
ultimately depends on your specific needs.
For a more detailed introduction to CSS nesting features, please read the
course “ CSS Selectors: : where () vs. .: is () “ in “ Modern CSS “!
💯 selector: has ()
CSS’s :has() selector is known as the parent selector of CSS! It, like the
container query feature of CSS, has always been the most wanted CSS
feature for web developers.
<!-- Case ①
-->
<figure>
<figcaption>CSS Pseudo-Class Specification</figcaption>
<img src="https://picsum.photos/1240/?random=11" alt="">
</figure>
<!-- Case ②
-->
<figure>
<div class="media">
<img src="https://picsum.photos/1240/?random=12" alt="">
</div>
<figcaption>CSS Pseudo-Class Specification</figcaption>
</figure>
<!-- Case ③
-->
<figure>
<img src="https://picsum.photos/1240/?random=13" alt="">
<figcaption>CSS Pseudo-Class Specification</figcaption>
</figure>
The DOM trees corresponding to these three cases are shown in the
following figure.
What you saw above is just the simplest use of :has() , which can help you
do more complex things, even some operations with interactive behavior.
For example, in the following example, using :has() selector and state
selector, you can implement a pure CSS-made rating component
(StarRating):
Demo address: https://codepen.io/airen/full/poxeoeE
For more information about the :has() selector, you can also refer to:
💯 complex n- * choice
New in CSS selector level 4 is the option to pass selector lists to :nth-child()
and :nth-last-child() .
By default, the :nth- *() pseudocode considers all child elements. Starting
from Chrome 111, you can optionally pass the selector list to :nth-child()
and :nth-last-child() . This allows you to pre-filter the list of child items
before An + B performs the operation.
In the demonstration below, the 3n + 1 logic is applied only to small dolls
by pre-filtering them using of.small. Use the drop-down menu to
dynamically change the selector used.
😍 : not(: has (+ .😍 )) .
😍 :not(:has (+ .😍 )) .
Now, with CSS’s trigonometry function, you can more easily arrange
elements on a circle centered around a point:
Demo address: https://codepen.io/web-dot-dev/full/poGVqLO
cos() function can be used to keep the size of the rotated element
unchanged;
💯 Subgrid
With CSS subgrid , you can create more complex grid layouts and achieve
better alignment between sublayouts.
It allows a grid within another grid to take the rows and columns of the outer
grid as its own by using subgrid as the values of the grid rows or columns.
Simply put, explicitly setting the value of display on a grid item to grid or
inline-grid , or inheriting the display value of its parent grid container,
means that the grid item is an independent grid formatting context. At the
same time, when the grid-template-columns and/or grid-template-rows of
the subgrid are explicitly set to subgrid , it means that the content of the
subgrid participates in the formatting context of its parent grid, without
establishing a new grid formatting context.
Subgrids in grid layouts are very useful and will give us more ways to achieve
functionality that was previously impossible with CSS grids. Subgrids are
great for aligning peers based on each other's dynamic content, and with
subgrids , the layout can be adjusted to fit the content.
If you are interested in the subgrid layout features, you can read Subgrids
and Nested Grids in Grid Layouts in Modern Web Layouts !
Typesetting
Web typesetting has always been very complex and involves a lot of
knowledge. In the just past year of 2023, web typesetting has made some
important updates. For example, you can use the text-wrap property to
effect of first letter sinking , and you can even use variable fonts and
colored fonts on the web to provide users with a better reading experience.
👁️ the first word sinking
The first character sinking first appeared in print media such as newspapers,
magazines, novels, textbooks, etc. It can add some fashion sense to the first
letter of a chapter or paragraph. These sinking letters can attract readers’
attention and can also use very fancy fonts because they only process one
letter in a string of text and do not affect the readability of the text.
The CSS internal connection layout module Level 3 (CSS Inline Layout
Module Level 3) provides the initial-letter property that allows you to
finely control the style of the first letter sinking. It can specify the size and
number of sinking initials, concave initials, and raised initials.
p::first-letter {
initial-letter: 3 2;
}
Demo address: https://codepen.io/web-dot-dev/full/XWOKGaL
initial-letter is a small but beautiful CSS feature that can be used to style
the placement of initials.
This requires us to adopt two new text wrapping techniques, one called
balance and the other called pretty . The balance value is designed to
create a harmonious text block, while pretty is designed to prevent isolated
characters and ensure healthy hyphenation. Traditionally, both tasks have
been done manually, and it's great to hand this work over to the browser and
have it support any translated language.
text-wrap: balance; There will be a limit on the number of lines, but this
depends on how many lines are under the text width. When using text-wrap:
balance; , because the browser has a limit on the number of lines for text
wrapping, it should only be used for headings and subheadings. Applying it
to large pieces of text will have no effect and will incur performance costs
because the browser is still trying to calculate the best balance even though
it will not apply anything.
When typesetting text, you need to choose between balance and aesthetics.
For headings and subheadings, use text-wrap: balance; . For text
paragraphs, use text-wrap: pretty; to eliminate isolated words in the last
line. These features are good candidates for progressive enhancement. If
someone is not in a supported browser, it will not have a negative impact on
the experience, but it will improve the visual balance of the page in a
supported browser.
For a more detailed introduction to the CSS text-wrap feature, please read
the course " Classic Typesetting Techniques: Using text-wrap: balance to
achieve text balance and line breaks " in " Modern CSS
Note that only two typesetting features, initial-letter and text-wrap , are
introduced here. In fact, there are other typesetting features that will be
supported by the Web platform in 2023. For example:
Color
2023 is the year of color for web platforms. With new color spaces and
functions, dynamic color themes can be realized. You can create rich and
bright themes, and customize them!
:
URL https://www.saji8k.com/displays/color-space/
The problem with color and the web is that CSS does not support high-
definition preparations . When CSS was introduced to the web in 1996, most
computer monitors were very bad and mostly not high-definition. Colors
defined using RGB, HSL, or HEX (hexadecimal) were within the sRGB color
gamut and only applicable to monitors at the time.
Nowadays, most new devices have wide color gamut display capabilities,
such as using the Display P3 color gamut , and even larger color gamut like
Rec.2020 . In 2023, we have new colors, more colors, new color spaces, color
functions, and new features.
Hexadecimal color
hwb ()
Color formats in modern CSS: RGB, HSL, HWB, LAB, and LCH
New CSS color space: set high definition colors for the web
💯 color-mix () function
Blending colors is a classic task, and in 2023, CSS can also do it. You can not
only mix white or black for colors, but also mix transparency, and perform
all these operations in any color space you choose. Compared to traditional
color mixing methods (such as simple average, linearly weighted average,
etc.), the color-mix() function provides more flexible mixing methods. In
addition, it can mix in different color spaces, which is more flexible when
adjusting colors, and can choose the appropriate color space for mixing as
needed.
You can think of color-mix() as a point in time in gradual change. In
gradual change, we can see all the sections from blue to white, and color-
mix() only shows one of the steps. Things get more complicated when you
start thinking about color space and learning how much the mixed color
space differs from the result.
URL: https://color-mix.style/
For a more detailed introduction to color-mix() , please read the course "
Blending Colors in CSS: color-mix () " in " Modern CSS "!
When you provide the starting color, you can access “channel keywords”,
which allow you to reference each channel in the color space. The keyword
depends on the color function you use. For rgb() , you will have r , g ,
and b channel keywords. For oklch() , you will have l , c , and h
keywords. For each color function, you also have a transparent channel
keyword that references the starting color's Alpha channel. For example:
:root {
--theme-primary: #8832CC;
}
.bg-primary-100 {
background-color: hsl(from var(--theme-primary) h s 90%);
}
.bg-primary-200 {
background-color: hsl(from var(--theme-primary) h s 80%);
}
.bg-primary-300 {
background-color: hsl(from var(--theme-primary) h s 70%);
}
.bg-primary-400 {
background-color: hsl(from var(--theme-primary) h s 60%);
}
For a more detailed introduction to Relative Color Syntax (RCS), please read
the Relative Color Syntax in the course Color Formats in Modern CSS: RGB,
HSL, HWB, LAB, and LCH in Modern CSS !
color-scheme media query is often used. To make things easier, CSS now
provides a useful function called light-dark() . This function accepts two
color values as arguments. Depending on the color scheme you are currently
using, it will output the first or second argument.
If the color scheme used is light or unknown, the function calculates the
value of the first color; if the color scheme used is dark, it calculates the
value of the second color.
The color scheme used is based not only on the user’s light or dark mode
settings, but also on the value of the color-scheme property. This is similar to
how System Colors are calculated.
Since color-scheme is taken into account, this also means that you can
override its value on each element to force it into a specific mode:
.dark {
color-scheme: dark; /* light-dark() 在此元素及其子元素上始终返回 dark */
}
Using the light-dark() function is much simpler and more convenient than
using prefers-color-scheme media queries.
Responsive design
@Una Kravets Presented at the 2021 Google I/O Conference Proposed a new
responsive design: Component-Driven Web Design . The web ecosystem is
about to enter a new era of responsive web design, changing our perception
of its meaning and bringing new changes to web design. Component-Driven
Web Design (or development) is also known as the next generation of
responsive web design .
In other words, we are once again witnessing the evolution of the responsive
design ecosystem, namely CSS’s new features will be directly based on
components rather than page-based style injection responsiveness This
capability is called Component-Driven Web Design , and component-driven
development will become a truly popular development model.
I also spent a class in “ Modern Web Layout “ to discuss this topic with you: “
Next Generation Responsive Web Design: Component-Driven Web Design “!
Container Query allows you to apply styles based on the size, computed style,
and state of the element container. Its biggest features are: Container Query
allows developers to define any element as containing context. The
descendant elements of the query container can change style based on the
Open in app Sign up Sign in
size of the query container or changes in computed style and state !
Search Write
In other words, a query container specifies its query type by using the
container type attribute ( container-type or container ). At the same time,
the style rules of the descendant elements of the query container can be set
independently by using the @container conditional group rules. Simply put,
Query containers (also known as CSS containment) provide a way to isolate
various parts of the page and declare to the browser that these parts are
independent of other parts of the page in terms of style and layout .
Container queries were originally limited to size queries, but over time, style
queries and status queries were added to container queries. In other words,
container queries include three types:
Size query : Adjust the style of its descendant elements according to the
size of the query container
Style Query : Adjust the style of its descendant elements based on the
query container style or CSS variables
State query : adjust the style of its descendant elements according to the
query capacity state
The size query supports querying the size of the parent element, instead of
using the global size information of the viewport (browser window) to apply
CSS styles. This means that you can set dynamic styles for components in
multiple layouts and views.
To use this feature, first set the element on the element you want to query,
and then apply the style using @container with size parameters, similar to
media queries. In addition to container queries, you can also get the
container query size. In the following demonstration, the container query
size cqi (representing the size of the embedded container) is used to adjust
the size of the card header.
Demo address: https://codepen.io/web-dot-dev/full/eYxBNQy
If you are interested in this feature, you can move on to read “ Next
Generation Responsive Web Design: Container Queries “ and “ CSS Container
Queries: Size Queries “. Note that the cqi mentioned above is the container
query unit, which is somewhat similar to the window unit. For more detailed
information on this, please refer to " Relative Units in Modern CSS ".
function accepts:
Note that the conditions of the style query (as declared in the style ()
Many times, style queries can also achieve the same results as size queries.
That is, you can query the value of a custom property on the parent element
when using @container style() . For example, query whether a custom
property value exists or is set to a specific value, such as @container style(--
rain: true) :
Although this sounds similar to using class names in CSS, style queries have
some advantages. Firstly, for style queries, you can update the values in CSS
according to the needs of pseudo-state. In addition, in future
implementations, you will be able to query the range of values (such as
style(60 < = --weather < = 70) ) and determine the applied style based on
attribute value pairs (such as style(font-style: italic) ).
For a more detailed introduction to style queries, please read the course “
CSS Container Queries: Style Queries and Status Queries “ in “ Modern CSS “!
In fact, CSS state queries and CSS style queries are somewhat similar. We
mainly use the state() function to distinguish between size queries and
style queries in CSS container queries. For example:
Most of the devices you design may have a fast refresh rate, including
desktop devices and most mobile devices. The refresh rate of devices such as
e-readers and low-energy payment systems may be slow. Knowing that the
device cannot handle animations or frequent updates means you can save
battery life or reduce erroneous view updates.
When making animations, this media query is very useful if you want to
reduce motion for users with slow networks. In addition, it is often used with
the prefers-reduced-motion: reduce media query to provide different
animation effects for users who have reduced motion preferences:
@media (prefers-reduced-motion: reduce), (update: slow) {
*,
::before,
::after {
animation-delay: -1ms !important;
animation-duration: 1ms !important;
animation-iteration-count: 1 !important;
background-attachment: initial !important;
scroll-behavior: auto !important;
transition-duration: 0s !important;
transition-delay: 0s !important;
}
}
If you are interested in this area, especially how to make accessible web
animations, you can move to read “ Web Animation Journey “ “ Key Tips for
Improving Accessibility Animation “!
.steam {
transition: opacity .2s ease;
}
@media (scripting: none) {
.steam {
opacity: 0;
}
}
@media (scripting: enabled) {
.steam {
opacity: 1;
}
}
In some cases, you can provide an alternate layout that does not overlay
content on top of other content. In other cases, you can adjust the opacity of
the color to opaque or almost opaque. @Adam Argyle’s blog post provides
more inspiring demos that can be adjusted according to user preferences .
) are called query ranges. Just using min- and max- is often confusing, at
least for myself: "Using min-width and max-width is often confusing."
Therefore, I always like to use the following figure to make judgments:
But the Media Queries Level 4 specification introduces a new syntax that
uses common mathematical comparison operators such as < , > and = to
determine the range of window widths, which makes more sense
syntactically while writing less code and making it easier to understand the
code.
/* 老方式的查询范围语法 */
@media (min-width: 375px) {
/* 视窗宽度大于或等于 375px */
}
@media (max-width: 768px) {
/* 视窗宽度小于或等于 768px */
}
@media (min-width: 375px) and (max-width: 768px) {
/* 视窗宽度在 之间
375px ~ 768px */
}
/* 新式的查询范围语法 */
@media (width >= 375px) {
/* 视窗宽度大于或等于 375px */
}
@media (width <= 768px) {
/* 视窗宽度小于或等于 768px */
}
@media (375px <= width <= 768px) {
/* 视窗宽度在 375px ~ 768px 之间 */
}
The biggest change in the Media Queries Level 4 specification is that we have
a new operator for comparing values instead of combining values.
New Grammar Apart from the visual differences in grammar, they work
slightly differently. Using min- and max- is equivalent to using
mathematical comparison operators:
Max-width is equivalent to the < = operator, for example, (max-width:
For a more detailed introduction to CSS media queries, please read “ Modern
CSS “ in “ CSS Media Query New Features: @media “!
Interactive animation
👁️ view transition
View transitions can have a huge impact on the User Experience of the Web.
With the CSS View Transitions API, you can create visual transitions between
two page states in a Single Page Application. These transitions can be full-
page transitions or smaller transitions on a webpage, such as adding or
removing new items from a list.
function. Pass in the function that updates the DOM to the new state, and
this API takes care of all the work for you. To do this, it takes before and after
snapshots and then transitions between the two. With CSS, you can control
what is captured and customize how these snapshots are animated as
needed.
:root {
view-transition-name: none;
}
::view-transition-group(*) {
animation-duration: 0.5s;
}
For a more detailed introduction to view transitions, please read “ Using CSS
View Transitions to Create Smooth Interface Motion Graphics “ in “ Web
Animation Journey “; or see “ Modern CSS “ in “ Unlocking the Magic of CSS
View Transitions API “!
Using ScrollTimeline , you can track the overall progress of the scroller, as
shown in the following demonstration. When you scroll to the end of the
page, the text will be displayed character by character.
Demo address: https://codepen.io/web-dot-dev/full/JjxvwqG
Nowadays, we only need to use pure CSS to achieve it. By combining CSS
scroll capture , custom properties , and scroll-driven motion graphics, we
can achieve a parallax scrolling animation effect.
The above example also has a responsive function. When you reduce the
browser screen to less than 768px , the page can also scroll the whole
screen, and the parallax effect is more obvious:
For a more detailed introduction to CSS scroll-driven motion graphics,
please read “The Art of CSS Scroll-Driven Motion Graphics “ in “ Web
Animation Journey “; or see “ CSS Scroll-Driven Motion Graphics “ in “
Modern CSS “!
@layer transition {
.card {
transition: opacity 0.25s, display 0.25s;
transition-behavior: allow-discrete;
}
.fade-out {
opacity: 0;
display: none;
}
}
👁️ @starting-style
The above picture is from @XboxYan
@Starting-style is a new CSS @ rule that uses new web features to animate
the entry and exit of elements with display: none . This rule provides a way
for browsers to look for "before-open" styles before the element is opened on
the page. This is useful for intro animations and for animating elements
such as pop-ups or dialog boxes. It can also be used when creating an
element and wanting to animate it. The following example animates the
popover property to smoothly enter the view from outside the viewport and
into the top layer.
/* IS-OPEN STATE */
dialog[open] {
translate: 0 0;
}
/* EXIT STATE */
dialog {
transition:
translate 0.7s ease-out,
overlay 0.7s ease-out,
display 0.7s ease-out allow-discrete;
translate: 0 100vh;
}
/* 0. BEFORE-OPEN STATE */
@starting-style {
dialog[open] {
translate: 0 100vh;
}
}
Demo address: https://codepen.io/web-dot-dev/full/OJdjjdX
👁️ overlay
New CSS overlay properties can be added to your transitions to allow
elements with top-level styles (such as popover and dialog ) to smoothly
move out of the top-level animation. If you do not use overlay transitions,
your element will immediately return to the state of being cropped,
transformed, and overwritten, and you will not see the transition occur.
Similarly, when you add an overlay to a top-level element, :: backdrop can
also be smoothly animated out.
👁️ Anchor Positioning
CSS Anchor Positioning (CSS Anchor Positioning) is a new method for
locating elements on a web page. It is a new feature of CSS. The W3C
specification describes it as follows : “Through anchor positioning (through
the anchor positioning function anchor() and anchor-size() ), an absolutely
positioned element can be anchored with one or more other elements on the
page, while also allowing them to try multiple possible positions to find the
best position to avoid overlap and overflow".
Simply put, CSS anchor positioning improves the ability of element absolute
positioning. Web developers can use a simpler and more natural way to
locate the relationship between elements, allowing page elements to be
positioned and adjusted according to the position and size of other elements
within their containing blocks. The emergence of this feature provides web
developers with more control and flexibility, while reducing dependence on
JavaScript, making page performance more optimized.
In the past, to achieve the effect of the lava lamp menu, we had to rely on
JavaScript scripts. Now, we can use CSS anchor positioning to achieve it. For
example:
If you want to learn more about CSS anchor positioning, read CSS Anchor
Positioning: Exploring the Next Generation of Web Layouts in Modern CSS !
👁️ animation compositing
The W3C specification describes CSS compositing animations as follows :
Don’t let the name of this function mislead you. linear() function (not to be
confused with the linear keyword) allows you to create complex easing
functions in a simple way, at the expense of some precision.
:root {
--linear: linear(0, 1);
--quad-in: linear( 0, 0.0039, 0.0156, 0.0352, 0.0625, 0.0977, 0.1407, 0.1914,
--quad-out: linear( 0, 0.2342, 0.4374, 0.6093 37.49%, 0.6835, 0.7499, 0.8086,
--quad-in-out: linear( 0, 0.0027, 0.0106 7.29%, 0.0425, 0.0957, 0.1701 29.16%,
--power-1-in: linear( 0, 0.0039, 0.0156, 0.0352, 0.0625, 0.0977, 0.1407, 0.191
--power-1-out: linear( 0, 0.2342, 0.4374, 0.6093 37.49%, 0.6835, 0.7499, 0.808
--power-1-in-out: linear( 0, 0.0027, 0.0106 7.29%, 0.0425, 0.0957, 0.1701 29.1
--cubic-in: linear( 0, 0.0014 11.11%, 0.0071 19.24%, 0.0188 26.6%, 0.037 33.33
--cubic-out: linear( 0, 0.2688 9.91%, 0.3859 15%, 0.4917 20.19%, 0.5865 25.5%,
--cubic-in-out: linear( 0, 0.0036 9.62%, 0.0185 16.66%, 0.0489 23.03%, 0.0962
--power-2-in: linear( 0, 0.0014 11.11%, 0.0071 19.24%, 0.0188 26.6%, 0.037 33.
--power-2-out: linear( 0, 0.2688 9.91%, 0.3859 15%, 0.4917 20.19%, 0.5865 25.5
--power-2-in-out: linear( 0, 0.0036 9.62%, 0.0185 16.66%, 0.0489 23.03%, 0.096
--quart-in: linear( 0, 0.0039 25%, 0.0117 32.89%, 0.0248 39.68%, 0.0457 46.22%
--quart-out: linear( 0, 0.1538 4.09%, 0.2926 8.29%, 0.4173 12.63%, 0.5282 17.1
--quart-in-out: linear( 0, 0.0029 13.8%, 0.0184 21.9%, 0.0339 25.51%, 0.0551 2
--power-3-in: linear( 0, 0.0039 25%, 0.0117 32.89%, 0.0248 39.68%, 0.0457 46.2
--power-3-out: linear( 0, 0.1538 4.09%, 0.2926 8.29%, 0.4173 12.63%, 0.5282 17
--power-3-in-out: linear( 0, 0.0029 13.8%, 0.0184 21.9%, 0.0339 25.51%, 0.0551
--quint-in: linear( 0, 0.0024 29.91%, 0.008 38.03%, 0.0179 44.72%, 0.035 51.16
--quint-out: linear( 0, 0.1641 3.52%, 0.311 7.18%, 0.4413 10.99%, 0.5553 14.96
--quint-in-out: linear( 0, 0.0012 14.95%, 0.0089 22.36%, 0.0297 28.43%, 0.0668
--power-4-in: linear( 0, 0.0024 29.91%, 0.008 38.03%, 0.0179 44.72%, 0.035 51.
--power-4-out: linear( 0, 0.1641 3.52%, 0.311 7.18%, 0.4413 10.99%, 0.5553 14.
--power-4-in-out: linear( 0, 0.0012 14.95%, 0.0089 22.36%, 0.0297 28.43%, 0.06
--strong-in: linear( 0, 0.0024 29.91%, 0.008 38.03%, 0.0179 44.72%, 0.035 51.1
--strong-out: linear( 0, 0.1641 3.52%, 0.311 7.18%, 0.4413 10.99%, 0.5553 14.9
--strong-in-out: linear( 0, 0.0012 14.95%, 0.0089 22.36%, 0.0297 28.43%, 0.066
--elastic-in: linear( 0, 0.0019 13.34%, -0.0056 27.76%, -0.0012 31.86%, 0.0147
--elastic-out: linear( 0, 0.2178 2.1%, 1.1144 8.49%, 1.2959 10.7%, 1.3463 11.8
--elastic-in-out: linear( 0, 0.0009 8.51%, -0.0047 19.22%, 0.0016 22.39%, 0.02
--bounce-in: linear( 0, 0.0117, 0.0156, 0.0117, 0, 0.0273, 0.0468, 0.0586, 0.0
--bounce-out: linear( 0, 0.0039, 0.0157, 0.0352, 0.0625 9.09%, 0.1407, 0.25, 0
--bounce-in-out: linear( 0, 0.0078, 0, 0.0235, 0.0313, 0.0235, 0.0001 13.63%,
--expo-in: linear( 0, 0.0085 31.26%, 0.0167 40.94%, 0.0289 48.86%, 0.0471 55.9
--expo-out: linear( 0, 0.1684 2.66%, 0.3165 5.49%, 0.446 8.52%, 0.5581 11.78%,
--expo-in-out: linear( 0, 0.0053 17.18%, 0.0195 26.59%, 0.0326 30.31%, 0.0506
--circ-in: linear( -0, 0.0048 9.8%, 0.0192 19.5%, 0.043 29.02%, 0.0761 38.26%,
--circ-out: linear( 0, 0.0979 0.48%, 0.195 1.92%, 0.2901 4.3%, 0.3827 7.61%, 0
--circ-in-out: linear( -0, 0.0033 5.75%, 0.0132 11.43%, 0.0296 16.95%, 0.0522
--sine-in: linear( 0, 0.0035, 0.0141 10.7%, 0.0318 16.09%, 0.0566 21.51%, 0.08
--sine-out: linear( 0, 0.2861 18.47%, 0.4829 32.08%, 0.6437 44.52%, 0.7712 56.
--sine-in-out: linear( 0, 0.007 5.35%, 0.0282 10.75%, 0.0638 16.26%, 0.1144 21
--back-in: linear( 0, -0.0029 4.31%, -0.0119 9.02%, -0.0838 31.27%, -0.0956 36
--back-out: linear( 0, 0.1935 4.37%, 0.3671 8.83%, 0.521 13.38%, 0.6557 18.04%
--back-in-out: linear( 0, -0.0077 5.2%, -0.0452 16.98%, -0.0493 22.35%, -0.041
--spring-1: linear(0, 0.006, 0.025 2.8%, 0.101 6.1%, 0.539 18.9%, 0.721 25.3%,
--spring-2: linear(0, 0.007, 0.029 2.2%, 0.118 4.7%, 0.625 14.4%, 0.826 19%, 0
--spring-3: linear(0, 0.009, 0.035 2.1%, 0.141 4.4%, 0.723 12.9%, 0.938 16.7%,
--spring-4: linear(0, 0.009, 0.037 1.7%, 0.153 3.6%, 0.776 10.3%, 1.001, 1.142
--spring-5: linear(0, 0.01, 0.04 1.6%, 0.161 3.3%, 0.816 9.4%, 1.046, 1.189 14
--bounce-1: linear(0, 0.004, 0.016, 0.035, 0.063, 0.098, 0.141, 0.191, 0.25, 0
--bounce-2: linear(0, 0.004, 0.016, 0.035, 0.063, 0.098, 0.141 15.1%, 0.25, 0.
--bounce-3: linear(0, 0.004, 0.016, 0.035, 0.062, 0.098, 0.141 11.4%, 0.25, 0.
--bounce-4: linear(0, 0.004, 0.016 3%, 0.062, 0.141, 0.25, 0.391, 0.562 18.2%,
--bounce-5: linear(0, 0.004, 0.016 2.5%, 0.063, 0.141, 0.25 10.1%, 0.562, 1 20
}
These easing curves will make your animation more dynamic, weighty, etc.
They bring the animation closer to the movement of objects in real life.
Demo address: https://codepen.io/web-dot-dev/full/bGzMOJM
For a more detailed introduction to the linear() function, see Using Linear
() Function to Create Stunning Animated Curves in Web Animation Journey !
👁️ Scrollend incident
Many interfaces include scrolling interactions, and sometimes interfaces
need to synchronize information related to the current scrolling position or
extract data based on the current state. Before the scrollend event, you have
to use an inaccurate timeout method that can be triggered when the user's
finger is still on the screen. With the scrollend event, you can implement
precisely timed scrollend events to see if the user is still performing gesture
operations.
Demo address: https://codepen.io/web-dot-dev/full/OJdxKqJ
💯 Scroll Capture
Actually, scroll capture is not a feature that will just be supported in 2023, but
I still included this feature in this article. One of the main reasons is that it
can really help us improve the user experience. It is a very good and easy-to-
use feature.
CSS Scroll Capture gives us a way to, scroll a document to a specific point
(location) as the user scrolls through it . This is helpful for creating a more
application-like (native Client) experience for certain types of applications
(such as <Carousel> pieces) on mobile devices, and even on desktops. In
short, CSS Scroll Capture can:
CSS scroll capture related properties are similar to CSS Flexbox and Grid
properties, divided into properties that act on containers (scroll containers)
and properties that act on positioning sub-items (scroll container sub-
elements) . The properties that act on scroll containers mainly include
scroll-snap-type , scroll-padding and scroll-snap-stop ; the properties
that act on positioning sub-items mainly include scroll-margin and scroll-
snap-align .
Now, you can combine scroll capture with scroll-driven animation and
custom scroll bars to achieve a 3D cover effect like the one shown below
without relying on any JavaScript.
What you need to know is that besides scroll capture , there are other ways
to improve the user’s scrolling experience. For more details, please refer to “
How CSS Improves Scrolling Experience “ in “ Defensive CSS Insights “, “
Beautiful Scrolbar UI: Custom Scrolbar UI “ and “ CSS Scrolling Capture “!
CSS in 2022
CSS In 2023
If you are also interested in CSS, you can also follow my brochure.
“ Modern Web Layout “: focuses on modern CSS features that can be used
for web layout
“ Modern CSS “: mainly talks about the latest features in the CSS field, the
features introduced earlier, basically can be found in this booklet
361 Followers
Author of "Modern CSS," "Modern Web Layout," "In-Depth CSS Defense," and "A Journey
into Web Animation!"
w3cplus w3cplus
Mar 11 31 Jun 3 1
Lists
4d ago 3 Oct 15 6
Oct 27 52 Jul 30 8