CSS stands for Cascading Style Sheets. You can control the style of a webpage using CSS. Even you can control the style of multiple web pages all at once.
Basically with css you can change the color of text, font of an element(e.g. heading, paragraph, list etc. ), element's size ( height, width ), element's position, and make animation too.
Normal HTML is just nothing but a text. you can't change the color of the text. It'll be black always. Moreover nither you can change element's position nor you can change element's size. Simply you can't give any style.
Here you can see the power of CSS. Almost you can do everything using CSS. you can change the color of text, element's size ( height, width ), element's position and not just that you can make animation too.
There are three ways you can add or link css in html.
We use Inline CSS for a single particular element. So in Inline CSS whenever you want to give a style to an element you have to use style attribute.
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8”>
<title>Inline CSS Example</title>
</head>
<body>
<p style="margin:30px; font-family:Times New Roman; font-size:40px;"> I am first paragraph </p>
</body>
</html>
Here you can notice there are three property-value pairs inside of the style attribute and all are separated by semicolon. Also notice that spaces at all sides of paragraph because of margin property and fontSize of paragraph is 40px.
We use Internal css for a single HTML document.
For example if you have multiple p elements inside <body> and you want all the p elements' color to be color red then you just need to declare one time inside <style> element.
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8”>
<title>Internal CSS Example</title>
<style>
p{color:red}
</style>
</head>
<body>
<p>I am first paragraph</p>
</body>
</html>
You can see here we used <style> element inside head section to declare Internal CSS. Also notice it doesn't change the size of text as we didn't use font-size.
A .html file is called a single HTML document. We use External css for multiple HTML documents.
For instance if you have multiple .html pages and inside of every pages you have h1 elements and you want all the h1 elements' color to be yellow then you just need to declare one time inside of the .css file.
To understand this you just need a .css file. See the example below -
Here we have four files first, second and third one is .html files called myfirstpage.html , mysecondpage.html and mythirdpage.html respectively and the last one is .css file called style.css.
Note: All the files should have inside one folder.
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8”>
<title>Inline CSS Example</title>
</head>
<body>
<h1> This is my first heading. </h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8”>
<title>Inline CSS Example</title>
</head>
<body>
<h1> This is my second heading. </h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8”>
<title>Inline CSS Example</title>
</head>
<body>
<h1> This is my third heading. </h1>
</body>
</html>
h1 {
color:yellow;
}
Now question is how to link .css file to all the .html pages. To do so you need to add a <link> element inside of head section. See the example below -
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css"> // "style" is the name of the css file.
<meta charset=”UTF-8”>
<title>Inline CSS Example</title>
</head>
<body>
<h1> This is my first heading. </h1>
</body>
</html>
Now add the link element for the last two pages ( mysecondpage.html and mythirdpage.html ) .
Now you can see it does change the color of h1 element of every pages. Now what if you want to change the background color of every pages ?
you don't need to do correct every single files. You just need to correct .css file. see the example below -
h1 {
color:yellow;
}
body{
background-color:red;
}
Now you know the power of external CSS .