CSS Lesson 3 – Ways to Add CSS | Dataplexa
Lesson 3

Ways to Add CSS

Learn the three different methods for adding CSS to HTML and discover when to use each approach for maximum effectiveness.

CSS can be added to HTML in three distinct ways. Think of it like adding music to a party — you could have a live band (inline styles), a DJ booth in the room (internal styles), or a sound system broadcasting from another building (external styles). Each method has its place, and understanding when to use each one will make you a better developer.

The WanderLust development team needs to understand these three approaches because they'll be using different methods for different parts of their travel website. Some styles need to be quick and specific, others need to apply to entire pages, and some need to work across all their pages.

The Three Methods

Every CSS method has a specific purpose. Inline styles go directly on HTML elements like makeup on a person's face. Internal styles sit in the HTML document's head like instructions at the top of a recipe. External styles live in separate files like a cookbook you reference while cooking.

Inline CSS

Styles applied directly to individual HTML elements using the style attribute.

Internal CSS

Styles placed inside a <style> tag within the HTML document's <head> section.

External CSS

Styles written in separate .css files and linked to HTML documents.

Best Practice

Use external CSS for most styling, internal for page-specific styles, inline sparingly.

Method 1: Inline CSS

Inline CSS applies styles directly to individual HTML elements using the style attribute. Think of it like using a marker to color one specific word in a book — it only affects that one element and nothing else.

The WanderLust team wants to make their main heading stand out with a special blue color and larger text. Here's how they could do it with inline CSS:

<!-- WanderLust homepage heading with inline styles -->
<h1 style="color: #0ea5e9; font-size: 48px; text-align: center;">
  Discover Amazing Destinations
</h1>

<!-- A button with inline styling -->
<button style="background: #f97316; color: white; padding: 12px 24px; border: none; border-radius: 8px;">
  Book Now
</button>
localhost/index.html

What just happened?

The style attribute applied CSS directly to each element. The heading became blue and larger, while the button got an orange background and rounded corners. Each element carries its own styling instructions.

When to Use Inline CSS

Inline CSS works best for quick fixes, one-off styling, or when you need to override other styles with maximum priority. However, it makes HTML messy and harder to maintain. If you find yourself copying the same inline styles to multiple elements, it's time to switch to internal or external CSS.

Method 2: Internal CSS

Internal CSS lives inside a <style> tag within your HTML document's <head> section. Think of it like writing cooking instructions at the top of a recipe — they apply to everything you're making in that one dish (or in this case, that one HTML page).

The WanderLust team wants to style multiple elements on their booking page with consistent colors and fonts. Internal CSS makes this much cleaner:

<!DOCTYPE html>
<html>
<head>
  <title>WanderLust - Book Your Trip</title>
  
  <!-- Internal CSS styles for this page only -->
  <style>
    h1 { 
      color: #0ea5e9; 
      text-align: center; 
      font-size: 36px; 
    }
    
    .booking-form { 
      background: #f8fafc; 
      padding: 30px; 
      border-radius: 12px; 
      max-width: 500px; 
      margin: 0 auto; 
    }
    
    .btn-primary { 
      background: #f97316; 
      color: white; 
      padding: 12px 24px; 
      border: none; 
      border-radius: 8px; 
      font-size: 16px; 
    }
  </style>
</head>
<body>
  <h1>Book Your Dream Vacation</h1>
  
  <div class="booking-form">
    <h3>Trip Details</h3>
    <p>Choose your destination and travel dates below.</p>
    <button class="btn-primary">Search Flights</button>
  </div>
</body>
</html>
localhost/booking.html

What just happened?

The CSS rules in the <style> tag applied to multiple elements at once. The h1 selector styled the heading, the .booking-form class styled the container, and .btn-primary styled the button. Much cleaner than inline styles!

When to Use Internal CSS

Internal CSS works perfectly for page-specific styles that won't be used elsewhere, prototyping designs, or small single-page websites. It keeps everything in one file, making it easy to see all your styles without switching between files. However, styles can't be shared between pages, leading to duplication across your website.

Method 3: External CSS

External CSS stores styles in separate .css files that you link to your HTML documents. Think of it like having a master style guide that multiple people can reference — one set of rules that applies consistently across your entire website.

