Style Sheets - HTML Tutorial
A style sheet is a collection of CSS rules kept in one place. Instead of styling elements one at a time, you write a rule once, such as "all paragraphs are dark gray", and the browser applies it to every matching element. Change the rule, and every paragraph updates at once.
The Parts of a CSS Rule
p {
color: #333;
line-height: 1.6;
}
- Selector (
p): which elements the rule applies to. Here, allpelements. - Declaration block (everything inside the curly braces
{ }). - Declarations (
color: #333;): each one is a property, a colon, a value and a semicolon.
You can add comments to CSS between /* and */:
/* Main text color for the whole site */
p {
color: #333;
}
Three Places to Put CSS
1. Inline Styles
Written in a style attribute on a single element, as in the previous section. Best kept for quick tests.
2. An Internal Style Sheet
Written inside a style element in the page's head. The rules apply to that page only:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Corner Cafe</title>
<style>
body {
background-color: #fffaf0;
}
h1 {
color: #8b4513;
}
p {
color: #333;
}
</style>
</head>
<body>
<h1>Corner Cafe</h1>
<p>Fresh coffee and homemade food since 1998.</p>
<p>Open every day, 7am to 4pm.</p>
</body>
</html>
3. An External Style Sheet
This is the method you'll use most. The CSS goes in its own file, ending in .css, and each page links to it with a link element in the head. One file can style every page on your site.
Try it. Create a file called styles.css in your practice folder:
body {
background-color: #fffaf0;
}
h1 {
color: #8b4513;
}
p {
color: #333;
}
Then link to it from your HTML page:
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Corner Cafe</title>
<link rel="stylesheet" href="styles.css">
</head>
The href works like a link, so if your CSS file is in a folder, include the folder in the path, for example href="css/styles.css". A CSS file contains only CSS rules, with no <style> tags.
More Ways to Select Elements
To apply the same rule to several kinds of element, separate the selectors with commas:
h1, h2, h3 {
font-family: Georgia, serif;
}
To select elements only when they're inside another element, separate the selectors with a space. This is called a descendant selector:
/* Only links that are inside a nav element */
nav a {
color: white;
}
To style everything, use the universal selector, *. You'll see it used in Margins and Padding.
The Cascade: When Rules Conflict
Sometimes more than one rule tries to set the same property on the same element. The "cascading" in Cascading Style Sheets is the set of rules browsers use to decide which one wins. For everyday work, three ideas cover almost everything.
1. Specificity
A more specific selector beats a less specific one, no matter where it appears. From weakest to strongest:
- element selectors, such as
p - class selectors, such as
.notice(you'll meet these in the next section) - ID selectors, such as
#header - inline styles in a
styleattribute
p { color: black; }
.notice { color: red; }
<p class="notice">This paragraph is red.</p>
The .notice rule wins because a class is more specific than an element name.
2. Source Order
When two rules are equally specific, the one that comes last wins:
p { color: black; }
p { color: blue; } /* paragraphs are blue */
This applies across files too. Internal and external style sheets carry the same weight, so if your page links a style sheet and then has a style element after it, the later rules win when specificity is equal. That's why the order of your link and style elements matters.
3. !important
Adding !important to a declaration makes it override normal rules:
p { color: green !important; }
Avoid it. It makes your styles hard to override later, and usually there's a cleaner fix, such as using a more specific selector.
Inheritance
Some properties are passed down from an element to the elements inside it. This is called inheritance. If you set a color on the body, every paragraph, list and heading inside it uses that color too, unless another rule says otherwise.
body {
color: #333;
font-family: system-ui, sans-serif;
}
Text properties, such as color and the font properties, are inherited. Box properties, such as borders, padding and margins, are not. A border on a div doesn't add borders to every paragraph inside it.
Tip
When a style isn't working, inspect the element in your browser's Developer Tools. The Styles panel lists every rule that applies to it, with overridden declarations crossed out, so you can see exactly which rule is winning.