7

6 CSS snippets every front-end developer should know in 2023

 1 year ago
source link: https://web.dev/6-css-snippets-every-front-end-developer-should-know-in-2023/?ref=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

6 CSS snippets every front-end developer should know in 2023

web.dev uses cookies to deliver and enhance the quality of its services and to analyze traffic. If you agree, cookies are also used to serve advertising and to personalize the content and advertisements that you see. Learn more.

Skip to content About Blog Learn Explore Patterns Case studies

We want to hear from you! We are looking for web developers to participate in user research, product testing, discussion groups and more. Apply now to join our WebDev Insights Community.
A wooden table with a hammer and a wrench neatly placed on it.

6 CSS snippets every front-end developer should know in 2023

Toolbelt worthy, powerful, and stable CSS you can use today.

Mar 15, 2023

I believe every front-end developer should know how to use container queries, create a scroll snap experience, avoid position: absolute with grid, swiftly hammer out a circle, use cascade layers, and reach more with less via logical properties. Here's a quick overview of each of those expectations.

1. A container query #

The top requested CSS feature for 10 years straight, is now stable across browsers and available for you to use for width queries in 2023.

.panel {
container: layers-panel / inline-size;
}

.card {
padding: 1rem;
}

@container layers-panel (min-width: 20rem) {
.card {
padding: 2rem;
}
}
@container
Browser support
  • chrome 105, Supported 105
  • firefox 110, Supported 110
  • edge 105, Supported 105
  • safari 16, Supported 16
Source
container
Browser support
  • chrome 105, Supported 105
  • firefox 110, Supported 110
  • edge 105, Supported 105
  • safari 16, Supported 16
Source

2. Scroll snap #

Well orchestrated scroll experiences set your experience apart from the rest, and scroll snap is the perfect way to match system scroll UX while providing meaningful stopping points.

.snaps {
overflow-x: scroll;
scroll-snap-type: x mandatory;
overscroll-behavior-x: contain;
}

.snap-target {
scroll-snap-align: center;
}

.snap-force-stop {
scroll-snap-stop: always;
}

Learn more about the potential of this CSS feature in this huge and inspiring Codepen collection of around 25 demos.

scroll-snap-type
Browser support
  • chrome 69, Supported 69
  • firefox 99, Supported 99
  • edge 79, Supported 79
  • safari 11, Supported 11
Source
scroll-snap-align
Browser support
  • chrome 69, Supported 69
  • firefox 68, Supported 68
  • edge 79, Supported 79
  • safari 11, Supported 11
Source
scroll-snap-stop
Browser support
  • chrome 75, Supported 75
  • firefox 103, Supported 103
  • edge 79, Supported 79
  • safari 15, Supported 15
Source
overscroll-behavior
Browser support
  • chrome 63, Supported 63
  • firefox 59, Supported 59
  • edge 18, Supported 18
  • safari 16, Supported 16
Source

3. Grid pile #

Avoid position absolute with a single cell CSS grid. Once they're piled on top of each other, use justify and align properties to position them.

.pile {
display: grid;
place-content: center;
}

.pile > * {
grid-area: 1/1;
}
grid
Browser support
  • chrome 57, Supported 57
  • firefox 52, Supported 52
  • edge 16, Supported 16
  • safari 10.1, Supported 10.1
Source

4. Quick circle #

There are lots of ways to make circles in CSS, but this is definitely the most minimal.

.circle {
inline-size: 25ch;
aspect-ratio: 1;
border-radius: 50%;
}
aspect-ratio
Browser support
  • chrome 88, Supported 88
  • firefox 89, Supported 89
  • edge 88, Supported 88
  • safari 15, Supported 15
Source

5. Control variants with @layer #

Cascade layers can help insert variants discovered or created later, into the right place in the cascade with the original set of variants.

/* file buttons.css */
@layer components.buttons {
.btn.primary {

}
}

Then, in some entirely different file, loaded at some other random time, append a new variant to the button layer as if it was there with the rest of them this whole time.

/* file video-player.css */
@layer components.buttons {
.btn.player-icon {

}
}
@layer
Browser support
  • chrome 99, Supported 99
  • firefox 97, Supported 97
  • edge 99, Supported 99
  • safari 15.4, Supported 15.4
Source

6. Memorize less and reach more with logical properties #

Memorize this one new box model and never have to worry about changing left and right padding or margin for international writing modes and document directions again. Adjust your styles from physical properties to logical ones like padding-inline, margin-inline, inset-inline, and now the browser will do the adjusting work.

button {
padding-inline: 2ch;
padding-block: 1ch;
}

article > p {
text-align: start;
margin-block: 2ch;
}

.something::before {
inset-inline: auto 0;
}
padding-inline
Browser support
  • chrome 87, Supported 87
  • firefox 66, Supported 66
  • edge 87, Supported 87
  • safari 14.1, Supported 14.1
Source
margin-block
Browser support
  • chrome 87, Supported 87
  • firefox 66, Supported 66
  • edge 87, Supported 87
  • safari 14.1, Supported 14.1
Source
inset-inline
Browser support
  • chrome 87, Supported 87
  • firefox 63, Supported 63
  • edge 87, Supported 87
  • safari 14.1, Supported 14.1
Source
CSS
Last updated: Mar 15, 2023 — Improve article

We want to help you build beautiful, accessible, fast, and secure websites that work cross-browser, and for all of your users. This site is our home for content to help you on that journey, written by members of the Chrome team, and external experts.

Dark theme
Choose language

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies.

By Chrome DevRel


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK