How to Add CSS to HTML: Methods and Clear Examples

Diego Cortés
Diego Cortés
September 15, 2025
How to Add CSS to HTML: Methods and Clear Examples

The incorporation of CSS (Cascading Style Sheets) into HTML is essential for beautifying web pages and enhancing the user experience. Through CSS, it is possible to transform simple, unappealing text into captivating visual elements. While HTML primarily handles the structure and textual content, CSS defines the aesthetics and design of the elements. This style sheet language allows you to specify how elements should appear in various formats, whether on screens, printed documents, or other media.

Methods for Adding CSS to HTML

There are three main methods for adding CSS to an HTML document: inline CSS, internal CSS, and external CSS. Each has its applications and advantages, which are detailed below.

1. Inline CSS

Inline CSS allows you to apply styles to individual HTML elements using the style attribute. This method involves inserting CSS code directly into the HTML element in question. Although inline CSS reduces the number of files the browser must download, it is considered less advisable for large projects since it can hinder maintainability and code readability.

Example 1:

<body>
    <p style="color:red; font-size: 20px;">This is our first HTML code.</p>
    <p>This is our second HTML code.</p>
</body>

Example 2:

<h1 style="color:yellow; font-size:40px;">This is an HTML code</h1>
<p style="color:black; font-size:42px;">This is an HTML code with inline CSS.</p>
<div style="color:orange; font-size:44px;">This is a text content with CSS styling.</div>

Despite its advantages, such as reducing HTTP requests, inline CSS is not ideal for lengthy documents since it interferes with the separation of content and design.

2. Internal CSS

Internal CSS is a popular option for setting specific styles within a single HTML document. To use it, the <style> tag must be included in the <head> section of the document. This approach is useful for making customizations on a single web page but cannot be used to modify multiple pages simultaneously.

Example 1:

<!DOCTYPE html>  
<html>  
<head>  
    <style>  
        body {  
            background-color: grey;  
        }  
        h1 {  
            color: red;  
            margin-left: 75px;  
        }   
    </style>  
</head>  
<body>  
    <h1>Internal CSS applies to this header, so it will have a unique appearance.</h1>  
    <p>This paragraph will not be affected as no internal CSS is applied.</p>  
</body>  
</html>  

Example 2:

<!DOCTYPE html>
<html>
<head>
    <style>
        p {
            color: powderblue;
        }
    </style>
</head>
<body>
    <h2>Internal CSS Demonstration</h2>
    <p>The default text color for the page is black. However, we can change the color of each paragraph element on the page using internal CSS.</p>
    <p>With internal CSS, only a single set of rules is needed to change the color of each paragraph element.</p>
    <p>With inline CSS, we would have to add a style attribute to each of the paragraphs in my HTML.</p>
</body>
</html>

This method provides greater flexibility than inline CSS and is easier to manage for pages that require a consistent style.

3. External CSS

External CSS is commonly used to apply styles to multiple HTML pages. This method is accomplished through a separate CSS file that is linked to the HTML document using the <link> tag in the <head> section. CSS files must be saved with a .css extension and should not contain HTML elements.

This approach is especially useful for large projects, as it allows for modifying the design of an entire website with a single change to the CSS file.

Example of including external CSS:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="mystyle.css">
</head>
<body>
    <h1>This is a header</h1>
    <p>This is a paragraph.</p>
</body>
</html>

In the mystyle.css file, the necessary CSS code can be written to style the website.

Example 1 from the mystyle.css file:

body {
    background-color: powderblue;
}

.main {
    text-align: center;   
}

.sideway {
    color: #009900;
    font-size: 40px;
    font-weight: bold;
}

#change1 {
    font-style: bold;
    font-size: 25px;
}

Example 2 from the mystyle.css file:

body {  
    background-color: lightblue;  
}  

h1 {  
    color: navy;  
    margin-left: 20px;  
}   

Using Different Types of CSS

Each method of incorporating CSS has its pros and cons. Inline CSS, while speeding up page loading by reducing the number of requested files, often results in code that's harder to maintain. On the other hand, internal CSS is excellent for individual pages, but its scope is limited to a single document. External CSS is ideal for large projects, as it allows centralized style management.

Advantages of Inline CSS

  • Speeds up loading time by not requiring multiple files.
  • Useful for quick testing and debugging.

Advantages of Internal CSS

  • Allows customization of the appearance of a single page without needing additional files.
  • Facilitates the implementation of styles for specific elements.

Advantages of External CSS

  • Enables the application of a consistent design across multiple pages.
  • Allows for global style updates through a single file.

CSS Properties

The concept of "cascading" means that styles can inherit and override each other depending on their order of application. Generally, inline CSS has the highest priority, followed by internal CSS, and finally external CSS. This means that if the same styles are defined in more than one location, the browser will apply the one with the highest priority.

Customizing Websites with CSS

Cascading Style Sheets (CSS) allow for effective customization of the aesthetics of a website. By using any of the mentioned methods, it is possible to adjust the presentation to align with the desired vision. HTML establishes the structure, while CSS handles the styles, ensuring a uniform and appealing appearance.

Conclusion

CSS is fundamental for defining how a page should look in the browser. It allows for a wide range of changes, from text and background colors to animations and complex styles. The methods for implementing CSS (inline, internal, and external) offer flexibility for adapting and customizing web pages according to project needs. This article has explored in depth the different ways to apply CSS to HTML and their respective advantages. For more information on web design, you are invited to continue exploring additional content in this blog.

Article information

Published: September 15, 2025
Category: CSS Tutorials
Reading time: 5-8 minutes
Difficulty: Intermediate

Key tips

1

Take your time to understand each concept before moving on to the next one.

2

Practice the examples in your own development environment for better understanding.

3

Don't hesitate to review the additional resources mentioned in the article.

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Frequently Asked Questions

Categories

Page loaded in 23.18 ms