React JS: CSS basics
HTML for React (5/5)
1 Overview
📘 CSS CSS (Cascading Style Sheets) describes how HTML elements are displayed: colours, fonts, spacing, size and layout. HTML gives the structure, CSS gives the look, JavaScript gives the behaviour.
In React we write the same CSS. The only difference is how we attach it to components: className, imported CSS files, style objects or CSS modules.
2 CSS syntax and where to put it
A CSS rule is a selector plus a block of declarations (property: value;):
There are three ways to add CSS to HTML:
- External file (the usual one):
<link rel="stylesheet" href="styles.css">in the<head>. - Internal: a
<style>block in the<head>. - Inline: a
styleattribute on one element:<p style="color:red">.
When several rules hit the same element, the cascade decides: more specific selectors win, and if they are equal, the last one wins. Inline styles beat stylesheet rules.
3 Selectors
| Selector | Example | Selects |
|---|---|---|
| Element | p |
All <p> elements |
| Class | .card |
All elements with class="card" |
| Id | #header |
The element with id="header" |
| Universal | * |
Every element |
| Group | h1, h2 |
All <h1> and all <h2> |
| Descendant | .card p |
<p> anywhere inside .card |
| Child | ul > li |
<li> that are direct children of <ul> |
| Attribute | input[type="email"] |
Inputs with that attribute value |
| Pseudo-class | a:hover, li:first-child, input:invalid |
Elements in a state or position |
| Pseudo-element | p::first-line, .req::after |
A part of an element |
3.1 class vs id
index.html
- An
idmust be unique in the page. Use it for one specific element (anchors#section,label for,getElementById). - A
classcan be reused on many elements, and one element can have several classes (class="card featured"). - For styling, prefer classes: they are reusable and have lower specificity, so they are easier to override. In React this means
className, and ids are rarely needed (remember that components can be rendered many times).
4 The box model
Every element is a box made of four layers, from inside out:
+------------------------------- margin -------------------------------+
| +---------------------------- border ----------------------------+ |
| | +------------------------- padding -------------------------+ | |
| | | content | | |
| | +-----------------------------------------------------------+ | |
| +-----------------------------------------------------------------+ |
+-----------------------------------------------------------------------+
- content: the text or image, sized with
widthandheight, - padding: space inside the border, it takes the background colour,
- border: the line around the padding,
- margin: space outside the border, transparent, it separates boxes.
styles.css
box-sizing: border-box
By default width is only the content, so a 300px card with 16px padding and a 1px border is really 334px wide. With border-box it stays 300px. Almost every project sets it globally.
5 Flexbox essentials
Flexbox lays out items in one dimension, a row or a column. You turn it on in the parent (the flex container) and the children become flex items.
styles.css
.navbar {
display: flex;
flex-direction: row; /* row (default) | column */
justify-content: space-between; /* main axis: flex-start | center | flex-end | space-between | space-around */
align-items: center; /* cross axis: stretch (default) | flex-start | center | flex-end */
gap: 16px; /* space between items */
flex-wrap: wrap; /* allow items to go to a new line */
}
.navbar .search {
flex: 1; /* this item grows to fill the free space */
}| Property | On | What it does |
|---|---|---|
display: flex |
container | Turns on Flexbox |
flex-direction |
container | Main axis: row or column |
justify-content |
container | Alignment along the main axis |
align-items |
container | Alignment along the cross axis |
gap |
container | Space between items |
flex-wrap |
container | Lets items wrap to new lines |
flex |
item | How an item grows/shrinks (flex: 1) |
The classic trick to centre anything: display: flex; justify-content: center; align-items: center; on the parent.
6 Grid (briefly)
CSS Grid lays out in two dimensions, rows and columns at the same time. Perfect for page layouts and card galleries:
styles.css
This creates as many 200px+ columns as fit, sharing the free space (1fr = one fraction). Rule of thumb: Flexbox for a row or column of things, Grid for a whole layout.
7 Styling in React
7.1 1. Import a CSS file and use className
The most common way with Vite: write a normal .css file and import it. The styles are global.
Card.css
7.2 2. Inline style object
Good for dynamic values computed in JavaScript (a width, a colour from data). Property names are camelCase and the value is an object:
ProgressBar.jsx
Inline styles can’t do :hover, media queries or animations, so don’t use them for everything.
7.3 3. CSS Modules
A file named *.module.css gets locally scoped class names: no clashes between components. Vite supports it out of the box.
Button.jsx
There are also libraries (Tailwind CSS, styled-components, component libraries like MUI). They all build on the CSS above. More in React JS App: styling.
1. HTML basics · 2. Forms · 3. Events and the DOM · 4. From HTML to JSX · 5. CSS basics
⬅️ Previous: From HTML to JSX