Open In App

How to link CSS with HTML Document?

Last Updated : 03 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

CSS is probably the most powerful style language in web development. It allows you to keep presentations of a document separate from the structure using CSS, making the maintenance and updating of a web page quite easy. CSS provides possibilities to style HTML elements, handle layouts, and create responsive designs that look great on any device.

Below are the three approaches to applying CSS in HTML:

Inline CSS

Inline CSS allows you to apply styles directly to specific HTML elements using the style attribute. This method is useful for applying unique styles to individual elements without influencing other document parts. Although convenient for quick adjustments or testing, inline CSS is typically not recommended for larger projects, as it can lead to a cluttered codebase and hinder maintainability.

Syntax:

<element style="property: value;">
Content
</element>

Example: This demonstrates the use of inline CSS to style an h1 heading with blue color and centered text, a paragraph with green text, and a font size of 18px.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
               "width=device-width,initial-scale=1.0">
    <title>Inline CSS Example</title>
</head>

<body>
    <h1 style="color: blue; text-align: center;">
      Hello, World!
    </h1>
    <p style="font-size: 18px; color: green;">
      This is a paragraph with inline CSS.
    </p>
</body>

</html>

Output:

imresizer-1724921738490
Output

Internal CSS

Internal CSS is written within a <style> element inside the <head> section of an HTML document. This method is useful when you want to apply styles to a single webpage without affecting other pages or when you need to keep the styles separate from the content within the same document. However, it may not be ideal for larger projects where multiple pages share the same styling, as external CSS is generally preferred for better organization and reusability.

Syntax:

<head>
<style>
selector {
property: value;
}
</style>
</head>

Example: This demonstrates the use of internal CSS within the <style> element to style the background, heading, and paragraph of a webpage.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Internal CSS Example</title>
    <style>
        body {
            background-color: lightgrey;
        }

        h1 {
            color: red;
            text-align: center;
        }

        p {
            font-family: Arial, sans-serif;
            font-size: 16px;
        }
    </style>
</head>

<body>
    <h1>Welcome to Internal CSS</h1>
    <p>This is a paragraph
        styled with internal CSS.</p>
</body>

</html>

Output:

imresizer-1724921980347
Output

External CSS

On External CSS requires attaching an outside .css record to your HTML document. that is quite beneficial if you have multiple net pages on that you need to enforce the same patterns. This continues the code cleaner, isolating CSS absolutely from HTML.

Syntax:

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

Example: This demonstrates the use of external CSS within the <style> element to style the background, heading, and paragraph of a webpage.

HTML
<!-- index.html-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>External CSS Example</title>
    <link rel="stylesheet" href="Styles.css">
</head>

<body>
    <h1>Welcome to External CSS</h1>
    <p>This paragraph is
        styled using an external CSS file.</p>
</body>

</html>
CSS
/* Styles.css*/
body {
    background-color: white;
}

h1 {
    color: rgb(142, 37, 37);
    text-align: center;
}

p {
    font-family: 'Courier New', Courier, monospace;
    font-size: 14px;
    color: rgb(100, 0, 0);
}

Output:

Screenshot-2024-09-02-124418
External CSS

Conclusion

Each of the above ways of CSS implementation in HTML has its own scenarios of usage and list of benefits. Inline CSS is handy and fast for minor adjustments; internal CSS comes in handy when it's about only one page styling, whereas external CSS is useful when it's about having a consistent look across many pages. Mastering these approaches will get you a well-structured and good-looking website.


Next Article
Article Tags :

Similar Reads