CSS Essentials
CSS controls presentation. Understand the box model first, then use Flexbox and Grid for layout, and finish with responsive design.
Start with simple selectors, colors, spacing, and typography. Practice with small, focused snippets.
/* Selectors */p { margin-bottom: 1rem; }#highlight { background: gold; }.note { color: #374151; }/* Typography */h1 { font-size: 2rem; line-height: 1.2; }body { font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; }/* Colors, borders, radius */.box {color: #111827;background: #f3f4f6;border: 1px solid #e5e7eb;border-radius: 8px;padding: 12px;}/* Display */.inline { display: inline; }.block { display: block; }.inline-block { display: inline-block; }
CSS provides multiple ways to specify colors and create beautiful backgrounds. Use modern color functions and understand different color formats for better design control.
/* Complete color and background example */.color-showcase {/* Background with multiple layers */background:linear-gradient(135deg, rgba(59, 130, 246, 0.1), rgba(16, 185, 129, 0.1)),url('/pattern.png') repeat,#ffffff;background-size: cover, 20px 20px, auto;/* Border with gradient */border: 3px solid;border-image: linear-gradient(45deg, #3b82f6, #10b981) 1;border-radius: 12px;/* Text styling */color: #1f2937;padding: 2rem;/* Box shadow for depth */box-shadow:0 4px 6px -1px rgba(0, 0, 0, 0.1),0 2px 4px -1px rgba(0, 0, 0, 0.06);}/* Color scheme using CSS custom properties */:root {--color-primary: #3b82f6;--color-secondary: #10b981;--color-accent: #f59e0b;--color-neutral-50: #f9fafb;--color-neutral-900: #111827;}.theme-aware {color: var(--color-neutral-900);background-color: var(--color-neutral-50);border: 2px solid var(--color-primary);accent-color: var(--color-accent); /* For form controls */}
Typography is crucial for readability and visual hierarchy. Master font properties, text alignment, and spacing for professional-looking designs.
/* Typography scale and hierarchy */.typography-scale {/* Base font settings */font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;line-height: 1.6;color: #1f2937;}/* Heading scale */.heading-1 {font-size: 2.25rem; /* 36px */font-weight: 800;line-height: 1.2;margin-bottom: 1rem;color: #111827;}.heading-2 {font-size: 1.875rem; /* 30px */font-weight: 700;line-height: 1.3;margin-bottom: 0.75rem;color: #374151;}.heading-3 {font-size: 1.5rem; /* 24px */font-weight: 600;line-height: 1.4;margin-bottom: 0.5rem;color: #4b5563;}/* Body text variations */.body-large {font-size: 1.125rem; /* 18px */line-height: 1.7;margin-bottom: 1rem;}.body-normal {font-size: 1rem; /* 16px */line-height: 1.6;margin-bottom: 0.75rem;}.body-small {font-size: 0.875rem; /* 14px */line-height: 1.5;color: #6b7280;}/* Special text styles */.text-highlight {background: linear-gradient(120deg, #fbbf24 0%, #f59e0b 100%);background-repeat: no-repeat;background-size: 100% 0.2em;background-position: 0 88%;transition: background-size 0.25s ease-in;}.text-highlight:hover {background-size: 100% 88%;}/* Responsive typography */@media (min-width: 768px) {.heading-1 { font-size: 3rem; }.heading-2 { font-size: 2.25rem; }.heading-3 { font-size: 1.875rem; }}
Every element is a rectangular box made up of content, padding, border, and margin. The box-sizing property controls how width/height are calculated.
/* Use border-box to make sizing intuitive */*, *::before, *::after { box-sizing: border-box; }/* Visual spacing */.card {padding: 1rem; /* space inside */border: 1px solid var(--color-border);margin: 1rem; /* space outside */}
CSS positioning controls where elements appear on the page. Understand static, relative, absolute, fixed, and sticky positioning for precise element placement.
/* Advanced positioning example */.modal-overlay {position: fixed;top: 0;left: 0;right: 0;bottom: 0;background: rgba(0, 0, 0, 0.5);display: flex;align-items: center;justify-content: center;z-index: 1000;}.modal {position: relative;background: white;border-radius: 12px;padding: 2rem;max-width: 90vw;max-height: 90vh;box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);}.close-button {position: absolute;top: 1rem;right: 1rem;background: #f3f4f6;border: none;border-radius: 50%;width: 32px;height: 32px;cursor: pointer;}/* Sticky navigation */.sticky-nav {position: sticky;top: 0;background: rgba(255, 255, 255, 0.9);backdrop-filter: blur(10px);border-bottom: 1px solid #e5e7eb;padding: 1rem;z-index: 100;}/* Centering techniques */.center-absolute {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);}.center-flex {display: flex;align-items: center;justify-content: center;min-height: 200px;}.center-grid {display: grid;place-items: center;min-height: 200px;}
Flexbox arranges items along a single axis with powerful alignment controls. Use it for toolbars, navbars, and one‑dimensional layouts.
.toolbar {display: flex; /* enable flex layout */gap: 0.5rem; /* spacing between items */justify-content: space-between; /* main-axis alignment */align-items: center; /* cross-axis alignment */}.item { flex: 1 } /* items can grow to fill space */
Grid is for two‑dimensional layouts. Define rows and columns and place items precisely.
.gallery {display: grid;grid-template-columns: repeat(3, 1fr);gap: 1rem;}/* Place an item to span two columns on row 1 */.feature {grid-column: 1 / span 2;grid-row: 1;}
Use relative units, fluid containers, and media queries. Start mobile‑first, then scale up.
/* Mobile-first styles */.container { max-width: 100%; padding-inline: 1rem; }/* Enhance at larger screens */@media (min-width: 768px) {.container { max-width: 60ch; margin-inline: auto; }}/* Modern container query example */.card { container-type: inline-size; }@container (min-width: 420px) {.card { display: grid; grid-template-columns: 1fr auto; gap: 1rem; }}
- Design mobile-first, then add breakpoints with
@media. - Use flexible units like
fr,%, andrem. - Set images to
max-width: 100%andheight: auto.