New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming CSS How to Add CSS to HTML

How to Add CSS to HTML

🎨 CSS

Learn the three ways to add CSS to your web pages.

Lesson 1 of 5 Tutorial
0/5 completed

How to Add CSS

Three Methods

1. Inline CSS

Use the style attribute directly on an element:

<p style="color: blue; font-size: 18px;">Blue text</p>

2. Internal CSS (Style Tag)

Put CSS inside a <style> tag in the <head>:

<head>
    <style>
        p { color: blue; }
        h1 { font-size: 36px; }
    </style>
</head>

3. External CSS (Best Practice)

Link a separate .css file:

<head>
    <link rel="stylesheet" href="styles.css">
</head>
💡 Best Practice

Always use external CSS files. This keeps your HTML clean and makes styles reusable across pages.

Example

<!DOCTYPE html>
<html>
<head>
    <title>CSS Methods</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
        }
        h1 {
            color: #5E256D;
            text-align: center;
        }
        p {
            color: #333;
            line-height: 1.6;
        }
        .highlight {
            background-color: #fef3c7;
            padding: 10px;
            border-left: 4px solid #f59e0b;
        }
    </style>
</head>
<body>
    <h1>My Styled Page</h1>
    <p>This paragraph is styled with internal CSS.</p>
    <div class="highlight">This is a highlighted box!</div>
</body>
</html>

Lesson Nav

Lesson sections