CSS (Cascading Style Sheets) is the language that determines the appearance of HTML elements.
This lesson covers how to connect CSS to HTML, along with selectors, specificity, and the concept of inheritance.
1. Ways to Connect CSS
1-1. Inline
Written directly in the style attribute of an HTML tag.
<p style="color: red; font-size: 18px;">Inline style</p>
- Has the highest specificity.
- Cannot be reused and is hard to maintain. Not recommended.
1-2. Internal
Written with a <style> tag inside the <head> tag.
<head>
<style>
p { color: blue; }
</style>
</head>
- Applies only to that HTML file.
- Suitable for small projects or tests.
1-3. External — Recommended
Create a separate .css file and connect it with a <link> tag.
<head>
<link rel="stylesheet" href="style.css">
</head>
- The same CSS can be reused across multiple HTML files.
- Easiest to maintain. This is the standard approach in practice.
2. Selector
The syntax that specifies which HTML elements a style applies to.
| Selector type | Example | Description |
|---|---|---|
| Tag selector | p { } |
Selects every <p> tag |
| Class selector | .card { } |
Selects elements with class="card" |
| ID selector | #header { } |
Selects the element with id="header" |
| Descendant selector | div p { } |
Selects every p inside a div |
| Child selector | ul > li { } |
Selects only the direct children li of ul |
| Sibling selector | h1 + p { } |
Selects the p immediately after an h1 |
| Universal selector | * { } |
Selects every element |
Pseudo-class Selector
a:hover { color: red; } /* when the mouse is over the link */
a:visited { color: purple; } /* a visited link */
li:first-child { color: blue; } /* the first child element */
li:last-child { color: green; } /* the last child element */
li:nth-child(2) { color: orange; } /* the second child element */
3. Specificity
When several styles conflict on the same element, one of them is applied according to its specificity.
Inline style > ID selector > Class selector > Tag selector
| Selector | Score |
|---|---|
Inline (style="") |
1000 points |
ID (#id) |
100 points |
Class (.class), pseudo-class (:hover) |
10 points |
Tag (p, div) |
1 point |
/* Example: all applied to the same <p id="title" class="text"> */
p { color: black; } /* 1 point */
.text { color: blue; } /* 10 points */
#title { color: red; } /* 100 points → this color is applied */
Using
!importantoverrides all specificity, but abusing it makes maintenance difficult.
4. Inheritance
Some CSS properties of a parent element are automatically inherited by its child elements.
body {
font-family: 'Arial', sans-serif; /* inherited by every child */
color: #333;
}
Inherited properties: color, font-family, font-size, line-height, text-align, and others
Non-inherited properties: margin, padding, border, background, width, and others
5. Basic Text Properties
p {
color: #333333; /* text color */
font-size: 16px; /* text size */
font-weight: bold; /* text weight: normal, bold, 100~900 */
text-align: center; /* alignment: left, center, right, justify */
text-decoration: none; /* removes the underline (often used on links) */
line-height: 1.6; /* line spacing */
}
Example Files
Open index.html in the examples/ folder in your browser to see the various selectors in action.
Assignment
Read assignment.md and complete Build Your Own Badge.