Open In App

How to Add CSS

Last Updated : 08 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Adding CSS (Cascading Style Sheets) to your HTML is essential for creating visually appealing and user-friendly web pages. In this guide, we will explore the three primary methods to link CSS to HTML documents: inlineinternal, and external CSS. Each method has its advantages and best-use scenarios, helping you decide the best approach for your web development needs.

What is CSS?

CSS, or Cascading Style Sheets, is a stylesheet language used to control the layout and appearance of HTML elements on a web page. By linking CSS to your HTML, you can enhance the look and feel of your website, ensuring a better user experience.

Why Use CSS?

  • Separation of Concerns: CSS separates content (HTML) from design (CSS), making it easier to manage.
  • Consistency: By using CSS, you can maintain a consistent look across multiple pages of a website.
  • Responsive Design: CSS enables you to create responsive designs that adapt to different screen sizes.

Methods to Link CSS to HTML

Note: It is a best practice to keep your CSS separate from your HTML

1. Inline CSS

Inline CSS allows you to apply styles directly within HTML tags using the style attribute. This method is useful for small-scale styling or when you need to apply a unique style to a single element.

Example: In this example, we will add CSS using inline styling.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Inline CSS</title>
</head>

<body>
    <h2 style="color: green;">
          Welcome to 
          <i style="color: green;">
              GeeksforGeeks
          </i>
      </h2>
</body>

</html>

Output:

Example of Inline CSS Output

2. Internal CSS

Internal CSS involves adding CSS styles directly within the HTML file by including them inside the <style> tags. This method is more efficient for applying styles to a single HTML document and keeps your CSS code more organized compared to inline styling.

Note: It’s best to add the <style> tags within the <head> section to ensure the styles are read before the body content is loaded.

Example: In this example, we will use the internal CSS approach for styling the web page.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Internal CSS</title>
  
      <style>
        h2 {
            color: green;
        }
    </style>
</head>

<body>
    <h2>Welcome to GeeksforGeeks</h2>
</body>

</html>

Output:

Example of Internal CSS Output


3. External CSS

External CSS involves creating a separate CSS file with a .css extension and linking it to the HTML file using the <link> tag. This method is the most efficient for large projects, as it separates the design from the content, allowing for better code maintainability and reusability.

Example of External CSS:

1. Create a file named styles.css:

CSS
h2 {
    color: green;
    font-size: 20px;
}


2. Link this CSS file in your HTML:

HTML
<!DOCTYPE html>
<html>

<head>
    <title>External CSS</title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <h2>Welcome to GeeksforGeeks</h2>
</body>

</html>

Output:

Example of External CSS Output


Linking CSS to HTML allows you to create well-designed web pages. While inline CSS is suitable for small, unique styles, internal CSS is best for single-document styling. However, external CSS is the recommended best practice for larger projects due to its maintainability and reusability.



Next Article

Similar Reads