Three Ways to Add CSS in HTML

  1. Inline CSS [Not recommandable]
  2. Internal CSS
  3. External CSS

Inline CSS:

Inline CSS is used to add custom properties to specific elements. The added style will only reflect on that particular element only.

To use inline CSS, Insert the "style" attribute within the HTML element's opening tag.

As you can see in the example below heading About Me which is in maroon color:

<h2 style="color:antiquewhite; background-color: brown;"> About Me </h2>

In the below About ME heading we can see this css applied style.

About Me

My Short Note: In Above <h2> tag we are using "Inline CSS" but this is not so considered as a good practice. Why? because inline is not recommandable to use for big projects. This technique is very time consuming process because whenever you want to add some changes in your project you have to change all the inline css in every individual attributes or tags.

Note: The downside of using inline CSS is, that once the project complexity increases, it will become difficult to manage the styles of each and individual elements.

Internal CSS:

Internal CSS is used to apply custom style to the multiple elements on the same page. The style can be used throughout the HTML page.

Internal CSS is defined in a style block, which will be inside the head section.

As you can see in the example below heading About Me which is in darkcyan color:

            <!DOCTYPE html>
                <html lang="en">
                <head>
                    <meta charset="UTF-8">
                    <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    <title>Document</title>
                    <style>
                        #internal h1 {
                        color: skyblue; 
                        background-color: darkcyan;
                        }
                        </style>
                        </head>
                        <body>
                        <header></header>
                        <main></main>
                        <footer><footer>
                        </body>
                        </html>
                
            

In the below About ME heading we can see this css applied style.

About Me

Note: The <style> block should always be in the <head> section.

My Short Note: In Above <h1> tag we are using "Internal CSS" basically it is used in html pages under the <head> section. Why? because internal CSS is a style, and in html, styles and important scripts are only used in <head> section. This technique is very usefult for save time because whenever you want to add some changes in your project you have to changes only in specific styles in css instead of changing all the css styles in Inline CSS.

Exterenal CSS:

External CSS works similarly to internal CSS but with a twist. Instead of adding the styles within the HTML file, we create a separate file with .css extension. This file will hold all the styling details. Then, we link this file to the HTML page, giving it the instructions on how to look.

There is a new <link> tag in the head section, and this <link> tag has rel and href properties.

The following points will explain each keyword's meaning:

  1. <link>: This tag is used to create links between different resources, like stylesheets, fonts, and more. In our case, we are using a link tag to link the CSS file with the HTML file.
  2. rel="stylesheet": rel stands for relationship, this defines the type of relationship between the HTML document and the linked resource. When set to "stylesheet", it specifies that the linked resource is a stylesheet that will be used to style the HTML content.
  3. href="style.css": The href attribute stands for "hypertext reference." It specifies the path or URL to the external resource we want to link. In this case, it's the path to the external CSS file called "style.css".

As you can see in the example below heading About Me which is in darkcyan color:

            <!DOCTYPE html>
                <html lang="en">
                <head>
                    <meta charset="UTF-8">
                    <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    <title>Document</title>
                    <link rel="stylesheet" href="./style.css"/>
                    </head>
                    <body>
                    <header></header>
                    <main></main>
                    <footer><footer>
                    </body>
                    </html>
            
            
                    #external h1 {
                        color: red;
                        background-color: blue;
                    }
            

This above css code are from serpertad file which is called style.css.

In the below About ME heading we can see this css applied style.

About Me

This approach enables to use of the same CSS to multiple HTML files, wherever the same custom style is required.

This is helpful when we have to maintain consistency on our web pages and want to use the same CSS styles across multiple pages.

Note: The precedence is Inline CSS > Internal CSS > External CSS. If we define the same property with different property values in three different ways, the element will have the property value of inline CSS.

My Short Note: In Above <h1> tag we are using "External CSS" basically it is used in multiple html pages with only one style.css and we can use multiple stylesheets for multiple html pages, under the <head> tag with the help of <link> tag. Why? because external CSS is a style, and in html, styles and important scripts are only used in <head> section. This technique is very usefult for save time and want to changes in bulk because whenever you want to add some changes in your project you have to changes only in specific styles in css instead of changing all the css styles in Inline CSS.