CSS stands for Cascading Style Sheets. While HTML structures a webpage's content, CSS controls how that content looks — colors, fonts, spacing, layout, and more. Without CSS, every website would look like plain black text on a white background.
HTML and CSS work as a team — HTML builds the skeleton, CSS dresses it up. Once you know HTML, learning CSS is the natural next step to actually make your pages look good instead of just functional.
This page covers the essential CSS fundamentals every beginner needs.
A selector tells CSS which HTML elements to style. There are several ways to select elements, depending on how specific you need to be.
Targets every element of that tag on the page.
p {
color: blue;
}
Targets elements with a specific class attribute.
Written with a dot (.) before the name. Classes can be
reused on multiple elements.
A class in HTML is a name you give to one or more elements so you can style them using CSS.
.highlight {
background-color: yellow;
}
<p class="highlight">This text is highlighted.</p>
Targets a single element with a specific id attribute.
Written with a hash (#) before the name. An ID should
only be used once per page.
#header {
font-size: 24px;
}
<h1 id="header">Welcome</h1>
.name) can be reused on multiple elements#name) should be unique — used once per pageCSS lets you set colors for text, backgrounds, borders, and more. There are a few different ways to specify a color.
CSS understands plain English color names.
p {
color: red;
}
A hex code represents a color using six characters after a
# — more precise than named colors, and what most
designers actually use.
p {
color: #2563eb;
}
RGB stands for Red, Green, Blue — each value ranges from 0 to 255, mixed together to create a color.
p {
color: rgb(37, 99, 235);
}
Use background-color to color an element's background.
body {
background-color: #f5f5f5;
}
div {
background-image: url("photo.jpg");
background-size: cover;
}
background-color and background-image both style an element's backgroundCSS gives you control over how text looks — font, size, weight, alignment, and more.
Sets which typeface is used. It's good practice to list a few fallback fonts, in case the first one isn't available.
p {
font-family: Arial, sans-serif;
}
h1 {
font-size: 32px;
}
Controls how bold text appears.
p {
font-weight: bold;
}
h1 {
text-align: center;
}
p {
color: #333;
}
Commonly used to remove the default underline from links.
a {
text-decoration: none;
}
Controls the spacing between lines of text, improving readability.
p {
line-height: 1.6;
}
font-family sets the typefacefont-size and font-weight control size and boldnesstext-align positions text (left, center, right)line-height improves readability by spacing out linesEvery HTML element is treated as a rectangular box by the browser. The box model describes the parts that make up that box — content, padding, border, and margin — from the inside out.
div {
width: 200px;
padding: 20px;
border: 2px solid black;
margin: 10px;
}
Here, the content area is 200px wide, with 20px of padding around it, a 2px border around that, and 10px of margin outside the border.
By default, padding and border are added on top of the width
you set — so an element with width: 200px and
padding: 20px actually takes up more than 200px of space
total. To avoid this confusion, many developers use:
* {
box-sizing: border-box;
}
This makes padding and border count within the stated width, instead of adding to it — much easier to predict and work with.
box-sizing: border-box is a common fix to make sizing more predictableBorders and margins are two parts of the box model we just covered — here's a closer look at how to actually use them.
A border wraps around an element. It needs three things: width, style, and color.
div {
border: 2px solid black;
}
2px — the thickness of the bordersolid — the style (other options: dashed, dotted, double)black — the colordiv {
border: 2px solid black;
border-radius: 12px;
}
border-radius rounds the corners — a higher value
creates a more rounded (or fully circular) shape.
div {
border-top: 2px solid red;
border-bottom: 1px solid grey;
}
Margin creates space outside an element, pushing other elements away from it.
div {
margin: 20px;
}
div {
margin-top: 10px;
margin-bottom: 20px;
margin-left: 5px;
margin-right: 5px;
}
div {
width: 300px;
margin: 0 auto;
}
Setting left and right margin to auto (with a defined
width) centers a block element horizontally — a trick you've
actually used already on EDUhub.
border-radius rounds cornersmargin: 0 auto is a common trick to center elementsThink you've got CSS down? Test yourself!
Start Quiz →