Part 1: Syntax, Selectors, Box Model, Typography, Colors, Units & Variables
A CSS rule consists of a selector and a declaration block.
selector {
property: value;
property2: value2;
}
CSS supports only block comments. No single-line comments.
/* This is a comment */
p { color: red; /* inline comment */ }
<!-- 1. Inline Style -->
<p style="color:red">Text</p>
<!-- 2. Internal Style -->
<style>
p { color: red; }
</style>
<!-- 3. External Style -->
<link rel="stylesheet" href="style.css">
1. Browser default styles
2. External stylesheet
3. Internal stylesheet (<style>)
4. Inline styles (style="...")
5. !important declarations
6. !important in inline styles
* { } /* Universal - selects ALL elements */
div { } /* Type - selects all <div> */
.class { } /* Class - selects class="class" */
#id { } /* ID - selects id="id" */
div, p { } /* Grouping - selects all <div> AND <p> */
[href] /* Has attribute */
[href="url"] /* Exact match */
[class~="box"] /* Contains word "box" */
[lang|="en"] /* Starts with "en" or "en-" */
[href^="https"] /* Starts with "https" */
[src$=".png"] /* Ends with ".png" */
[title*="hello"] /* Contains "hello" anywhere */
[type="text" i] /* Case-insensitive match */
div p { } /* Descendant - p inside div (any depth) */
div > p { } /* Child - p directly inside div */
div + p { } /* Adjacent Sibling - p right after div */
div ~ p { } /* General Sibling - all p after div */
/* Examples */
ul > li /* Direct list items only */
h1 + p /* First paragraph after h1 */
.card ~ .card /* All cards after the first */
a:hover { } /* Mouse over element */
a:active { } /* While being clicked */
a:visited { } /* Already visited link */
a:link { } /* Unvisited link */
input:focus { } /* Element has keyboard focus */
input:focus-visible { } /* Keyboard focus (not mouse) */
div:focus-within { } /* Contains a focused element */
input:checked { } /* Checked checkbox/radio */
input:disabled { } /* Disabled form element */
input:enabled { } /* Enabled form element */
input:required { } /* Has required attribute */
input:optional { } /* Does NOT have required */
input:valid { } /* Passes validation */
input:invalid { } /* Fails validation */
input:placeholder-shown { } /* Placeholder is visible */
:target { } /* Element matching URL hash #id */
:root { } /* Document root (<html>) */
:first-child /* First child of its parent */
:last-child /* Last child of its parent */
:only-child /* Only child of its parent */
:first-of-type /* First of its type in parent */
:last-of-type /* Last of its type in parent */
:only-of-type /* Only one of its type in parent */
:empty /* No children (including text) */
/* nth-child patterns */
:nth-child(3) /* 3rd child */
:nth-child(odd) /* 1st, 3rd, 5th... = :nth-child(2n+1) */
:nth-child(even) /* 2nd, 4th, 6th... = :nth-child(2n) */
:nth-child(3n) /* Every 3rd: 3, 6, 9... */
:nth-child(3n+1) /* 1, 4, 7, 10... */
:nth-child(-n+3) /* First 3 children */
:nth-child(n+4) /* From 4th child onward */
:nth-last-child(2) /* 2nd from end */
:nth-of-type(2) /* 2nd of its type */
:nth-last-of-type(1) /* Last of its type */
/* :is() - matches any in the list (forgiving) */
:is(h1, h2, h3) { color: blue; }
/* Same as: h1, h2, h3 { color: blue; } */
/* Nested with :is() */
article :is(h1, h2, h3) { margin-top: 2em; }
/* :where() - like :is() but ZERO specificity */
:where(h1, h2, h3) { color: blue; }
/* :not() - negation */
p:not(.special) { color: gray; }
div:not(:first-child) { margin-top: 10px; }
:not(p, span) { } /* Exclude multiple */
/* :has() - parent selector! */
div:has(> img) { padding: 0; }
form:has(:invalid) { border: 2px solid red; }
h1:has(+ p) { margin-bottom: 0; }
::before /* Insert content before */
::after /* Insert content after */
::first-line /* First line of text */
::first-letter /* First letter of text */
::selection /* User-selected text */
::placeholder /* Input placeholder text */
::marker /* List item bullets/numbers */
/* Example: Adding decorative content */
.quote::before {
content: "\201C"; /* Left double quote */
font-size: 3em;
color: var(--primary);
}
/* Clearfix with ::after */
.clearfix::after {
content: "";
display: table;
clear: both;
}
Specificity is calculated as a 4-part score: inline, IDs, Classes, Elements
| Selector | Specificity | Score |
|---|---|---|
* | 0, 0, 0, 0 | 0 |
p | 0, 0, 0, 1 | 1 |
p.class | 0, 0, 1, 1 | 11 |
.class | 0, 0, 1, 0 | 10 |
#id | 0, 1, 0, 0 | 100 |
#id .class p | 0, 1, 1, 1 | 111 |
style="..." | 1, 0, 0, 0 | 1000 |
!important | Overrides everything (except other !important) | |
/*
+------------------------------+
| MARGIN |
| +--------------------------+ |
| | BORDER | |
| | +--------------------+ | |
| | | PADDING | | |
| | | +--------------+ | | |
| | | | CONTENT | | | |
| | | +--------------+ | | |
| | +--------------------+ | |
| +--------------------------+ |
+------------------------------+
*/
.box {
width: 200px;
height: 100px;
padding: 20px;
border: 2px solid #333;
margin: 10px;
}
/* content-box (DEFAULT):
Total width = width + padding + border
200 + 40 + 4 = 244px */
.box {
box-sizing: content-box;
width: 200px;
padding: 20px;
border: 2px solid;
}
/* border-box:
Total width = width (includes padding + border)
200px total */
.box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 2px solid;
}
/* BEST PRACTICE: Apply to all elements */
*, *::before, *::after {
box-sizing: border-box;
}
/* Individual sides */
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
/* Shorthand: top right bottom left (clockwise) */
margin: 10px 20px 10px 20px;
/* Shorthand: top-bottom left-right */
margin: 10px 20px;
/* Shorthand: top left-right bottom */
margin: 10px 20px 30px;
/* All sides same */
margin: 10px;
/* Auto centering (needs width) */
margin: 0 auto;
/* Negative margins (pull element) */
margin-top: -20px;
/* Padding - same shorthand as margin */
padding: 10px 20px;
/* Border */
border: 2px solid #333; /* width style color */
border-top: 3px dashed red;
border-radius: 10px; /* Rounded corners */
border-radius: 50%; /* Circle */
border-radius: 10px 0 10px 0; /* TL TR BR BL */
/* Border styles: */
/* solid | dashed | dotted | double |
groove | ridge | inset | outset | none */
/* Outline (doesn't affect layout!) */
outline: 2px solid blue;
outline-offset: 4px; /* Space from border */
width: 300px;
height: 200px;
min-width: 100px;
max-width: 800px;
min-height: 50px;
max-height: 600px;
/* Viewport units */
width: 100vw; /* Full viewport width */
height: 100vh; /* Full viewport height */
/* Intrinsic sizing */
width: fit-content;
width: min-content;
width: max-content;
width: auto;
/* Generic families */
font-family: serif;
font-family: sans-serif;
font-family: monospace;
font-family: cursive;
font-family: fantasy;
/* Font stack (fallback chain) */
font-family: 'Helvetica Neue', Arial, sans-serif;
/* System font stack (native look) */
font-family: system-ui, -apple-system,
'Segoe UI', Roboto, sans-serif;
/* Monospace stack */
font-family: 'Cascadia Code', 'Fira Code',
'JetBrains Mono', monospace;
/* Font size */
font-size: 16px; /* Absolute */
font-size: 1.2em; /* Relative to parent */
font-size: 1.2rem; /* Relative to root */
font-size: 120%; /* Percent of parent */
font-size: clamp(14px, 2vw, 22px); /* Fluid */
/* Font weight */
font-weight: normal; /* = 400 */
font-weight: bold; /* = 700 */
font-weight: 100; /* Thin */
font-weight: 300; /* Light */
font-weight: 500; /* Medium */
font-weight: 900; /* Black */
/* Font style */
font-style: normal;
font-style: italic;
font-style: oblique 14deg;
text-align: left | center | right | justify;
text-decoration: none | underline | overline | line-through;
text-transform: uppercase | lowercase | capitalize | none;
text-indent: 2em;
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
/* Spacing */
line-height: 1.6; /* Unitless (recommended) */
letter-spacing: 0.05em;
word-spacing: 0.1em;
/* Overflow handling */
text-overflow: ellipsis; /* Needs overflow:hidden */
white-space: nowrap;
word-break: break-all;
overflow-wrap: break-word;
hyphens: auto;
/* Font shorthand:
font: style weight size/line-height family */
font: italic 700 16px/1.5 Arial, sans-serif;
font: bold 2em/1.4 Georgia, serif;
/* @font-face - load custom fonts */
@font-face {
font-family: 'MyFont';
src: url('font.woff2') format('woff2'),
url('font.woff') format('woff');
font-weight: 400;
font-style: normal;
font-display: swap; /* Show fallback while loading */
}
/* Truncate text with ellipsis */
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
/* Multi-line truncate */
.line-clamp {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
/* Named Colors */
color: red;
color: tomato;
color: transparent; /* Fully transparent */
color: currentColor; /* Inherits current text color */
/* Hexadecimal */
color: #ff6347; /* 6-digit hex */
color: #f63; /* 3-digit shorthand (= #ff6633) */
color: #ff634780; /* 8-digit hex with alpha */
color: #f637; /* 4-digit shorthand with alpha */
/* RGB / RGBA */
color: rgb(255, 99, 71);
color: rgba(255, 99, 71, 0.5); /* 50% opacity */
color: rgb(255 99 71 / 50%); /* Modern syntax */
/* HSL / HSLA (Hue, Saturation, Lightness) */
color: hsl(9, 100%, 64%); /* Hue: 0-360, S & L: 0-100% */
color: hsla(9, 100%, 64%, 0.5);
color: hsl(9 100% 64% / 50%); /* Modern syntax */
/* Opacity (affects entire element + children) */
opacity: 0.5; /* 0 = invisible, 1 = fully visible */
| Name | Hex | Chars | Name | Hex | Chars |
|---|---|---|---|---|---|
red | #ff0000 | 3 | tan | #d2b48c | 3 |
gold | #ffd700 | 4 | cyan | #00ffff | 4 |
blue | #0000ff | 4 | gray | #808080 | 4 |
lime | #00ff00 | 4 | navy | #000080 | 4 |
pink | #ffc0cb | 4 | peru | #cd853f | 4 |
plum | #dda0dd | 4 | teal | #008080 | 4 |
aqua | #00ffff | 4 | snow | #fffafa | 4 |
coral | #ff7f50 | 5 | wheat | #f5deb3 | 5 |
olive | #808000 | 5 | ivory | #fffff0 | 5 |
khaki | #f0e68c | 5 | linen | #faf0e6 | 5 |
| Unit | Type | Description | Example |
|---|---|---|---|
px | Absolute | Pixels (1/96 of 1 inch) | font-size: 16px |
em | Relative | Relative to parent's font-size | padding: 1.5em |
rem | Relative | Relative to root (<html>) font-size | margin: 2rem |
% | Relative | Relative to parent's size | width: 50% |
vw | Viewport | 1% of viewport width | width: 100vw |
vh | Viewport | 1% of viewport height | height: 100vh |
vmin | Viewport | 1% of smaller viewport dimension | font-size: 5vmin |
vmax | Viewport | 1% of larger viewport dimension | width: 50vmax |
ch | Relative | Width of the "0" character | max-width: 60ch |
ex | Relative | Height of the "x" character | margin: 1ex |
fr | Grid | Fraction of available space (grid only) | grid-template-columns: 1fr 2fr |
cm | Absolute | Centimeters | width: 10cm |
mm | Absolute | Millimeters | width: 100mm |
in | Absolute | Inches (1in = 96px) | width: 2in |
pt | Absolute | Points (1pt = 1/72 inch) | font-size: 12pt |
pc | Absolute | Picas (1pc = 12pt) | font-size: 1pc |
/* Mix different units */
width: calc(100% - 40px);
height: calc(100vh - 60px);
font-size: calc(1rem + 0.5vw);
margin: calc(var(--gap) * 2);
/* Operators: + - * / */
/* IMPORTANT: spaces required around + and - */
width: calc(100%/3); /* OK */
width: calc(100% - 10px); /* OK */
width: calc(100%-10px); /* BROKEN! */
/* Nesting */
width: calc(100% - calc(2 * 20px));
/* min() - picks the smallest value */
width: min(90%, 800px);
/* At most 800px, but shrinks on small screens */
/* max() - picks the largest value */
width: max(300px, 50%);
/* At least 300px, grows on large screens */
/* clamp(min, preferred, max) */
font-size: clamp(14px, 2.5vw, 24px);
/* Never below 14px, never above 24px,
scales with viewport in between */
width: clamp(300px, 50%, 800px);
/* Responsive width with min/max bounds */
/* Declare on :root for global scope */
:root {
--primary: #6c63ff;
--text-color: #333;
--spacing: 16px;
--font: 'Inter', sans-serif;
--shadow: 0 2px 10px rgba(0,0,0,0.1);
}
/* Use with var() */
.card {
color: var(--text-color);
padding: var(--spacing);
font-family: var(--font);
box-shadow: var(--shadow);
}
/* With fallback value */
color: var(--undefined, #000);
color: var(--a, var(--b, red));
/* Variables are inherited */
:root {
--bg: white;
--text: black;
}
/* Override in a scope */
.dark-theme {
--bg: #1a1a2e;
--text: #e4e4e7;
}
/* Media query overrides */
@media (prefers-color-scheme: dark) {
:root {
--bg: #1a1a2e;
--text: #e4e4e7;
}
}
/* Use in calc() */
.box {
padding: calc(var(--spacing) * 2);
margin-bottom: calc(var(--spacing) / 2);
}
/* Use in shorthand */
.box {
border: 2px solid var(--primary);
background: var(--bg);
}