env.dev

CSS Cheat Sheet — Flexbox, Grid & Selectors Quick Reference

CSS quick reference: Flexbox, Grid, selectors, units, CSS variables, media queries, and transitions.

Last updated:

A quick reference for CSS properties and values. Covers Flexbox, Grid, selectors, units, variables, media queries, box model, and animations.

Flexbox

PropertyValues / Example
display: flexEnable flex container
flex-directionrow | row-reverse | column | column-reverse
justify-contentflex-start | center | space-between | space-around | space-evenly
align-itemsstretch | flex-start | center | flex-end | baseline
flex-wrapnowrap | wrap | wrap-reverse
gap8px, 1rem — space between flex items
flex: 1Shorthand for flex-grow:1 flex-shrink:1 flex-basis:0%
align-selfOverride align-items for a single item

Grid

PropertyValues / Example
display: gridEnable grid container
grid-template-columnsrepeat(3, 1fr) | 200px 1fr 200px
grid-template-rowsauto 1fr auto
grid-gap / gap16px — space between grid cells
grid-columnspan 2 | 1 / 3 — column placement
grid-rowspan 2 | 1 / -1 — row placement
grid-template-areasNamed grid areas for layout
place-itemscenter — shorthand for align-items + justify-items

Selectors

SelectorDescription
.classSelect elements by class name
#idSelect element by ID
div > pDirect child combinator
div + pAdjacent sibling combinator
div ~ pGeneral sibling combinator
:hoverMouse over state
:nth-child(2n)Every even child element
::before / ::afterInsert pseudo-element content

Units

UnitDescription
pxAbsolute pixels
remRelative to root font size (16px default)
emRelative to parent font size
%Percentage of parent element
vw / vhPercentage of viewport width / height
dvhDynamic viewport height (mobile-safe)
chWidth of the "0" character
frFractional unit for CSS Grid

CSS Variables

SyntaxDescription
--color: #3b82f6Define a custom property
var(--color)Use a custom property
var(--color, #000)Use with fallback value
:root { --x: 1rem }Define global variable on root
element.style.setProperty()Set variable from JavaScript
@property --x { ... }Register typed custom property

Media Queries

QueryDescription
@media (max-width: 768px)Styles for screens up to 768px
@media (min-width: 1024px)Styles for screens 1024px and up
@media (prefers-color-scheme: dark)Dark mode preference
@media (prefers-reduced-motion)Reduced motion preference
@media printPrint stylesheet
@container (min-width: 400px)Container query

Box Model

PropertyDescription
box-sizing: border-boxWidth/height include padding and border
marginSpace outside the border
paddingSpace inside the border
border1px solid #ccc — border shorthand
outlineLike border but does not affect layout
width / heightElement dimensions
max-width / min-widthConstrain element width
overflowvisible | hidden | scroll | auto

Transitions & Animations

PropertyDescription
transitionall 0.3s ease — shorthand
transition-propertyopacity, transform — properties to animate
transition-duration300ms | 0.3s
transition-timing-functionease | linear | ease-in-out | cubic-bezier()
@keyframes name { }Define animation keyframes
animationname 1s ease infinite — shorthand
animation-delayTime before animation starts
transformtranslate() rotate() scale()

Frequently Asked Questions

What is the difference between Flexbox and Grid?

Flexbox is one-dimensional (row OR column). Grid is two-dimensional (rows AND columns). Use Flexbox for component layouts and Grid for page layouts. They can be combined.

What are CSS custom properties (variables)?

CSS variables are defined with -- prefix: --color: #333. Use with var(): color: var(--color). They cascade and can be scoped to selectors. Define globals on :root.

How do media queries work?

Media queries apply styles based on viewport conditions. Example: @media (max-width: 768px) { ... }. Use min-width for mobile-first design. Modern CSS also supports @container queries.