CSS: reference
Page styling: selectors, the box model and flexbox.
Basics
A rule
A rule consists of a selector and a block of properties in {}. Every property ends with a semicolon.
h1 {
color: #2F5BEA;
font-size: 2rem;
}Linking
The best way is a separate stylesheet file.
<link rel="stylesheet" href="style.css">
<style>
p { margin: 0 0 1rem; }
</style>
<p style="color: red">Inline style</p>Comments
CSS has no one-line // comments.
/* comment in CSS */
Selectors
Basic
Classes are used more often than id: they can be repeated.
p { } /* by tag */
.card { } /* by class */
#header { } /* by id (unique) */
* { } /* all elements */
a, button { } /* several selectors */Combinators
A space means “any nested element”.
.menu a { } /* all a inside .menu */
.menu > li { } /* direct children only */
h2 + p { } /* p immediately after h2 */Pseudo-classes
A pseudo-class describes the state or position of an element.
a:hover { color: tomato; }
input:focus { outline: 2px solid #2F5BEA; }
li:first-child { font-weight: 700; }
li:nth-child(2n) { background: #eee; }Colours and units
Writing a colour
rgba and hsl let you set transparency and hue.
color: red; color: #2F5BEA; color: rgb(47, 91, 234); color: rgba(0, 0, 0, 0.5); color: hsl(220, 80%, 55%);
Units of measurement
rem and em are handy for fonts and spacing.
width: 300px; /* pixels */ font-size: 1.2rem; /* relative to the page font size */ padding: 2em; /* relative to the element's font */ width: 50%; /* percentage of the parent */ height: 100vh; /* window height */
Text
Font and alignment
The list of fonts in font-family is fallback options, in order.
body {
font-family: "Inter", Arial, sans-serif;
font-size: 16px;
font-weight: 400;
line-height: 1.6;
text-align: left;
text-decoration: none;
text-transform: uppercase;
}The box model
Spacing, border, size
The order from the inside out: content, padding, border, margin.
.box {
width: 300px;
padding: 16px; /* inside */
border: 2px solid #333;
margin: 20px auto; /* outside; auto centres */
box-sizing: border-box; /* the width includes padding and border */
}Shorthand
Four values go clockwise starting from the top.
margin: 10px; /* all sides */ margin: 10px 20px; /* top-bottom, left-right */ margin: 10px 20px 30px 40px; /* top, right, bottom, left */ border-radius: 8px;
Flexbox
A row with alignment
Flexbox lays elements out in a row or a column. flex-direction: column changes the direction.
.row {
display: flex;
gap: 16px;
justify-content: space-between; /* along the main axis */
align-items: center; /* along the cross axis */
flex-wrap: wrap;
}
.row .item { flex: 1; }Grid
Grid
1fr is a share of the free space. Grid is handy for two-dimensional layouts.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}
.wide { grid-column: span 2; }Positioning
position
absolute is positioned relative to the nearest ancestor whose position is not static.
.parent { position: relative; }
.badge {
position: absolute;
top: 8px;
right: 8px;
}
.header { position: sticky; top: 0; z-index: 10; }Responsiveness and animations
Media query
A block inside @media works only on screens that match the condition.
.card { width: 33%; }
@media (max-width: 600px) {
.card { width: 100%; }
}Variables and transition
--name variables are set once and substituted with var().
:root { --accent: #2F5BEA; }
.btn {
background: var(--accent);
transition: transform 0.2s ease;
}
.btn:hover { transform: translateY(-2px); }Learn CSS with practice
- 40 lessons, 35 of them base
- Tasks with in-browser code checking
- Free, nothing to install
Nearby in «Web technologies»: HTML · JavaScript · TypeScript