CSS Basics: Selectors, Layout, and Mobile-Friendly Pages
Select elements with selectors, reason about spacing with the box model, and lay things out with Flexbox and Grid. Includes fitting the screen width and a common sizing mistake.
Contents
HTML puts your content on the page, but on its own it cannot fine-tune spacing or layout. With CSS, you keep the same HTML and adjust how strong a heading looks or how cards line up.
This page covers how to tell which elements a rule applies to, and how to rearrange things to fit the screen.
1. Selectors decide "where"
A CSS rule pairs "what to select" with "what to change." An element name selects every tag of that type, and a class such as .notice selects elements with that class.
.notice {
color: #24324a;
background: #eef4ff;
}
.notice strong {
font-weight: 700;
}
button:hover {
background: #dbeafe;
}.notice strong selects a strong inside a notice, and :hover is the state when the mouse is over an element. When rules conflict, the more specific one wins. If they are equally specific, the one written later wins.
2. The box model adds layers around the content
By default, an element's width is the content width, with padding and border added on top. Margin sits outside that. With box-sizing: border-box, the width you set includes padding and border.
* {
box-sizing: border-box;
}
.card {
width: 280px;
padding: 16px;
border: 2px solid #789;
margin: 12px;
}The card in this example is 280px wide, border included. Instead of margins, you can also manage the space between cards with gap on the parent.
3. Flexbox lines things up in one direction
To line items up in a single row or column, set display: flex on the parent. The children follow the free space; justify-content aligns them along the main direction, and align-items along the cross direction.
.toolbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}In a row, the main direction is left to right. Switch to flex-direction: column and the main direction becomes top to bottom.
4. Grid handles rows and columns together
For a list of cards with a set number of columns, Grid is handy. repeat repeats the same column, and minmax(0, 1fr) gives each column an equal share.
.cards {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 16px;
}Flexbox and Grid are not rivals. Choose by the shape of the layout: Flexbox for a row of navigation links, Grid for a multi-column list.
5. Media queries adapt to phones
Rules that change columns or font sizes only on narrow screens are called media queries. The example below switches the cards to one column at 640px wide or less.
.cards {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 16px;
}
@media (max-width: 640px) {
.cards {
grid-template-columns: 1fr;
}
}Lots of fixed widths make content overflow on small screens. Prefer values that follow the parent's width, and add breakpoints where you actually need them.
A common mistake, and the fix
Even with width set to 100%, the default box calculation adds padding and border outside that width. You meant to fill the parent and end up overflowing sideways.
.card {
width: 100%;
padding: 24px;
border: 2px solid #789;
}* {
box-sizing: border-box;
}
.card {
width: 100%;
padding: 24px;
border: 2px solid #789;
}Applying border-box to every element makes width math much easier to predict.
Where this site uses it
The Chinese grammar map arranges its cards and control panel to organize the information. CSS spaces the content for easy reading and fits it to the screen width.
Read next
Page structure is covered in HTML basics, and what happens after a button is clicked in JavaScript basics.
When a layout breaks, picture each element's box one at a time. Separating width, padding, and border makes the cause much easier to find.
⚠️ These notes were written for my own study. They do not guarantee accuracy or that you will pass an exam. Please don’t rely on them alone — check official sources and other materials yourself.