CSS-1
CSS-1
Inline CSS
We can apply CSS in a single element by inline CSS technique.
The inline CSS is also a method to insert style sheets in HTML document.
This method mitigates some advantages of style sheets so it is advised to
use this method sparingly.
If you want to use inline CSS, you should use the style attribute to the
relevant tag.
Syntax:
Example:-
Internal CSS
The internal style sheet is used to add a unique style for a single
document. It is defined in <head> section of the HTML page inside the
<style> tag.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
h1 {
color: red;
margin-left: 80px;
</style>
</head>
<body>
</body>
</html>
External CSS
The external style sheet is generally used when you want to make changes on
multiple pages. It is ideal for this condition because it facilitates you to change
the look of the entire web site by changing just one file.
It uses the <link> tag on every pages and the <link> tag should be put inside
the head section.
Example:
<head>
</head>
The external style sheet may be written in any text editor but must be saved
with a .css extension. This file should not contain HTML elements.
What are Comments in CSS?
CSS Selector
CSS selectors are used to select the content you want to style. Selectors are the
part of CSS rule set. CSS selectors select HTML elements according to its id,
class, type, attribute etc.
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: blue;
</style>
</head>
<body>
<p>And me!</p>
</body>
</html>
Output:
Me too!
And me!
2) CSS Id Selector
It is written with the hash character (#), followed by the id of the element.
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: blue;
}
</style>
</head>
<body>
</body>
</html>
Output:
Hello Javatpoint.com
The class selector selects HTML elements with a specific class attribute. It is
used with a period character . (full stop symbol) followed by the class name.
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: blue;
}
</style>
</head>
<body>
</body>
</html>
Output:
If you want to specify that only one specific HTML element should be affected
then you should use the element name with class selector.
<!DOCTYPE html>
<html>
<head>
<style>
p.center {
text-align: center;
color: blue;
}
</style>
</head>
<body>
</body>
</html>
Output:
The universal selector is used as a wildcard character. It selects all the elements
on the pages.
<!DOCTYPE html>
<html>
<head>
<style>
*{
color: green;
font-size: 20px;
</style>
</head>
<body>
<h2>This is heading</h2>
<p>And me!</p>
</body>
</html>
Output:
This is heading
Me too!
And me!
The grouping selector is used to select all the elements with the same style
definitions.
Grouping selector is used to minimize the code. Commas are used to separate
each selector in grouping.
h1 {
text-align: center;
color: blue;
h2 {
text-align: center;
color: blue;
p{
text-align: center;
color: blue;
As you can see, you need to define CSS properties for all the elements. It can be
grouped in following ways:
h1,h2,p {
text-align: center;
color: blue;
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: blue;
</style>
</head>
<body>
<h1>Hello Javatpoint.com</h1>
<p>This is a paragraph.</p>
</body>
</html>
Output:
Hello Javatpoint.com
This is a paragraph.