Skip to main content
← All cheat sheets
🎨

CSS Cheat Sheet

A quick-reference CSS cheat sheet covering selectors, the box model, flexbox, grid, colors, typography, and transitions.

Selectors

p { ... }Selects all elements by tag name.
.btn { ... }Selects elements with the class btn.
#main { ... }Selects the element with id main.
* { ... }Universal selector, matches every element.
nav a { ... }Descendant: any a inside a nav.
ul > li { ... }Child: direct li children of a ul.
a:hover { ... }Pseudo-class for the hover state.
li:nth-child(2n) { ... }Selects every even list item.

Box Model

margin: 16px;Space outside the element's border.
padding: 8px 12px;Space inside the border (vertical horizontal).
border: 1px solid #ccc;Border width, style, and color.
box-sizing: border-box;Include padding and border in width/height.
width: 100%;Sets the content/box width.
height: 240px;Sets a fixed element height.

Display & Position

display: block;Element takes full line width.
display: inline;Flows inline; ignores width/height.
display: none;Removes the element from layout.
position: relative;Offsets relative to its normal spot.
position: absolute;Positions vs nearest positioned ancestor.
position: fixed;Pins to the viewport while scrolling.
position: sticky; top: 0;Sticks once it reaches the offset.
z-index: 10;Stacking order for overlapping elements.

Flexbox

display: flex;Creates a flex container.
flex-direction: column;Stacks items vertically instead of in a row.
justify-content: space-between;Distributes items along the main axis.
align-items: center;Aligns items on the cross axis.
gap: 16px;Spacing between flex items.
flex-wrap: wrap;Allows items to wrap onto new lines.
flex: 1;Item grows to fill available space.

Grid

display: grid;Creates a grid container.
grid-template-columns: 1fr 1fr;Defines two equal-width columns.
grid-template-columns: repeat(3, 1fr);Three equal columns via repeat().
grid-template-rows: auto 1fr;Sizes rows; 1fr fills remaining space.
gap: 24px;Spacing between grid tracks.
grid-column: span 2;Makes an item span two columns.
grid-area: header;Places an item into a named area.

Typography

font-family: system-ui, sans-serif;Sets the typeface stack.
font-size: 1.125rem;Sets the text size.
font-weight: 600;Sets boldness (100 to 900).
line-height: 1.5;Vertical spacing between lines.
text-align: center;Aligns inline text horizontally.
letter-spacing: 0.05em;Adjusts spacing between letters.
text-transform: uppercase;Forces casing of the text.

Colors & Backgrounds

color: #1a1a1a;Sets text color via hex.
background: #f5f5f5;Sets a solid background color.
color: rgb(34, 102, 255);Color using red/green/blue values.
color: hsl(210, 90%, 55%);Color via hue, saturation, lightness.
background: linear-gradient(90deg, #06f, #0cf);Left-to-right gradient fill.
opacity: 0.6;Sets element transparency (0 to 1).

Spacing & Sizing Units

padding: 16px;Absolute pixels, fixed size.
font-size: 1.2em;Relative to the parent's font size.
margin: 2rem;Relative to the root font size.
width: 50%;Percentage of the parent's size.
width: 100vw;Percentage of the viewport width.
height: 100vh;Percentage of the viewport height.

Transitions & Transforms

transition: all 0.2s ease;Animates property changes smoothly.
transform: translate(10px, 0);Moves an element along x/y.
transform: scale(1.05);Scales the element's size.
transform: rotate(45deg);Rotates the element.
@keyframes spin { to { transform: rotate(360deg); } }Defines an animation sequence.
animation: spin 1s linear infinite;Runs a named keyframe animation.

Responsive

@media (min-width: 768px) { ... }Applies styles at tablet width and up.
@media (min-width: 1024px) { ... }Targets desktop-sized viewports.
@media (max-width: 640px) { ... }Targets small/mobile viewports.
@media (prefers-color-scheme: dark) { ... }Styles for dark mode preference.
img { max-width: 100%; height: auto; }Makes images scale responsively.

Reading is good β€” doing is better. Practice CSS with interactive, auto-graded lessons.

Start learning CSS