Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Adding CSS to a Page | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 2 of 50

·css

Adding CSS to a Page

7 mins

Course progress4%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Prefer external stylesheets for reusability and caching.
  • Inline styles have highest specificity but are hard to maintain.
  • Embedded `<style>` is useful for page‑specific or critical CSS.

There are three ways to attach CSS to an HTML document. Each has trade‑offs in performance, maintainability and specificity.

MethodSyntaxWhen to useProsCons
Inline<p style="…">Quick prototyping or dynamically generated styles from JS.Highest specificity, no extra request.Hard to maintain, breaks cascade, not cacheable.
Embedded<style> … </style> inside <head>Critical CSS that must load before the page paints.No extra HTTP request, can be inlined by build tools.Still part of the same file, grows the HTML size.
External<link rel="stylesheet" href="style.css">Site‑wide visual rules, libraries, theme files.Cached by the browser, keeps HTML lean, easy to edit.Extra network request unless preloaded.

The golden rule for production sites: external stylesheet + critical‑CSS inlining.


Example – Linking an External Stylesheet

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link rel="stylesheet" href="/css/main.css" />
  <title>My Site</title>
</head>
<body>
  <!-- page markup -->
</body>
</html>

Pro Tip – Preload for Faster Paint

<link rel="preload" href="/css/main.css" as="style" onload="this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="/css/main.css"></noscript>

Preloading tells the browser to fetch the CSS early while still keeping it non‑blocking. Once loaded the onload handler swaps the rel attribute, turning the resource into a normal stylesheet.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonWhat is CSS?Continue to lesson 3Basic SelectorsBack to CSS Basics to Advanced curriculum