CSS Cheat Sheet

Use this CSS cheat sheet as a quick reference for selectors, properties, layout, colors, spacing, typography, responsive design, and common patterns.

Basic CSS Syntax

A CSS rule has a selector, a property, and a value.

selector {
    property: value;
}

Example:

p {
    color: blue;
    font-size: 16px;
}

CSS Rule Parts

  • p is the selector.
  • color is the property.
  • blue is the value.
  • Each declaration ends with a semicolon.
  • Declarations live inside curly braces.

Ways to Add CSS

External CSS

Use an external stylesheet for most projects.

<link rel="stylesheet" href="styles.css">
body {
    font-family: Arial, sans-serif;
}

Internal CSS

Use a <style> block inside the HTML file.

<style>
    body {
        background-color: #f5f5f5;
    }
</style>

Inline CSS

Add styles directly to an element.

<p style="color: red;">Important message</p>

Use inline CSS sparingly. External CSS is easier to maintain.

Basic Selectors

Element Selector

Targets all elements of one type.

p {
    color: #333;
}

Class Selector

Targets elements with a class.

.card {
    padding: 24px;
}
<div class="card">Content</div>

ID Selector

Targets one element with a specific ID.

#main-header {
    background-color: white;
}
<header id="main-header">Header</header>

Use classes for most styling. Use IDs when you need a unique hook.

Universal Selector

Targets every element.

* {
    box-sizing: border-box;
}

Group Selector

Applies the same styles to multiple selectors.

h1,
h2,
h3 {
    font-family: Arial, sans-serif;
}

Combinator Selectors

Descendant Selector

Targets elements inside another element.

.card p {
    color: #555;
}

Child Selector

Targets direct children only.

.menu > li {
    list-style: none;
}

Adjacent Sibling Selector

Targets the next sibling.

h2 + p {
    margin-top: 0;
}

General Sibling Selector

Targets later siblings.

h2 ~ p {
    color: #444;
}

Pseudo-Classes

Pseudo-classes style elements in a specific state.

Hover

.button:hover {
    background-color: #005fcc;
}

Focus

.input:focus {
    outline: 2px solid #0066ff;
}

Focus Visible

.button:focus-visible {
    outline: 2px solid currentColor;
    outline-offset: 2px;
}

First Child

li:first-child {
    font-weight: bold;
}

Last Child

li:last-child {
    margin-bottom: 0;
}

Nth Child

li:nth-child(odd) {
    background-color: #f5f5f5;
}

Pseudo-Elements

Pseudo-elements style part of an element.

Before

.badge::before {
    content: "★ ";
}

After

.external-link::after {
    content: " ↗";
}

First Letter

.article p::first-letter {
    font-size: 2rem;
}

Placeholder

input::placeholder {
    color: #999;
}

Colors

Named Color

.text {
    color: red;
}

Hex Color

.text {
    color: #1f2937;
}

RGB Color

.text {
    color: rgb(31 41 55);
}

RGBA or Alpha Color

.card {
    background-color: rgb(255 255 255 / 0.8);
}

HSL Color

.highlight {
    color: hsl(220 80% 50%);
}

Current Color

.icon {
    color: #2563eb;
    border: 1px solid currentColor;
}

Units

Absolute Units

  • px sets fixed pixel values.
  • pt is mostly used for print styles.
.box {
    width: 300px;
}

Relative Units

  • % is relative to the parent.
  • em is relative to the current element’s font size.
  • rem is relative to the root font size.
  • vw is relative to viewport width.
  • vh is relative to viewport height.
.container {
    width: 90%;
    max-width: 1200px;
}

.title {
    font-size: 2rem;
}

.hero {
    min-height: 100vh;
}

Box Model

Every element has content, padding, border, and margin.

.card {
    width: 300px;
    padding: 24px;
    border: 1px solid #ddd;
    margin: 32px;
}

Box Model Properties

  • width sets the content width.
  • height sets the content height.
  • padding adds space inside the element.
  • border adds a line around the element.
  • margin adds space outside the element.

Use Border Box

* {
    box-sizing: border-box;
}

This makes width and height calculations easier because padding and border are included in the element size.

Spacing

Margin

.section {
    margin-top: 48px;
    margin-bottom: 48px;
}

Padding

.card {
    padding: 24px;
}

Shorthand

.box {
    margin: 10px 20px 30px 40px;
}

The order is top, right, bottom, left.

Two-Value Shorthand

.box {
    padding: 16px 24px;
}

The first value controls top and bottom. The second controls left and right.

Display

Block

div {
    display: block;
}

Block elements start on a new line and take the full available width.

Inline

span {
    display: inline;
}

Inline elements flow with text and do not accept width or height in the same way.

Inline Block

.badge {
    display: inline-block;
    padding: 4px 8px;
}

Inline-block elements flow inline but accept width, height, padding, and margin.

None

