In this article, we will learn how to use CSS in your HTML. There are 2-3 ways to use CSS in your HTML. ALL way we will see this in this article with examples. Before going to using CSS lets understand what is CSS first.

The full form of CSS is Cascading Style Sheet, which is a text file with .css extension. This is used to design the webpages to look more attractive and user friendly.

CSS is commonly used to define styles and layouts of Web pages which are written in HTML and XHTML. We can control each tag of HTML using CSS from the perspective of design.

CSS3 is an upgraded version of CSS with more design, features and new elements.

Syntax of CSS:-

1
2
3
4
5
6
7
8
9
10
{ first property : value;

Second property : value;

Third property: value;

.
.

}

Inline CSS

If you want to style a particular and specific element of HTML then you can use inline CSS.

To use inline CSS, add the style attribute to the relevant element.

Below is an example of inline CSS.

1
2
3
4
5
6
7
8
9
<!DOCTYPE html>
<html>
<body>

<h1 style="color:Green;">This is a h1 heading</h1>
<p style="color:red;">This is a paragraph.</p>

</body>
</html>

Internal CSS

Internal CSS is also very useful. It is used most of the time when you want to use the same style for the multiple tags or for all tags.

Suppose if you want to make the color of h tag red for all HTML page then you can use it instead of inline CSS.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html>
<head>
<style>
body {
  background-color: yellow;
}

h1 {
  color: red;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

External CSS

External CSS is not a complicated process. It is very easy. The only difference in external CSS from the above two is that here we create a separate CSS file with .css extension where we write the code of CSS. And inside the HTML file we give the location of the CSS file.

Like

Lets see the complete example for how we can use it efficiantly.

index.html

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h2>This is h2 tag</h2>
<p>This is paragraph tag</p>
</body>
</html>

As we can write HTML in any text editor. Similarly css file we can write in any text editor.

style.css


1
2
3
4
5
6
body{
    background-color:green;
}
h2{
color:red;
}

Using the above css of body, we are trying to make background green of the webpage.

And using color:red; we have tried to make heading h2 text color red.