
Responsive Website CSS Practice Projects
10/05/2024
Modern CSS Techniques: 10 Features to Try
11/05/2024
Advanced CSS cheat sheet
This cheat sheet collects the properties and syntax covered in the advanced CSS lessons. For beginner and intermediate topics as well, see the complete CSS properties reference.
1. CSS custom properties
:root {
--primary-color: black; /* Custom value */
--font: "Arial";
}
.example {
background-color: var(--primary-color);
font-family: var(--font);
}
Read the custom properties lesson →
2. CSS gradients
| Property | Values | Example |
|---|---|---|
| background | linear-gradient(direction or angle, color, color, …) | background: linear-gradient(to right, red, orange); |
| background | linear-gradient(direction or angle, color stop, color stop, …) | background: linear-gradient(to right, red 20%, orange 40%); |
| background | linear-gradient(color, color, color…) | background: linear-gradient(red, orange); |
| background | radial-gradient(circle or ellipse, color, color, …) | background: radial-gradient(circle, red, orange); |
| background | radial-gradient(shape and size at position, color, color, …) | background: radial-gradient(circle closest-side, red, orange); |
| background | radial-gradient(circle or ellipse, color stop, color stop, …) | background: radial-gradient(circle at top left, red 20%, orange 40%); |
| background | radial-gradient(color, color, color…) | background: radial-gradient(red, orange, blue); |
| background | radial-gradient(farthest-side, color, color, …) | background: radial-gradient(farthest-side, red, blue); |
3. CSS transitions
transition:[property] [duration] [timing-function] [delay];
4. CSS transforms
| Property | Description | Example |
|---|---|---|
| Scale | Takes one or two scaling factors, scale(x) or scale(x,y). An absolute factor greater than 1 enlarges the element along that axis. A negative factor also reflects it. | transform: scale(0.5) |
| Translate | Moves the element along one or both axes. Use translate(x) or translate(x,y) with lengths or percentages. | transform: translate(10px) |
| Rotate | Rotates the element by the given angle: rotate(angle). | transform: rotate(45deg) |
| Skew | Skews the element horizontally or vertically. Use skew(x) or skew(x,y) with angle values. | transform: skew(45deg) |
| Matrix | Specifies a two-dimensional transformation using matrix(a,b,c,d,tx,ty). The final two arguments set translation and the first four define the linear transformation. | transform: matrix(0.5, 0.1, 0.5, 1, 10, -2) |
Use transition to animate a change to transform:
.box{
transition: 1s linear;
}
.box:hover{
transform: scale(.5);
}
5. CSS animations
Shorthand syntax:
selector {
animation: name duration timing-function delay iteration-count direction fill-mode play-state;
}
nameidentifies therule that defines the animation.
keyframesdurationis the length of one cycle, in seconds or milliseconds.timing-functionsets the rate of change over time, for examplelinear,ease,ease-in,ease-out,ease-in-out,cubic-bezier(), orsteps().delayis the time to wait before starting, in seconds or milliseconds. A negative delay starts partway through the animation.iteration-countsets how many times the animation runs. Use a nonnegative number orinfinite.directionsets playback tonormal,reverse,alternate, oralternate-reverse.fill-modecontrols whether keyframe styles apply before or after playback. Usenone,forwards,backwards, orboth.play-stateisrunningorpaused.
Individual property syntax:
selector{
animation-name: identifier;
subproperty: value;
}
The individual properties are:
- animation-name
- animation-duration
- animation-timing-function
- animation-delay
- animation-iteration-count
- animation-direction
- animation-fill-mode
- animation-play-state
6. CSS math functions
| Function | Description | Values and examples |
|---|---|---|
| abs() | Returns the absolute value of a number | abs(-10) = 10 |
| sin() | Returns the sine of an angle | sin(45deg) ≈ 0.71 |
| cos() | Returns the cosine of an angle | cos(60deg) = 0.5 |
| tan() | Returns the tangent of an angle | tan(30deg) ≈ 0.58 |
| sqrt() | Returns the square root of a number | sqrt(16) = 4 |
| pow() | Raises a number to a power | pow(2, 3) = 8 |
| min() | Returns the smallest of the values | min(10, 5) = 5 |
| max() | Returns the largest of the values | max(10, 5) = 10 |
| random() | Returns a random value within a given range where supported | random(0, 1) → a value between 0 and 1 |
| round(down, …) | Use round(down, value, step) to round toward negative infinity | round(down, 3.8, 1) = 3 |
| round(up, …) | Use round(up, value, step) to round toward positive infinity | round(up, 3.2, 1) = 4 |
| round() | Rounds a value to an interval using the requested strategy | round(nearest, 3.7, 1) = 4 |
| clamp() | Clamps a preferred value between a minimum and a maximum | clamp(2px, 5px, 4px) = 4px |
| calc() | Computes a CSS value using arithmetic | width: calc(100% - 20px) |
| attr() | Reads an HTML attribute for use in CSS | content: attr(data-text) |
Read the math functions lesson →
7. CSS Grid
| Property | Description | Values and examples |
|---|---|---|
| display | Establishes a grid formatting context | grid | inline-grid |
| grid-template-columns | Defines the grid’s column tracks | grid-template-columns: 1fr 2fr 1fr |
| grid-template-rows | Defines the grid’s row tracks | grid-template-rows: 100px 200px |
| grid-gap | Shorthand for column-gap and row-gap (use gap in new code) | grid-gap: 10px |
| grid-column-gap | Sets the space between columns (use column-gap in new code) | grid-column-gap: 20px |
| grid-row-gap | Sets the space between rows (use row-gap in new code) | grid-row-gap: 10px |
8. CSS clamp()
property: clamp(minimum, preferred, maximum);
- Property: The property you want to set with
clamp(), such asfont-size. - Minimum value: The lower bound. If the preferred value is smaller, this bound takes effect.
- Preferred value: The value used when it falls within the bounds. A viewport-relative length can make it vary with screen size.
- Maximum value: The upper bound. If the preferred value is larger, this bound takes effect, provided the minimum is no larger than the maximum.
Read the fluid typography lesson →
9. CSS line-clamp
.elemento{
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
display: -webkit-boxselects the legacy box layout required by the prefixed clamping pattern.-webkit-line-clamp: 3limits the visible text to three lines in this example.-webkit-box-orient: verticalsets the orientation required to clamp lines of text vertically.overflow: hiddenclips excess content outside the box.
10. CSS Scroll Snap
.scroll-container {
scroll-snap-type: [x | y | block | inline] [mandatory | proximity];
}
.section {
scroll-snap-align: [start | center | end | none];
}
scroll-snap-typeselects an axis such asx,y,block, orinline. Usemandatoryto require the scroll position to settle at a snap point, orproximityto snap when close to one.scroll-snap-aligndefines how a child aligns with the snap area. Usestart,center,end, ornone.
11. CSS scroll-behavior
html{
scroll-behavior: auto | smooth;
}
autouses instant scrolling by default.smoothanimates scrolling initiated by navigation or programmatically, subject to browser behavior and user preferences.
Read the smooth scrolling lesson →
12. CSS blend modes
.element {
mix-blend-mode: <blend-mode>;
}
| 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. |
13. CSS hyphens
.elemento{
hyphens: auto | manual | none;
}
autolets the browser choose hyphenation points using the language of the text and the dictionaries available.manualbreaks at explicit hyphenation opportunities, such as a soft hyphen.nonedisallows hyphenation. A long word may overflow or wrap under another applicable wrapping rule.
14. CSS filters
elemento {
filter: <filter-function>(<value>) ;
}
| 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 |
15. Browser prefixes
| Prefix | Browser |
| -moz- | Firefox |
| -webkit- | Safari and Chrome |
| -o- | Opera |
| -khtml- | Konqueror |
| -ms- | Internet Explorer |
| -chrome- | Not a standard Chrome prefix. Use -webkit- only where required. |