CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
Example
p
{
color: red;
text-align: center;
}
color: red;
text-align: center;
}
CSS Selectors
CSS selectors name:
lement selectors
id selectors
class selectors
Grouping Selectors and more.
Element Selector
You can select all <p> elements on a page like this (in this case, all <p> elements will be center-aligned, with a red text color):Example
p
{
text-align: center;
color: red;
}
text-align: center;
color: red;
}
This is the manner by which the HTML code above :
Example
<p> This paragraph refers to two classes.</p>
Id Selector
To select an element with a specific id, write a hash (#) character, followed by the id of the element.The style rule below will be applied to the HTML element with id="para1":
Example
#para1
{
text-align: center;
color: red;
}
text-align: center;
color: red;
}
This is the manner by which the HTML code above :
Example
<p id="center large">This paragraph refers to two classes.</p>
Class Selector
To select elements with a specific class, write a period (.) character, followed by the name of the class.In the example below, all HTML elements with class="center" will be red and center-aligned:
Example
.center {
text-align: center;
color: red;}
text-align: center;
color: red;}
This is the manner by which the HTML code above :
Example
<p class="center large">This paragraph refers to two classes.</p>
Grouping Selectors
If you have elements with the same style definitions, like this:Example
h1
{
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;}
p {
text-align: center;
color: red;
}
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;}
p {
text-align: center;
color: red;
}
This is the manner by which the HTML code above :
Example
h1, h2, p
{
text-align: center;
color: red;}
CSS Comments
A CSS comment starts with /* and ends with */. Comments can also span multiple lines:Example
p
{
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is
a multi-line
comment */
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is
a multi-line
comment */
0 comments:
Post a Comment