.hidden {
    display: none;
}

This removes the element from layout.

Flexbox

Use flexbox for one-dimensional layouts, such as rows, columns, navigation bars, and centered content.

Basic Flex Container

.container {
    display: flex;
}

Horizontal Alignment

.container {
    display: flex;
    justify-content: center;
}

Vertical Alignment

.container {
    display: flex;
    align-items: center;
}

Center Both Ways

.container {
    min-height: 300px;
    display: flex;
    align-items: center;
    justify-content: center;
}

Row Direction

.container {
    display: flex;
    flex-direction: row;
}

Column Direction

.container {
    display: flex;
    flex-direction: column;
}

Gap Between Items

.container {
    display: flex;
    gap: 16px;
}

Wrap Items

.container {
    display: flex;
    flex-wrap: wrap;
}

Common Flex Item Properties

.item {
    flex: 1;
}
.item {
    align-self: flex-start;
}

Use flex: 1 when items should share available space.

CSS Grid

Use grid for two-dimensional layouts, such as cards, page sections, dashboards, and galleries.

Basic Grid

.grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 24px;
}

Responsive Grid

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 24px;
}

Place Items in the Center

.grid {
    display: grid;
    place-items: center;
}

Span Columns

.featured {
    grid-column: span 2;
}

Grid Areas

.layout {
    display: grid;
    grid-template-areas:
        "header header"
        "sidebar main"
        "footer footer";
    grid-template-columns: 250px 1fr;
}

.header {
    grid-area: header;
}

.sidebar {
    grid-area: sidebar;
}

.main {
    grid-area: main;
}

.footer {
    grid-area: footer;
}

Positioning

Static

Default positioning.

.box {
    position: static;
}

Relative

Moves an element relative to its normal position.

.box {
    position: relative;
    top: 10px;
}

Absolute

Positions an element relative to the nearest positioned parent.

.card {
    position: relative;
}

.badge {
    position: absolute;
    top: 12px;
    right: 12px;
}

Fixed

Positions an element relative to the viewport.

.cookie-banner {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
}

Sticky

Sticks an element after it reaches a scroll position.

.nav {
    position: sticky;
    top: 0;
}

Z-Index

Controls stacking order for positioned elements.

.modal {
    position: fixed;
    z-index: 1000;
}

Typography

Font Family

body {
    font-family: Arial, Helvetica, sans-serif;
}

Font Size

p {
    font-size: 16px;
}

Font Weight

strong {
    font-weight: 700;
}

Line Height

p {
    line-height: 1.6;
}

Letter Spacing

.label {
    letter-spacing: 0.08em;
}

Text Align

.center {
    text-align: center;
}

Text Transform

.label {
    text-transform: uppercase;
}

Text Decoration

a {
    text-decoration: none;
}

Backgrounds

Background Color

.section {
    background-color: #f9fafb;
}

Background Image

.hero {
    background-image: url("images/hero.jpg");
}

Background Size

.hero {
    background-size: cover;
}

Background Position

.hero {
    background-position: center;
}

Background Repeat

.hero {
    background-repeat: no-repeat;
}

Background Overlay

.hero {
    background-image:
        linear-gradient(rgb(0 0 0 / 0.5), rgb(0 0 0 / 0.5)),
        url("images/hero.jpg");
}

Borders and Radius

Border

.card {
    border: 1px solid #ddd;
}

Border Radius

.card {
    border-radius: 12px;
}

Circle

.avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
}

Border Side

.alert {
    border-left: 4px solid #2563eb;
}

Shadows

Box Shadow

.card {
    box-shadow: 0 8px 24px rgb(0 0 0 / 0.12);
}

Text Shadow

.hero-title {
    text-shadow: 0 2px 8px rgb(0 0 0 / 0.4);
}

Use shadows sparingly. Strong shadows can make a design look heavy.

Width and Height

Fixed Width

.box {
    width: 300px;
}

Maximum Width

.container {
    max-width: 1200px;
    margin: 0 auto;
}

Minimum Height

.hero {
    min-height: 500px;
}

Full Viewport Height

.section {
    min-height: 100vh;
}

Responsive Image

img {
    max-width: 100%;
    height: auto;
}

Overflow

Hide Overflow

.box {
    overflow: hidden;
}

Scroll Overflow

.panel {
    max-height: 300px;
    overflow-y: auto;
}

Text Ellipsis

.title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

Transitions

Use transitions to animate property changes.

.button {
    background-color: #2563eb;
    transition: background-color 0.2s ease;
}

.button:hover {
    background-color: #1d4ed8;
}

Common Transition Properties

  • transition-property sets what changes.
  • transition-duration sets how long it takes.
  • transition-timing-function sets the speed curve.
  • transition-delay waits before starting.
