Menu

CSS Cheat Sheet

Last updated

Selectors

How to target the elements you want to style.

SelectorMatches
*All elements
pAll <p> elements (by tag)
.btnElements with class="btn"
#mainThe element with id="main"
div pAll <p> inside a <div> (descendant)
div > pDirect children only
a:hoverLinks being hovered (pseudo-class)
li:first-childFirst <li> in its parent
li:nth-child(2n)Every even <li>
input[type="text"]Inputs with that attribute
p::beforeGenerated content before <p> (pseudo-element)

Box model

Every element is a box: content, padding, border, margin.

PropertyWhat it does
width / heightSize of the content box
paddingSpace inside the border
border1px solid #333 - width, style, color
marginSpace outside the border
box-sizing: border-boxMake width include padding + border
margin: 0 autoCenter a block element horizontally
overflow: hiddenClip content that spills out

Typography & color

PropertyExample
colorcolor: #333 - text color
backgroundbackground: #fff or an image/gradient
font-familyfont-family: system-ui, sans-serif
font-sizefont-size: 16px (or rem, em)
font-weight400 (normal) … 700 (bold)
line-height1.5 - spacing between lines
text-alignleft, center, right, justify
text-decorationunderline, none

Flexbox

One-dimensional layout - rows or columns. Set display: flex on the parent.

Property (on container)What it does
display: flexMake children flex items
flex-directionrow (default) or column
justify-contentAlign along the main axis (center, space-between)
align-itemsAlign along the cross axis (center, stretch)
gapSpace between items, e.g. gap: 16px
flex-wrapwrap lets items flow onto new lines
flex: 1 (on a child)Item grows to fill available space

Grid

Two-dimensional layout. Set display: grid on the parent.

PropertyWhat it does
display: gridMake children grid items
grid-template-columns1fr 1fr 1fr or repeat(3, 1fr)
grid-template-rowsDefine row sizes
gapSpace between rows and columns
grid-column1 / 3 - span an item across columns
auto-fit / minmaxrepeat(auto-fit, minmax(200px, 1fr)) for responsive grids
place-items: centerCenter every item in its cell

Positioning & display

Property / valueWhat it does
position: staticDefault - in normal flow
position: relativeOffset from its normal spot
position: absolutePositioned vs nearest positioned ancestor
position: fixedPinned to the viewport
position: stickySticks when scrolled to a threshold
top / right / bottom / leftOffsets for positioned elements
z-indexStacking order (higher = on top)
display: noneRemoves the element from layout
display: inline-blockFlows inline but accepts width/height

Transitions & transforms

PropertyExample
transitiontransition: all 0.2s ease
transform: translatetranslate(10px, 0) - move
transform: scalescale(1.1) - resize
transform: rotaterotate(45deg)
opacity0 (invisible) … 1 (opaque)
box-shadow0 2px 8px rgba(0,0,0,.15)
border-radius8px - rounded corners

Units & responsive design

Unit / featureMeaning
pxAbsolute pixels
%Relative to the parent
remRelative to the root font size
emRelative to the current font size
vw / vh1% of viewport width / height
frFraction of free space (grid)
@media (max-width: 600px) { … }Apply rules below a breakpoint

Selectors, the box model, flexbox, grid, and the properties you always forget - on one page. This CSS cheat sheet is a quick reference for styling and laying out web pages, from targeting elements to centering them.

Everything here is standard CSS that works in modern browsers. Copy a rule, or try it live in the HTML playground where you can edit CSS with an instant preview.

CSS cheat sheet FAQ

Is this CSS cheat sheet free?
Yes. This CSS cheat sheet is free with no sign-up. Bookmark it and use it whenever you need to look up a selector, property, or layout pattern.
Should I use flexbox or grid?
Use flexbox for one-dimensional layouts - a row of buttons, a navbar, items in a single line or column. Use grid for two-dimensional layouts where you control both rows and columns at once, like a page layout or a card gallery. They work well together: a grid of cards where each card uses flexbox internally is common.
How do I center a div with CSS?
The simplest modern way is flexbox on the parent: display: flex; justify-content: center; align-items: center; centers a child both horizontally and vertically. For just horizontal centering of a block element with a set width, margin: 0 auto still works.
Can I practice CSS online?
Yes. Open the HTML playground to write HTML and CSS together with a live preview. When you want structure, Coddy's free interactive CSS course covers selectors, the box model, flexbox, and grid step by step.
Is this cheat sheet good for beginners?
Yes. It moves from selectors and the box model (the foundations) to flexbox, grid, and transitions, so you can start with the top sections and grow into modern layout.
Coddy programming languages illustration

Learn CSS with Coddy

GET STARTED