
Advanced CSS Cheat Sheet
11/05/2024
Sass Tutorial: SCSS, Mixins, Modules, and Exercises
12/05/2024
This guide introduces ten CSS techniques for layout, color, typography, and scrolling. Check browser support for the features you plan to use.
Modern CSS techniques
Each section includes syntax, a runnable example, and a link to the full lesson.
1. CSS blend modes: combine colors from overlapping layers
Blend modes combine the colors of an element with the colors behind it. Use them with images, gradients, or solid colors to create effects such as lightening and contrast.
Syntax:
Set mix-blend-mode to a mode such as multiply, screen, or overlay.
.element {
mix-blend-mode: <blend-mode>;
}
Common values:
| Blend Mode | Description | Characteristics |
|---|---|---|
| multiply | Multiplies the top and bottom layer colors. | – Darkens the result. |
| screen | Inverts both color inputs, multiplies them, and inverts the result. | – Lightens the result. |
| overlay | Combines multiply and screen according to the backdrop color. | – Increases contrast in many cases. |
| darken | Keeps the darker value of each color channel. | – Compares the two layers channel by channel. |
| lighten | Keeps the lighter value of each color channel. | – Compares the two layers channel by channel. |
| color-dodge | Brightens the backdrop based on the source color. | – Lightens portions of the backdrop. |
| color-burn | Darkens the backdrop based on the source color. | – Darkens portions of the backdrop. |
| difference | Takes the absolute difference between the two colors in each channel. | – White inverts the backdrop, while black leaves it unchanged. |
| exclusion | Similar to difference, with lower contrast. | – Produces a softer difference effect. |
| hard-light | Combines multiply and screen based on the source color. | – Can produce strong contrast. |
| soft-light | Similar to hard light, with a gentler effect. | – Produces a softer change in contrast. |
| hue | Keeps the backdrop’s luminosity and saturation and uses the source hue. | – Applies the source hue to the backdrop. |
| saturation | Keeps the backdrop’s luminosity and hue and uses the source saturation. | – Applies the source saturation to the backdrop. |
| color | Keeps the backdrop’s luminosity and uses the source hue and saturation. | – Applies the source hue and saturation to the backdrop. |
| luminosity | Keeps the backdrop’s hue and saturation and uses the source luminosity. | – Applies the source luminosity to the backdrop. |
Example:
The demo uses overlay to blend text with the image behind it. Check legibility against every part of the background the text covers.
CSS:
.text {
mix-blend-mode: overlay;
}
Try it in CodePen:
2. CSS filters: apply effects to boxes or images
The filter property changes the rendered content of an element. It can blur it, adjust its brightness or contrast, or convert it to grayscale.
Filter functions accept the value appropriate to the effect. Separate multiple functions with spaces to apply them in sequence.
Syntax:
selector {
filter: <filter-function>(<value>) ;
}
Common filter functions:
| Function | Description | Values |
|---|---|---|
blur( | Blurs the element | <value> is the blur length |
brightness( | Adjusts the brightness | <value>: 0 is black, 1 is unchanged |
contrast( | Adjusts the contrast | <value>: 0 is gray, 1 is unchanged |
grayscale( | Converts the element to grayscale | <value>: 0 is unchanged, 1 is fully grayscale |
hue-rotate( | Rotates the hue | <value> is an angle, such as 90deg |
invert( | Inverts the colors | <value>: 0 is unchanged, 1 is fully inverted |
opacity( | Adjusts opacity | <value>: 0 is transparent, 1 is unchanged |
saturate( | Adjusts saturation | <value>: 0 is grayscale, 100% is unchanged |
sepia( | Gives the element a sepia tone | <value>: 0 is unchanged, 1 is fully sepia |
Filters and blend modes: A filter changes the rendered element itself. Blend modes determine how it combines with layers behind it.
Example:
The demo applies the filters in the table to a red box.
Try it in CodePen:
3. CSS Scroll Snap: align content at scroll positions
Scroll Snap lets a container settle on defined positions after scrolling, using CSS alone.
Use it to align horizontal cards or vertical sections. Typical examples include product galleries and image carousels.
Syntax:
Set scroll-snap-type on the scroll container and scroll-snap-align on the items that define the positions.
.scroll-container {
scroll-snap-type: [x | y | block | inline] [mandatory | proximity];
}
.section {
scroll-snap-align: [start | center | end | none];
}
- Choose an axis such as
x,y,block, orinline. Usemandatoryfor required snapping orproximityto snap when close to a position. scroll-snap-aligncan align the item’sstart,center, orend, or disable snapping for that item withnone.
Example:
Use scroll-snap-type: y mandatory on the vertical container. Give each section scroll-snap-align: start so it aligns with the top of the scroll area.
HTML:
<div class="snap-container">
<div class="snap-section">Section 1</div>
<div class="snap-section">Section 2</div>
<div class="snap-section">Section 3</div>
<div class="snap-section">Section 4</div>
</div>
CSS:
.scroll-container {
height: 300px; /* Container height */
overflow-y: scroll; /* Enable vertical scrolling */
scroll-snap-type: y mandatory; /* Require vertical snapping */
}
.section {
height: 300px; /* Height of each section */
scroll-snap-align: start; /* Align each section at the start */
}
Try it in CodePen:
4. CSS scroll-behavior: animate navigation to an anchor
Use scroll-behavior to request a smooth scroll when a visitor follows a fragment link.
CSS can provide this effect without a JavaScript library. The anchor link still works without the animation.
Syntax:
Set the property on html to affect the viewport. It applies to navigation and programmatic scrolling.
html{
scroll-behavior: auto | smooth;
}
- auto: Requests instant scrolling.
- smooth: Requests an animated scroll. Honor reduced-motion preferences when enabling it.
Example:
A landing page can use a navigation menu with links to different sections on the same page.
HTML:
<nav>
<ul>
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
<li><a href="#section4">Section 4</a></li>
</ul>
</nav>
<section id="section1">Section 1</section>
<section id="section2">Section 2</section>
<section id="section3">Section 3</section>
<section id="section4">Section 4</section>
CSS:
@media (prefers-reduced-motion: no-preference) {
html { scroll-behavior: smooth; }
}
Try it in CodePen:
Read the scroll-behavior lesson →
5. CSS position: sticky: keep headings in view while scrolling
A sticky box participates in normal flow. Once it reaches a specified inset, its position is constrained by its scroll context and containing block.
For example, set top: 0 on a sticky heading to keep it near the top of the scroll area while its section remains in view.
Sticky positioning also works for some sidebars and tables of contents. Check that the sticky content does not obscure links or text.
Syntax:
.container {
position: sticky;
}
Example:
The demo uses position: sticky and top: 0 to keep the current letter heading visible in an alphabetical list.
CSS:
p {
position: sticky;
top: 0px;
background-color: #e1f4fc;
padding: 20px;
font-weight: bold;
}
ul{
margin: 20px 0;
}
li{
padding-left: 20px;
}
Try it in CodePen:
6. CSS line-clamp: limit a preview to a number of lines
Line clamping can keep previews compact. With the prefixed implementation, combine a line limit, a legacy box layout, and overflow clipping.
Syntax:
.truncated-text {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
display: -webkit-boxselects the legacy box layout required for the prefixed pattern.-webkit-line-clamp: 3limits the block to three visible lines in this example.-webkit-box-orient: verticalsets the orientation required to clamp the text vertically.overflow: hiddenclips the content beyond the line limit.
Example:
Show a short product description in a list and let readers open the product page for the full text.
CSS:
.product-description {
display: -webkit-box;
-webkit-line-clamp: 3; /* Clamp after 3 lines */
-webkit-box-orient: vertical;
overflow: hidden;
}
Try it in CodePen:
7. CSS clamp(): fluid typography within limits
Use clamp() to let a font size vary with the viewport while keeping it between a minimum and a maximum.
Syntax:
Supply three values separated by commas.
property: clamp(minimum, preferred, maximum);
- Property: The CSS property receiving the value, such as
font-size. - Minimum: The lower bound.
- Preferred: The value to use while it falls within the bounds. A viewport-relative term can make this value fluid.
- Maximum: The upper bound, provided it is at least as large as the minimum.
Example:
This heading has a minimum of 1em, a maximum of 2.5em, and a preferred value of 3vw.
A value of 3vw equals 30px in a 1,000px viewport or 21px in a 700px viewport, before clamping. Check how your text scales with browser zoom and consider combining relative font and viewport units in a real type scale.
CSS:
h2 {
font-size: clamp(1em, 3vw, 2.5em);
}
Try it in CodePen:
Read the fluid typography lesson →
8. CSS hyphens: allow breaks inside words
hyphens controls whether text can hyphenate at the end of a line. Automatic hyphenation can help long words fit inside a narrow column.
Syntax:
.hyphenation {
hyphens: auto | manual | none;
}
Values:
- auto: Allows the browser to hyphenate using the text’s language and supported hyphenation rules.
- manual: Allows explicit break opportunities such as a soft hyphen.
- none: Disables hyphenation. Other wrapping rules can still allow a line break.
Set the HTML lang attribute to the page language. For a Spanish document, use lang="es" on <html>.
<html lang="es">
Set lang on a quote or paragraph that uses a different language from the surrounding text, such as lang="en" on an English paragraph.
<p lang="en">This is an example sentence in English within a paragraph.</p>
Example:
The demo uses hyphens: auto on an image card’s heading and paragraph so long words can break when the card is narrow.
Try it in CodePen:
9. CSS custom properties: reuse named values
Custom properties let you give names to values such as colors, font sizes, and spacing. Changing a declaration updates its references wherever that value applies in the cascade.
Modern browsers support custom properties. Check browser targets when maintaining a legacy project.
Syntax:
Start the name with --. Declare a shared default on :root and override it within a component when needed.
:root {
--primary-color: black; /* Custom value */
--font: "Arial";
}
Read the value where you need it with var(--name).
.example {
background-color: var(--primary-color);
font-family: var(--font);
}
Try it in CodePen:
Read the custom properties lesson →
10. Light and dark themes with prefers-color-scheme
Use prefers-color-scheme to adapt a site to the color scheme the browser reports.
The media feature matches a preference for a light or dark theme, often based on the operating system or browser settings.
Syntax:
Define rules for each theme. Test text, backgrounds, links, images, and native controls in both versions.
@media (prefers-color-scheme: dark) {
/* Dark theme styles */
body {
background-color: #222;
color: #fff;
}
}
@media (prefers-color-scheme: light) {
/* Light theme styles */
body {
background-color: #fff;
color: #222;
}
}
Try it in CodePen:
The example begins with the browser’s preference and adds a button for choosing a theme manually with JavaScript.
Read the light and dark themes lesson →
Continue with other CSS techniques after trying the ten examples above.
Use Grid and Flexbox for layout, math functions for calculated values, transitions and transforms for state changes, or gradients for backgrounds.
Follow the Eniun newsletter for new web development articles.