CSS
Cheatsheet

Quick reference for essential CSS properties and patterns. Click any code block to copy.

How to Include CSS

<!-- External stylesheet (BEST) -->
<link rel="stylesheet" href="style.css">

<!-- Internal stylesheet (OK for testing) -->
<style>
    body { margin: 0; }
</style>

<!-- Inline styles (AVOID) -->
<p style="color: red;">Text</p>

Selectors

/* Element selector */
p { color: blue; }

/* Class selector */
.classname { color: red; }

/* ID selector */
#idname { color: green; }

/* Multiple selectors */
h1, h2, h3 { font-weight: bold; }

/* Descendant selector */
nav a { text-decoration: none; }

/* Direct child */
ul > li { list-style: none; }

/* Pseudo-classes */
a:hover { color: red; }
a:focus { outline: 2px solid blue; }

/* Pseudo-elements */
p::first-line { font-weight: bold; }
p::before { content: "→ "; }

Colors

.element {
    /* Named colors */
    color: red;
    
    /* Hex */
    color: #ff0000;
    color: #f00;  /* Shorthand */
    
    /* RGB */
    color: rgb(255, 0, 0);
    
    /* RGBA (with transparency) */
    color: rgba(255, 0, 0, 0.5);
    
    /* HSL */
    color: hsl(0, 100%, 50%);
}

Typography

.text {
    /* Font family */
    font-family: Arial, Helvetica, sans-serif;
    
    /* Size */
    font-size: 16px;
    font-size: 1rem;   /* Relative to root */
    font-size: 1.2em;  /* Relative to parent */
    
    /* Weight */
    font-weight: normal;  /* 400 */
    font-weight: bold;    /* 700 */
    
    /* Line height */
    line-height: 1.6;
    
    /* Letter spacing */
    letter-spacing: 0.05em;
    
    /* Text alignment */
    text-align: left;
    text-align: center;
    text-align: right;
    
    /* Text decoration */
    text-decoration: none;
    text-decoration: underline;
    
    /* Text transform */
    text-transform: uppercase;
    text-transform: lowercase;
    text-transform: capitalize;
}

Box Model

.box {
    /* Width & Height */
    width: 300px;
    height: 200px;
    max-width: 100%;
    min-height: 400px;
    
    /* Padding (space inside) */
    padding: 20px;                    /* All sides */
    padding: 10px 20px;               /* Top/bottom, left/right */
    padding: 10px 20px 30px 40px;     /* Top, right, bottom, left */
    
    /* Margin (space outside) */
    margin: 20px;
    margin: 0 auto;  /* Center horizontally */
    
    /* Border */
    border: 1px solid black;
    border-radius: 10px;  /* Rounded corners */
    
    /* Box sizing (always use this!) */
    box-sizing: border-box;
}

Background

.element {
    /* Color */
    background-color: lightblue;
    
    /* Image */
    background-image: url('image.jpg');
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
    
    /* Shorthand */
    background: #fff url('image.jpg') no-repeat center/cover;
}

Display & Position

.element {
    /* Display */
    display: block;        /* Takes full width */
    display: inline;       /* Flows with text */
    display: inline-block; /* Hybrid */
    display: none;         /* Hides element */
    display: flex;         /* Flexbox */
    display: grid;         /* Grid */
    
    /* Position */
    position: static;      /* Default */
    position: relative;    /* Relative to normal position */
    position: absolute;    /* Relative to positioned ancestor */
    position: fixed;       /* Relative to viewport */
    position: sticky;      /* Hybrid of relative and fixed */
    
    /* Positioning properties */
    top: 10px;
    right: 20px;
    bottom: 30px;
    left: 40px;
    
    /* Z-index (stacking order) */
    z-index: 10;
}

Flexbox

.container {
    display: flex;
    
    /* Direction */
    flex-direction: row;      /* Left to right (default) */
    flex-direction: column;   /* Top to bottom */
    
    /* Wrap */
    flex-wrap: wrap;
    
    /* Justify content (main axis) */
    justify-content: flex-start;
    justify-content: center;
    justify-content: flex-end;
    justify-content: space-between;
    justify-content: space-around;
    justify-content: space-evenly;
    
    /* Align items (cross axis) */
    align-items: stretch;
    align-items: flex-start;
    align-items: center;
    align-items: flex-end;
    
    /* Gap */
    gap: 20px;
}

