CSS basics: linking a stylesheet, rules and properties
Your portfolio has a solid structure, but it looks like a document from the 1990s. Time to bring it to life. In this lesson you will connect a stylesheet to your pages, learn the anatomy of a CSS rule, and apply your first colors and text styles. By the end, your site will no longer look plain.
What CSS does
CSS stands for Cascading Style Sheets. It describes how HTML elements are displayed: colors, fonts, spacing, sizes, positions. The golden principle is the separation of concerns: HTML says what things are, CSS says how they look. You can redesign an entire site without touching its HTML, and keep a consistent look across dozens of pages by sharing one stylesheet.
Three ways to add CSS
There are three places where you can put styles.
- Inline, in a
styleattribute on one element:<p style="color: red;">. It works, but it mixes content and style and is hard to maintain. - Internal, in a
styleelement in the page’s head. Fine for a quick test. - External, in a separate
.cssfile linked from the page. This is the professional standard, and the one we will use.
Linking your stylesheet
You already created css/style.css. To use it, add this line inside the head of each page:
<link rel="stylesheet" href="css/style.css">
The path is relative to the HTML page, exactly like an image path. For a page in a subfolder, you would write ../css/style.css. If your styles do not apply, the path is the first thing to check: open the Network panel and look for a red 404 on the CSS file.
Test the link with a rule you cannot miss. Put this in style.css, save, and refresh the page:
body {
background-color: lightyellow;
}
If the background turns pale yellow, the connection works. You can remove it after that.
Anatomy of a rule
h1 {
color: #1d4ed8;
font-size: 2.5rem;
}
- The selector (
h1) picks which elements the rule applies to. - The declaration block is between curly braces.
- Each declaration is a property, a colon, a value and a semicolon.
A stylesheet is a list of such rules. Whitespace is free, but keep one declaration per line and indent by two spaces: you will read your own code more easily.
Comments in CSS
CSS comments use a different syntax than HTML:
/* Main heading of the home page */
h1 { color: #1d4ed8; }
The first selectors
You will study selectors thoroughly in the next lesson. For now, three simple ones are enough:
p { color: #333333; } /* every paragraph */
.intro { font-size: 1.25rem; } /* every element with class="intro" */
#logo { font-weight: bold; } /* the element with id="logo" */
A class is a label you add in HTML with the class attribute and reuse as many times as you wish: <p class="intro">. An id must be unique in a page. In practice, classes are what you will use most.
Colors
You can write a color in several ways:
- A name:
tomato,navy,white. - A hexadecimal code:
#1d4ed8, six characters for red, green and blue. A short form like#fffalso exists. - A function:
rgb(29, 78, 216), orhsl(224, 76%, 48%), which describes the hue, the saturation and the lightness.
Two properties matter most. color sets the text color. background-color sets the background. You will explore color further soon.
Your first text styles
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 1rem;
line-height: 1.6;
color: #222222;
background-color: #f8fafc;
}
h1 {
color: #1d4ed8;
}
a {
color: #1d4ed8;
}
a:hover {
color: #c2410c;
}
font-familylists fonts in order of preference, ending with a generic family such assans-serifas a fallback.font-size: 1remequals the browser’s base size, usually 16 pixels. The unitremrespects user settings.line-heightsets the spacing between lines. A value around 1.5 greatly improves readability.a:hoverapplies when the mouse is over a link. This is a pseudo-class: it targets a state of an element.
Styling several elements at once
Separate selectors with commas to share a declaration:
h1, h2, h3 {
font-family: Georgia, serif;
color: #1e293b;
}
When something does not work
CSS is forgiving but silent: an error is just ignored, and the page looks wrong without any message. Here are the usual suspects:
- A missing semicolon or closing brace: the next declaration gets ignored.
- A typo in a property name, such as
colourinstead ofcolor. - An invalid value, like
font-size: big. - A wrong path in the
linkelement. - Forgetting to refresh after saving.
Tip. Open the Elements panel and click on the element. The Styles pane lists all the rules applied, and shows invalid declarations crossed out or with a warning. It is the quickest way to find what went wrong.
Warning. Do not use style attributes everywhere “just to go faster”. Inline styles are hard to override, scattered across the HTML and impossible to reuse. Keep your CSS in the stylesheet.
Applying it to your portfolio
Add the link element to index.html, about.html, services.html and contact.html. As you edit style.css, all four pages change together, which shows the power of a shared stylesheet. Add an intro class to your first paragraph for later use:
<p class="intro">I tell stories with light and ink.</p>
Your turn
- Link
css/style.cssin all your pages. - In
style.css, stylebodywith a font family, aline-heightof 1.6, a text color and a light background. - Give the
h1andh2elements a color of your choice. - Make links change color on hover.
- Make the paragraph with class
introlarger.
See a solution
/* Base */
body {
font-family: Arial, Helvetica, sans-serif;
line-height: 1.6;
color: #1f2937;
background-color: #f9fafb;
}
/* Headings */
h1, h2 {
color: #0f766e;
}
/* Links */
a {
color: #0f766e;
}
a:hover {
color: #b45309;
}
/* Intro paragraph */
.intro {
font-size: 1.25rem;
}
In every HTML file, the head contains <link rel="stylesheet" href="css/style.css">. If one page does not change, compare its link with the working ones.
Key takeaways
- HTML describes content, CSS describes appearance; keep them separate in different files.
- Link an external stylesheet with
<link rel="stylesheet" href="...">in the head. - A rule is a selector plus declarations in braces:
property: value;. - Colors can be names, hexadecimal codes,
rgb()orhsl(). - CSS ignores errors silently: use the browser’s developer tools to find them.