CSS reference
CSS selectors, pseudo-classes and properties, with every value shown in a live example. The box model, Flexbox, Grid, typography, transitions and animations.
217 entries
Selectors
Selecting elements by tag, class, attribute and position in the document tree.
- Type selectorSelects every element with a given tag name, for example every p paragraph or every li list item.
- .classSelects elements that have the given class in their class attribute. The most commonly used selector for styling components.
- #idSelects the element with the given identifier from its id attribute. It carries very high specificity, so its rules are hard to override later.
- [attribute]Selects elements by an HTML attribute and its value: exact, starting, ending, a fragment, or a word from a list.
- *The asterisk matches every element. Used for style resets and rules that target all the children of a chosen container.
- A BA space between selectors selects B elements anywhere inside element A, at any depth.
- A > BThe > sign selects B elements that are direct children of element A, meaning they sit exactly one level below it.
- A + BThe + sign selects element B that sits directly after element A and shares the same parent.
- A ~ BThe ~ sign selects every B element that comes anywhere after element A and shares the same parent.
- A, BA comma combines several selectors into a single rule, to give them the same styles without repeating declarations.
- &Lets you write rules inside other rules, like in Sass. The & sign stands for the parent rule’s selector.
- SpecificityA selector’s weight, which decides which rule wins when several rules set the same property on the same element.
Pseudo-classes
States and positions of elements: hover, focus, checked, the nth element and validation.
- :hoverMatches the element the mouse cursor is currently over. The basis of hover effects on buttons, links, and cards.
- :focusMatches the element that has focus, meaning it currently receives keyboard input, such as a clicked form field.
- :focus-visibleMatches a focused element only when the browser decides the indicator should be visible, such as during keyboard navigation.
- :focus-withinMatches an element that either has focus itself or contains an element with focus. Lets you highlight an entire form or card.
- :activeMatches an element at the moment it’s activated, most often while the mouse button is pressed down on a button or link.
- :first-childMatches an element that is the first child of its parent.
- :last-childMatches an element that is the last child of its parent.
- :nth-child()Selects elements by position among siblings: every other one, every third, the first three, or everything from the seventh on.
- :nth-of-type()Selects elements by position among siblings with the same tag, for example every other image or the third paragraph.
- :not()Selects elements that match neither the given selector nor any selector in the list.
- :is()Matches an element against any selector in a list. Shortens long, repetitive selectors into a single notation.
- :where()Works like :is(), but always has zero specificity. Ideal for base styles that should be easy to override.
- :has()Selects an element based on what it contains or what follows it. Lets you style a parent according to its children.
- :checkedMatches a checked checkbox, a selected radio button, and a selected option in a select list.
- :disabledMatches disabled form elements, meaning ones that can’t be clicked, checked, or edited.
- :invalidMatches form fields whose value breaks an HTML validation rule, such as an empty required field or a malformed email address.
- :placeholder-shownMatches an input or textarea field that is currently showing its placeholder text, meaning it’s empty and has a placeholder attribute.
- :rootMatches the document’s root element, the html tag in HTML. The usual place for CSS variables shared across the whole page.
Pseudo-elements
Parts that are not in the HTML: content before and after an element, the first letter, the selection.
- ::beforeCreates a virtual element at the start of an element’s content. Used for icons, decorations, and labels without extra HTML.
- ::afterCreates a virtual element at the end of an element’s content. Ideal for arrows, badges, underlines, and overlays.
- ::placeholderStyles the hint text displayed in an empty input or textarea field.
- ::selectionChanges the appearance of a piece of text selected by the user with the mouse or keyboard.
- ::markerStyles a list item’s marker: the bullet in a ul list or the number in an ol list.
- ::first-letterStyles the first letter of the first line in a block of text. The classic way to make a drop cap at the start of an article.
- ::first-lineStyles the first line of text in a block element, regardless of how many words fit in it.
- ::backdropStyles the background layer behind a modal dialog, a fullscreen element, and an open popover.
Box model
Sizes, margins, padding, borders and overflow.
- widthSets the width of an element.
- heightSets the height of an element.
- min-widthSets a minimum width the element won’t shrink below.
- max-widthSets a maximum width the element won’t grow beyond.
- min-heightSets a minimum height, while letting the element grow with its content.
- max-heightSets the maximum height of an element.
- marginSets the outer space between an element and its neighbors.
- paddingSets the inner space between the content and the border.
- borderShorthand that sets the width, style, and color of a border.
- border-widthSets the thickness of a border.
- border-styleSets the border line style: solid, dashed, dotted, and more.
- border-colorSets the color of a border.
- border-radiusRounds the corners of an element.
- box-sizingDecides whether width and height include the padding and border.
- outlineDraws an outline around an element that doesn’t take up space in the layout.
- overflowDecides what happens to content that doesn’t fit in an element.
- aspect-ratioSets the ratio of an element’s width to its height.
- outline-offsetPushes the outline away from the element’s edge, or pulls it inward, without changing the page layout.
- overflow-xDecides what happens to content overflowing horizontally. Its twin overflow-y does the same vertically.
- margin-inlineSets the margins at the start and end of a line. The logical version of margin-left and margin-right, aware of writing direction.
- padding-inlineSets the inner space at the start and end of a line. The logical version of padding-left and padding-right, aware of writing direction.
- inline-sizeSets the element’s size along the text line. In horizontal writing that’s the width, and its pair block-size sets the height.
- border-imageReplaces a plain border with an image or gradient sliced into corners and edges.
Layout and positioning
How an element is displayed, its position, stacking and floats.
- displayDetermines how an element behaves in the page layout and how it lays out its children.
- positionChooses how an element is positioned: in the flow, relative, absolute, fixed, or sticky.
- topMoves a positioned element away from the top edge of its reference point.
- rightMoves a positioned element away from the right edge of its reference point.
- bottomMoves a positioned element away from the bottom edge of its reference point.
- leftMoves a positioned element away from the left edge of its reference point.
- insetA shorthand that sets top, right, bottom, and left in a single declaration.
- z-indexDetermines which element sits on top when elements overlap.
- floatMoves an element to the left or right edge, and text wraps around it on the other side.
- clearForces an element to move below earlier floated elements.
- visibilityHides an element but leaves behind the empty space it occupied.
- container-typeTurns an element into a container that @container rules can query for its size. Also covers container-name and the container shorthand.
- columnsSplits an element’s content into columns like a newspaper. A shorthand for column-width and column-count.
- column-ruleDraws a line between the columns of a multi-column layout. A shorthand for the line’s width, style, and color.
- containPromises the browser that an element’s interior doesn’t affect the rest of the page, so it can limit layout recalculation and painting.
- content-visibilityLets the browser skip rendering content that isn’t visible, speeding up long pages.
Flexbox
One-dimensional layout of items in a row or a column.
- justify-contentPositions items along the main axis of a flex or grid container.
- align-itemsAligns items along the cross axis of a flex or grid container.
- flex-directionSets the direction of the main axis: flex items line up in a row or in a column.
- flex-wrapLets flex items wrap onto new lines when there isn’t enough room.
- flex-flowA shorthand that combines flex-direction and flex-wrap in one declaration.
- align-contentPositions entire lines of items along a container’s cross axis.
- align-selfOverrides the cross-axis alignment of a single item.
- orderChanges the display order of an item inside a flex or grid container.
- flex-growDetermines how eagerly an item grows to fill free space.
- flex-shrinkDetermines how much an item shrinks when the container runs out of room.
- flex-basisSets a flex item’s starting size on the main axis, before it grows or shrinks.
- flexA shorthand that sets an item’s flex-grow, flex-shrink, and flex-basis.
- gapSets the spacing between items in a flex, grid, or multi-column container.
- row-gapSets the spacing between rows in a flex or grid container.
- column-gapSets the spacing between columns in a flex, grid, or multi-column container.
Grid
A two-dimensional grid with rows, columns and named areas.
- grid-template-columnsDefines the grid’s columns: their number and widths.
- grid-template-rowsDefines the grid’s rows: their number and heights.
- grid-template-areasDescribes the grid layout with named areas, almost like a drawing in code.
- grid-templateA shorthand that sets the grid’s rows, columns, and named areas in one declaration.
- grid-auto-columnsSets the width of the columns the grid creates automatically.
- grid-auto-rowsSets the height of the rows the grid creates automatically.
- grid-auto-flowDecides how items without an explicit position fill the grid.
- grid-columnSets which grid columns an item starts and ends in.
- grid-rowSets which grid rows an item starts and ends in.
- grid-areaAssigns an item to a named grid area, or sets its rows and columns all at once.
- justify-itemsAligns all items within their cells along the horizontal axis.
- justify-selfAligns a single item within its grid cell along the horizontal axis.
- place-itemsA shorthand that sets align-items and justify-items at the same time.
- place-contentA shorthand that sets align-content and justify-content at the same time.
- place-selfA shorthand that sets align-self and justify-self for a single item.
Typography
Fonts, sizes, line height, alignment and text decoration.
- font-familyPicks a font, with a list of fallback fonts in case the first one is unavailable.
- font-sizeSets the font size.
- font-weightSets the font weight, from thin to extra bold.
- font-styleTurns on italics or an oblique slant for text.
- line-heightSets the height of each line of text, the space between lines.
- letter-spacingChanges the spacing between letters.
- word-spacingChanges the spacing between words.
- text-alignAligns text horizontally: left, right, center, or justified.
- text-decorationAdds or removes lines next to text: underline, strikethrough, overline.
- text-transformChanges letter case: all uppercase, all lowercase, or capitalized first letters.
- text-indentSets the indentation of the first line of text in a block.
- text-shadowAdds a shadow beneath text.
- white-spaceDecides how to treat spaces, line breaks, and line wrapping in text.
- word-breakLets words break at any point when they don’t fit on a line.
- overflow-wrapBreaks long words only when they would otherwise overflow their container.
- text-overflowDecides how to mark text truncated at the end of a line, for example with an ellipsis.
- vertical-alignAligns inline elements and table cell content vertically.
- font-variantTurns on font variants, for example small caps or evenly spaced numerals.
- fontShorthand that sets font style, weight, size, line height, and family all at once.
Colors and backgrounds
Text color, backgrounds, gradients and background images.
- colorSets the text colour and the default colour of borders, decorations and SVG icons that use currentcolor.
- background-colorSets the background colour of an element.
- background-imageSets an image or gradient as an element’s background.
- background-sizeSets the size of the background image: covering everything, fitting entirely, or specific dimensions.
- background-positionSets the position of the background image within the element.
- background-repeatDecides whether and how the background image repeats to fill the element.
- background-attachmentDecides whether the background scrolls with the content or stays in place.
- background-clipSets which area of the element the background extends to, including the text background effect.
- background-originSets which area of the element the background image position is measured from.
- backgroundA shorthand that sets all background properties in one declaration.
- opacitySets the transparency of the whole element together with its contents.
Effects
Shadows, filters, opacity, clipping and blend modes.
- box-shadowAdds a shadow around an element, outside or inside.
- filterApplies graphical effects to an element: blur, brightness, contrast, greyscale and more.
- backdrop-filterApplies a filter to what is visible behind an element, for example blurring the background behind a panel.
- mix-blend-modeSets how an element’s colours blend with what lies beneath it.
- background-blend-modeBlends an element’s background layers together: images, gradients and colour.
- clip-pathClips an element to a chosen shape: a circle, ellipse, polygon and more.
- mask-imageUses an image or gradient as a transparency mask for an element.
- object-fitDecides how an image or video fills a box with different proportions.
- object-positionSets the position of an image or video within its box.
Transforms
Moving, rotating, scaling and perspective.
- transformMoves, rotates, scales, or skews an element without affecting the page layout.
- transform-originSets the point an element rotates around and scales from.
- translateMoves an element horizontally, vertically, or in depth, independently of the transform property.
- rotateRotates an element by a given angle, independently of the transform property.
- scaleEnlarges or shrinks an element, independently of the transform property.
- perspectiveAdds depth to the 3D transforms of an element’s children.
- perspective-originSets the point from which the viewer looks at a 3D scene.
- backface-visibilityDecides whether the back of an element rotated in 3D is visible.
- transform-styleDecides whether an element’s children live in a shared 3D space or are flattened.
Transitions
Smoothly changing a value between two states.
- transitionA shorthand that makes CSS value changes happen smoothly instead of abruptly.
- transition-propertySpecifies which CSS properties should change smoothly.
- transition-durationSets how long a transition lasts.
- transition-timing-functionSets the transition’s easing curve: accelerating, decelerating, or stepped motion.
- transition-delaySets how long after a value change the transition starts.
- transition-behaviorLets you include properties that change discretely, such as display, in transitions.
Animations
Keyframes and every property that controls an animation.
- animation-nameSpecifies which @keyframes an element plays.
- animationA shorthand that sets all the parameters of an animation based on @keyframes.
- animation-durationSets how long one animation cycle takes.
- animation-timing-functionSets the pacing curve of the animation between keyframes.
- animation-delaySets how long to wait before the animation starts.
- animation-iteration-countSets how many times the animation repeats.
- animation-directionSets the playback direction of the animation: forwards, backwards, or alternating.
- animation-fill-modeDecides how the element looks before the animation starts and after it ends.
- animation-play-statePauses or resumes an animation.
- @keyframesDefines the stages of an animation, known as keyframes.
Interaction and more
Cursor, selection, scrolling, lists, tables and fitting images.
- cursorSets the appearance of the mouse cursor over an element.
- pointer-eventsStops an element from reacting to clicks and hovering.
- user-selectLets you block or ease text selection by the user.
- resizeLets the user resize an element by dragging its corner.
- scroll-behaviorEnables smooth scrolling when jumping to anchors and when scrolling via script.
- scroll-snap-typeMakes a scrolling container stop on chosen elements.
- scroll-snap-alignSpecifies which edge an element snaps by in a scrolling container.
- overscroll-behaviorPrevents the whole page from scrolling after reaching the end of a scrollable element.
- accent-colorChanges the accent colour of form controls: checkboxes, radio buttons, sliders, and progress bars.
- caret-colorSets the colour of the text cursor in input fields.
- list-style-typeSets the type of list item marker: bullets, numbers, letters, or a custom symbol.
- list-style-positionSets whether the list marker sits outside or inside the element’s block.
- border-collapseMerges adjacent table cell borders into a single line or keeps them separate.
- border-spacingSets the gap between table cells with separated borders.
- table-layoutChooses the algorithm for computing table column widths: content-based or fixed.
- contentInserts generated content in the ::before and ::after pseudo-elements.
- appearanceTurns off the system appearance of form controls so they can be styled from scratch.
At-rules
Media queries, container queries, custom fonts, cascade layers and feature queries.
- @mediaApplies styles only when the device meets a condition: a matching window width, a dark theme, a mouse, or printing the page.
- @containerApplies styles based on the size of a container instead of the whole window. A component adapts to the space it gets in the layout.
- @supportsApplies styles only in browsers that support a given property, value, or selector. It lets you use new features safely.
- @font-faceRegisters a typeface under a chosen name that you then use in font-family. It lets you use fonts from your own site’s files.
- @importIncludes the contents of another CSS stylesheet in the current one. It can place it straight into a layer or load it only for chosen media.
- @layerSplits styles into cascade layers with a fixed order. A rule from a later layer wins regardless of selector specificity.
- @propertyRegisters a CSS variable with a type, an initial value, and an inheritance rule. Such a variable can be animated smoothly, e.g. a gradient angle.
- @starting-styleDescribes the style a transition starts from when an element appears on the page. This lets new elements slide in smoothly.
Functions and units
calc(), clamp(), var(), gradients, color notations and length units.
- calc()Computes a value from a mathematical expression and lets you combine different units, for example percentages with pixels.
- clamp()Keeps a value between a minimum and a maximum, and lets it grow smoothly in between. The basis of fluid typography.
- min()Picks the smallest of the given values. It works as an upper limit, for example a width of 100% of the parent, but no more than 600px.
- max()Picks the largest of the given values. It works as a lower limit, for example spacing of 5% of the width, but never less than 16px.
- var()Inserts the value of a CSS variable, that is, a custom property written as --name. It also lets you provide a fallback value.
- rgb()Writes a colour as a mix of red, green, and blue light, with optional transparency. It also covers hex notation.
- hsl()Writes a colour through its hue on the colour wheel, saturation, and lightness. It makes it easy to lighten, darken, or mute a colour.
- oklch()Writes a colour through lightness, chroma, and hue in a space matched to the eye. Colours of the same lightness look equally light.
- color-mix()Mixes two colours in given proportions and in a chosen colour space. It creates lighter, darker, and semi-transparent variants.
- linear-gradient()Creates a smooth colour transition along a straight line. It is used as an image, most often in background or background-image.
- radial-gradient()Creates a colour transition radiating outwards from a point, along a circle or an ellipse. Good for glows, vignettes, and soft lights.
- conic-gradient()Creates a colour transition around a point, like a clock hand. It draws pie charts, colour wheels, and rotating borders.
- repeat()Repeats a pattern of grid columns or rows. With auto-fill or auto-fit and minmax() it creates a responsive grid without media queries.
- UnitsAn overview of length units in CSS: from fixed pixels, through font-relative em and rem, to viewport, container, and grid units.
Comparisons
How similar properties and layout techniques differ and when to pick each one.
- Flexbox vs GridFlexbox lays out items along one dimension and sizes to their content. Grid defines a two-dimensional grid of rows and columns that you place items into.
- margin vs paddingmargin is transparent space outside the element. padding is space inside the border, between the content and the edge, and it takes on the element’s background.
- display vs visibility vs opacitydisplay: none removes an element from the layout, visibility: hidden hides it but keeps its space, and opacity: 0 makes it transparent, yet still clickable.
- transition vs animationtransition animates a change between two states after a trigger, e.g. :hover. animation plays back steps from @keyframes on its own, even in a loop.
- :nth-child() vs :nth-of-type()nth-child() counts every one of the parent’s elements, regardless of tag. nth-of-type() only counts siblings that share the same tag as the chosen selector.
- border vs outlineborder is part of the box model and enlarges the element, while outline draws a ring outside it, with no effect on the page layout or size.
- :focus vs :focus-visiblefocus matches any element that has focus, even after a mouse click. focus-visible only shows an indicator where the browser decides it’s actually needed.