.flex-item {
    flex: 1;  /* Grow to fill space */
    align-self: center;  /* Align individual item */
}

Grid

.grid-container {
    display: grid;
    
    /* Columns */
    grid-template-columns: 200px 1fr 1fr;
    grid-template-columns: repeat(3, 1fr);
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    
    /* Rows */
    grid-template-rows: auto 1fr auto;
    
    /* Gap */
    gap: 20px;
    
    /* Alignment */
    justify-items: center;
    align-items: center;
}

.grid-item {
    grid-column: span 2;  /* Span 2 columns */
    grid-row: 1 / 3;      /* From row 1 to 3 */
}

Transitions

.element {
    /* Basic transition */
    transition: all 0.3s ease;
    
    /* Specific property */
    transition: background-color 0.3s ease;
    
    /* Multiple properties */
    transition: 
        background-color 0.3s ease,
        transform 0.2s ease;
    
    /* Timing functions */
    transition-timing-function: ease;
    transition-timing-function: linear;
    transition-timing-function: ease-in-out;
}

Transforms

.element {
    /* Translate (move) */
    transform: translateX(50px);
    transform: translateY(-20px);
    transform: translate(50px, -20px);
    
    /* Scale (resize) */
    transform: scale(1.5);
    
    /* Rotate */
    transform: rotate(45deg);
    
    /* Combine multiple */
    transform: translateY(-10px) rotate(5deg) scale(1.1);
}

Animations

/* Define keyframes */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Apply animation */
.element {
    animation: fadeIn 1s ease forwards;
    
    /* Or with all properties */
    animation-name: fadeIn;
    animation-duration: 1s;
    animation-timing-function: ease;
    animation-delay: 0.5s;
    animation-iteration-count: infinite;
    animation-fill-mode: forwards;
}

Responsive Design

/* Mobile first approach */
.element {
    font-size: 16px;
}

/* Tablet and up */
@media (min-width: 768px) {
    .element {
        font-size: 18px;
    }
}

/* Desktop and up */
@media (min-width: 1024px) {
    .element {
        font-size: 20px;
    }
}

/* Print styles */
@media print {
    body {
        color: black;
        background: white;
    }
}

CSS Variables

:root {
    --primary-color: #0066cc;
    --secondary-color: #333;
    --spacing: 20px;
    --font-main: 'Inter', sans-serif;
}

.element {
    color: var(--primary-color);
    padding: var(--spacing);
    font-family: var(--font-main);
}

/* With fallback */
.element {
    color: var(--primary-color, blue);
}

Common Patterns

Center Anything

/* Horizontal center (block element) */
.element {
    margin: 0 auto;
    max-width: 800px;
}

/* Center with Flexbox */
.container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}

/* Center with Grid */
.container {
    display: grid;
    place-items: center;
    min-height: 100vh;
}

Responsive Container

.container {
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
}

Responsive Images

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

Units Reference

Unit Description Example
px Pixels (absolute) width: 300px;
% Percent of parent width: 50%;
em Relative to parent font size padding: 1.5em;
rem Relative to root font size font-size: 1.2rem;
vw Viewport width width: 50vw;
vh Viewport height height: 100vh;
ch Width of "0" character max-width: 70ch;

Reset / Normalize

/* Simple reset */
*, *::before, *::after {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: sans-serif;
    line-height: 1.6;
}

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

Specificity

Order of priority (highest to lowest):

Priority Selector Example
1 !important color: red !important; (avoid!)
2 Inline styles style="color: red;"
3 IDs #unique { }
4 Classes, attributes, pseudo-classes .class { }, :hover { }
5 Elements p { }, div { }

Quick Starter Template

/* Reset */
*, *::before, *::after {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 16px;
    line-height: 1.6;
    color: #333;
}

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

a {
    color: #0066cc;
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

/* Container */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
}
Pro tip: When stuck, open DevTools (F12) and experiment with CSS in real-time!