The WanderLust team wants all their pages (home, destinations, booking, about) to share the same basic styling. External CSS is perfect for this:

Step 1: Create the CSS File

First, create a file called styles.css with your CSS rules:

/* WanderLust global styles - used across all pages */
body {
  font-family: 'Arial', sans-serif;
  margin: 0;
  background: #f8fafc;
  color: #334155;
  line-height: 1.6;
}

/* Main heading styles */
h1 {
  color: #0ea5e9;
  font-size: 36px;
  text-align: center;
  margin: 30px 0;
}

/* Navigation styles */
.nav-bar {
  background: #0f172a;
  padding: 15px 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.nav-bar a {
  color: white;
  text-decoration: none;
  margin: 0 15px;
  font-weight: 500;
}

/* Button styles */
.btn-primary {
  background: #f97316;
  color: white;
  padding: 12px 24px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
}

.btn-primary:hover {
  background: #ea580c;
}

Step 2: Link the CSS File

Next, link the CSS file to your HTML using the <link> tag in the <head> section:

<!DOCTYPE html>
<html>
<head>
  <title>WanderLust Travel</title>
  
  <!-- Link to external CSS file -->
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <nav class="nav-bar">
    <div>WanderLust</div>
    <div>
      <a href="index.html">Home</a>
      <a href="destinations.html">Destinations</a>
      <a href="booking.html">Book Trip</a>
      <a href="about.html">About</a>
    </div>
  </nav>
  
  <h1>Welcome to WanderLust Travel</h1>
  
  <div style="text-align: center; margin: 40px;">
    <button class="btn-primary">Explore Destinations</button>
  </div>
</body>
</html>
localhost/index.html

What just happened?

The <link> tag connected the HTML to the external CSS file. Now all the styles from styles.css apply to this page. The same CSS file can be linked to multiple pages, creating consistent styling across the entire website.

When to Use External CSS

External CSS is the professional standard for most websites. Use it when you have multiple pages that share styles, when working in a team environment, or when you want to maintain clean, organized code. It enables code reuse, easier maintenance, and better performance through browser caching.

Choosing the Right Method

The method you choose depends on your specific situation. Each approach has advantages and trade-offs that make it suitable for different scenarios:

Method Best For Pros Cons
Inline CSS Quick fixes, email templates, one-off styles Immediate effect, highest priority Hard to maintain, clutters HTML
Internal CSS Single pages, prototypes, small sites Everything in one file, page-specific Can't share between pages, larger file size
External CSS Multi-page sites, team projects, production Reusable, clean HTML, cached by browsers Extra HTTP request, separate file to manage

Real-World Usage Examples

Major websites combine all three methods strategically. Netflix uses external CSS for their global styles, internal CSS for page-specific features, and inline CSS for dynamic content that changes based on user data. Stripe relies heavily on external CSS for consistency across their payment forms, with inline styles for real-time validation feedback.

Pro Tip

Start with external CSS as your foundation, add internal CSS for page-specific needs, and use inline CSS sparingly for dynamic or emergency fixes. This approach scales well as your website grows.

WanderLust Implementation Strategy

The WanderLust team decides to use external CSS for shared elements like navigation, buttons, and typography. They'll use internal CSS for the booking form's unique layout and inline CSS for dynamic pricing displays that change based on user selections.

Performance and Loading

Different CSS methods affect how fast your website loads. Inline CSS loads instantly with the HTML but can make pages larger. Internal CSS loads with each page, which means it can't be cached separately. External CSS requires an additional download but gets cached by browsers, making subsequent page loads faster.

For optimal performance, minimize inline CSS, use internal CSS only when necessary, and leverage external CSS for styles shared across multiple pages. This strategy reduces the total amount of CSS browsers need to download and process.

Modern websites typically use 80-90% external CSS, 10-15% internal CSS, and 0-5% inline CSS for the best balance of maintainability and performance.

Quiz

1. The WanderLust team wants consistent styling across their home, destinations, booking, and about pages. Why is external CSS the best choice for this?


2. Which HTML attribute is used to add inline CSS to an element?


3. What is the correct way to link an external CSS file named "styles.css" to an HTML document?


Up Next: CSS Selectors

Master the art of targeting HTML elements with precision using CSS selectors, from basic element selection to advanced pseudo-classes.