.card {
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

Transforms

Move

.box {
    transform: translateY(-4px);
}

Scale

.button:hover {
    transform: scale(1.05);
}

Rotate

.icon {
    transform: rotate(45deg);
}

Combine Transforms

.card:hover {
    transform: translateY(-4px) scale(1.02);
}

Opacity and Visibility

Opacity

.faded {
    opacity: 0.5;
}

opacity affects the element and its children.

Visibility

.hidden {
    visibility: hidden;
}

The element becomes invisible but still takes up space.

Display None

.removed {
    display: none;
}

The element is removed from layout.

CSS Variables

CSS variables make repeated values easier to manage.

:root {
    --color-primary: #2563eb;
    --space-md: 16px;
    --radius-md: 12px;
}

Use variables with var().

.button {
    background-color: var(--color-primary);
    padding: var(--space-md);
    border-radius: var(--radius-md);
}

Variable Fallback

.button {
    color: var(--button-text, white);
}

The fallback value is used if the variable is not defined.

Media Queries

Use media queries to change styles at different screen sizes.

.card {
    padding: 16px;
}

@media (min-width: 768px) {
    .card {
        padding: 32px;
    }
}

Common Breakpoints

  • 480px for small phones and larger.
  • 768px for tablets and larger.
  • 1024px for laptops and larger.
  • 1280px for large screens.

Responsive Layout Example

.grid {
    display: grid;
    gap: 16px;
}

@media (min-width: 768px) {
    .grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 1024px) {
    .grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

Common CSS Functions

calc()

.sidebar {
    width: calc(100% - 250px);
}

min()

.title {
    font-size: min(8vw, 64px);
}

max()

.section {
    padding: max(24px, 5vw);
}

clamp()

h1 {
    font-size: clamp(2rem, 6vw, 4rem);
}

clamp() sets a minimum, preferred, and maximum value.

Common CSS Patterns

Center an Element

.container {
    display: flex;
    align-items: center;
    justify-content: center;
}

Center a Page Container

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 24px;
}

Create a Card

.card {
    padding: 24px;
    border: 1px solid #ddd;
    border-radius: 16px;
    background-color: white;
    box-shadow: 0 8px 24px rgb(0 0 0 / 0.08);
}

Create a Button

.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 12px 20px;
    border: 0;
    border-radius: 8px;
    background-color: #2563eb;
    color: white;
    font-weight: 600;
    cursor: pointer;
}

Create a Responsive Grid

.cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 24px;
}

Make a Sticky Header

.header {
    position: sticky;
    top: 0;
    z-index: 100;
    background-color: white;
}

Hide Content Visually but Keep It for Screen Readers

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

Specificity Basics

CSS specificity decides which rule wins when multiple rules target the same element.

Low Specificity

p {
    color: gray;
}

Medium Specificity

.text {
    color: blue;
}

Higher Specificity

#intro {
    color: red;
}

Inline Styles Are Stronger

<p style="color: green;">Text</p>

Avoid Overusing !important

.text {
    color: red !important;
}

Use !important only when you have a clear reason. It makes future changes harder.

CSS Reset

A small reset can make layouts more predictable.

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: Arial, sans-serif;
}

img {
    max-width: 100%;
    height: auto;
}

Common Mistakes

Forgetting the Semicolon

.card {
    color: blue
    background-color: white;
}

Better:

.card {
    color: blue;
    background-color: white;
}

Missing a Closing Brace

.card {
    padding: 24px;

Better:

.card {
    padding: 24px;
}

Using width: 100vw and Causing Horizontal Scroll

.section {
    width: 100vw;
}

Better:

.section {
    width: 100%;
}

Stretching Images

img {
    width: 400px;
    height: 200px;
}

Better:

img {
    width: 400px;
    height: auto;
}

Using Fixed Heights for Dynamic Content

.card {
    height: 200px;
}

Better:

.card {
    min-height: 200px;
}

Debugging Tips

Add a Temporary Outline

* {
    outline: 1px solid red;
}

This helps you see element boundaries.

Check Layout with DevTools

Use the browser inspector to check:

  • Which CSS rules apply
  • Which rules are overwritten
  • The box model
  • Flexbox and grid layout
  • Computed sizes
  • Active media queries

Test One Change at a Time

Change one property, refresh, and check the result. This makes CSS problems easier to isolate.

Quick Reference

Select by Element

p {
    color: #333;
}

Select by Class

.card {
    padding: 24px;
}

Select by ID

#hero {
    min-height: 500px;
}

Add Spacing

.box {
    margin: 24px;
    padding: 16px;
}

Center with Flexbox

.box {
    display: flex;
    align-items: center;
    justify-content: center;
}

Create a Grid

.grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 24px;
}

Make Images Responsive

img {
    max-width: 100%;
    height: auto;
}

Add a Media Query

@media (min-width: 768px) {
    .grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

Use a CSS Variable

:root {
    --primary: #2563eb;
}

.button {
    background-color: var(--primary);
}