By the end of this lesson, you will:
:information_source: CSS (Cascading Style Sheet) is a style sheet language that contains all the information about how your website looks. Think of it as the paintbrush that colors and decorates your HTML content!
Web page without CSS and with CSS
Purpose of CSS
You can add CSS to your HTML in three different ways:
<style>
tag in the <head>
sectionInline CSS is perfect when you want to style just one element on your page.
:bulb: Tip Use inline CSS when you need to quickly style a single element. Add the "style" attribute directly to your HTML tag!
How it works:
<h1 style={{'color':'red'}}>Hello, world!</h1>
<h1>Welcome to my web page!</h1>
What you see:
<!-- Browser renders: -->
<h1 style={{'color':'red'}}>Hello, world!</h1> <!-- This text appears in red -->
<h1>Welcome to my web page!</h1> <!-- This text appears in default color -->
Internal CSS is great when you want to style multiple elements on one page.
:memo: Note With internal CSS, you write all your styles in one place inside the
<style>
tag. This makes it easier to manage styles for your whole page!
Benefits of Internal CSS:
How it works:
<head>
<style>
h1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1>Hello, world!</h1>
<h1>Welcome to my web page!</h1>
</body>
What you see:
<!-- Browser renders both h1 elements with red color and center alignment -->
<h1 style={{'textAlign': 'center', 'color': 'red'}}>Hello, world!</h1>
<h1 style={{'textAlign': 'center', 'color': 'red'}}>Welcome to my web page!</h1>
External CSS is the best choice for big websites with many pages!
:information_source: Info With external CSS, you save all your styles in a separate
.css
file. This file can style multiple HTML pages at once. Change one CSS file, and your entire website updates!
Benefits of External CSS:
How to link CSS to HTML:
<link rel="stylesheet" href="____.css">
Add this line in the <head>
section of your HTML file.
Possible output of CSS file linking
Example: Let's create an external CSS file called hello.css
CSS Input (hello.css):
h1 {
text-align: center;
color: red;
}
HTML Input:
<head>
<link rel="stylesheet" href="hello.css">
</head>
<body>
<h1>Hello, world!</h1>
<h1>Welcome to my web page!</h1>
</body>
What you see:
<!-- Browser renders both h1 elements with styles from hello.css -->
<h1 style={{'textAlign': 'center', 'color': 'red'}}>Hello, world!</h1>
<h1 style={{'textAlign': 'center', 'color': 'red'}}>Welcome to my web page!</h1>
All three CSS methods create the same result! Let's compare them:
Inline CSS Example:
<h1 style={{'textAlign': 'center', 'color': 'red'}}>Hello, world!</h1>
<h1 style={{'textAlign': 'center', 'color': 'red'}}>Welcome to my web page!</h1>
Internal CSS Example:
<head>
<style>
h1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1>Hello, world!</h1>
<h1>Welcome to my web page!</h1>
</body>
External CSS Example:
CSS file (styles.css):
h1 {
text-align: center;
color: red;
}
HTML file:
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello, world!</h1>
<h1>Welcome to my web page!</h1>
</body>
All three methods produce the same visual output:
<!-- Final rendered result: -->
<h1 style={{'textAlign': 'center', 'color': 'red'}}>Hello, world!</h1>
<h1 style={{'textAlign': 'center', 'color': 'red'}}>Welcome to my web page!</h1>
:bulb: Tip Which method should you use?
- Inline CSS: Quick styling for one element
- Internal CSS: Styling for one page
- External CSS: Best for multiple pages (recommended!)
Watch this video to see CSS in action:
:information_source: Info A font is the design for a set of characters. It's like choosing different handwriting styles for your text!
The font-family property lets you choose which font to use for your text.
Two types of font families:
Generic Family | Example Fonts | Description |
---|---|---|
Serif | Times New Roman, Georgia, Garamond | Fonts with small lines at the ends of letters (like newspaper text) |
Sans-serif | Arial, Verdana, Trebuchet MS | Clean fonts without lines at the ends ("sans" means "without") |
Monospace | Courier New | All letters have the same width (like typewriter text) |
Cursive | Comic Sans MS | Fonts that look like handwriting |
Fantasy | Impact | Fun, decorative fonts for special effects |
Serif fonts have small lines at the ends of characters, while Sans-serif fonts do not.
CSS uses a smart fallback system for fonts:
:memo: Note The Fallback System: List multiple fonts separated by commas. If the browser can't find the first font, it tries the next one. Always end with a generic family as a backup!
Important rules:
<!DOCTYPE html>
<html>
<head>
<style>
.a {
font-family: "Times New Roman", Times, serif;
}
.b {
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<h1>The Font-family Property</h1>
<p class="a">This paragraph is shown in Times New Roman font.</p>
<p class="b">This paragraph is shown in Arial font.</p>
</body>
</html>
Output showing different font families
You've learned three powerful ways to add CSS to your HTML:
Plus, you discovered how fonts work in CSS and how to create beautiful text with different font families!
Code with AI: AI can also help with CSS. Experiment with these:
"CSS to make text red and bold."
"CSS to center an image on a webpage."
"CSS to create a blue button with white text."
"CSS to style a paragraph with a specific font and font size."
Font Implementation & Styling:
Typography Strategy:
Font Concepts & Best Practices:
Font Troubleshooting:
Advanced Typography Techniques:
Try these fun CSS challenges:
Color Challenge: Create an HTML page with three paragraphs. Style each one with a different color using inline CSS.
Font Fun: Use internal CSS to create a page with:
External Excellence: Create a separate CSS file that: