Horje
How to add CSS

Adding CSS (Cascading Style Sheets) to HTML is essential for designing visually appealing and user-friendly web pages. There are three primary methods to use CSS into your HTML documents: external, internal, and inline. Each method has its advantages and best-use scenarios. This guide will explore these methods in detail, helping you choose the best approach for your web development needs.

Methods to Add 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.ags using the style attribute.

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

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

External CSS

xternal 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: In this example, we will use the external CSS method.

HTML
<!DOCTYPE html>
<html>

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

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

</html>
CSS
h2 {
    color: green;
    font-size: 20px;
}

Output:

Example of External CSS Output

Using CSS efficiently is important for creating well-designed web pages. While inline CSS is suitable for small, unique styles, internal CSS is best for single-document styling. External CSS, however, stands out as the best practice for larger projects due to its maintainability and reusability. By understanding and applying these methods, you can enhance the appearance and user experience of your website.




Reffered: https://www.geeksforgeeks.org


CSS

Related
How to Set 100% Height with Padding/Margin in CSS ? How to Set 100% Height with Padding/Margin in CSS ?
How to use Tailwind CSS with esbuild ? How to use Tailwind CSS with esbuild ?
How to Creating Horizontal Rule Divider with Text in Tailwind CSS ? How to Creating Horizontal Rule Divider with Text in Tailwind CSS ?
How to Remove Arrow on Input Type Number with Tailwind CSS ? How to Remove Arrow on Input Type Number with Tailwind CSS ?
How to Set Offset an HTML Anchor Element to Adjust Fixed Header in CSS ? How to Set Offset an HTML Anchor Element to Adjust Fixed Header in CSS ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12