Basics
CSS (Cascading Style Sheets) controls the visual presentation of HTML documents layout, colors, typography, and animations.
Linking CSS
<link rel="stylesheet" href="style.css" />
Selectors
elementType selector (h1,p).classClass selector#idID selector[attr]Attribute selector:hover,:focus,:nth-child()Pseudo-classes::before,::afterPseudo-elements
Flexbox
Flexbox is a one-dimensional layout method for arranging items in rows or columns.
Flexbox Container
.container {
display: flex;
justify-content: center; /* main axis */
align-items: center; /* cross axis */
gap: 1rem;
flex-wrap: wrap;
}
.item {
flex: 1; /* grow equally */
flex-basis: 200px; /* starting width */
}
Key Properties
flex-direction: row | column | row-reverse | column-reversejustify-content: flex-start | center | space-between | space-around | space-evenlyalign-items: stretch | center | flex-start | flex-end | baselineflex-wrap: nowrap | wrap | wrap-reversegap: spacing between flex items
CSS Grid
CSS Grid is a two-dimensional layout system perfect for complex page layouts.
Grid Layout
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
gap: 2rem;
}
.span-2 {
grid-column: span 2;
}
Custom Properties (Variables)
CSS Variables
:root {
--primary: #c4f026;
--bg: #0f0e0c;
--text: #f0ece6;
--radius: 12px;
}
.card {
background: var(--bg);
color: var(--text);
border-radius: var(--radius);
}
Animations & Transitions
Transitions & Keyframes
/* Transition */
.button {
transition: transform 0.3s ease, background 0.3s;
}
.button:hover {
transform: scale(1.05);
}
/* Keyframe Animation */
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.element {
animation: fadeIn 0.6s ease forwards;
}
Best Practices
- Use CSS custom properties for theming
- Mobile-first approach with
min-widthmedia queries - Prefer
remandemoverpxfor scalability - Use Flexbox for 1D layouts, Grid for 2D layouts
- Minimise specificity avoid
!important - Use
clamp()for fluid typography - Leverage
gapinstead of margins for spacing in flex/grid
