CSS Selector
CSS Selector
• selectors
• declaration
The declaration is a part that appears within the braces of the CSS rule
followed by the selector. The rules defined in the declaration part are
applied to the elements specified by the selector. The different types of
selectors are as follows.
The universal selector selects all the elements that are present in
an HTML document.
You can use this selector to apply the same rule to all the elements of an
HTML or XHTML document. The universal selector is represented by an
asterisk symbol, as shown in the following code snippet.
*{}
*
{
margin:0;
padding:0;
}
In the above code fragment, the margin and the padding properties are set
to 0 for all the elements in the HTML or XHTML document on which the
CSS rule is applied.
Example
<!DOCTYPE html>
<html>
<head>
<title>CSS Universal Selectors Example</title>
<style>
*
{
Font-family:Broadway;font-size:20;color:
green;}
</style>
</head>
<body>
Here is the sample output produced by the above CSS universal selector
example code:
The following code fragment shows how to use the type selector in CSS.
In the above code fragment, we have specified the font-family property for
the different heading elements (h1, h2, h3) and for the paragraph element
(p).
Example
Let's look at the following example. This example demonstrates CSS type
selector.
<!DOCTYPE html>
<html>
<head>
<title>CSS Selectors Example</title>
<style>
H2 {Font-family:Broadway;font-size:20;color:
green;}
</style>
</head>
<body>
</body>
</html>
Let's consider that you have an element, H1, with a class attribute whose
values is intro, as shown in the following code fragment.
(i) By applying the CSS rule to all the elements that have the class attribute
of the same value. The following code fragment shows how to apply the
CSS rule.
Example
Let's look at the following example. This example illustrates the CSS class
selector.
<!DOCTYPE html>
<html>
<head>
<title>CSS Selectors Example</title>
<style>
.green
{
color: green;
}
</style>
</head>
<body>
</body>
</html>
Output of the above Program
As you can see from the above example, only the first heading is applied to
display in red colored using the class selector.
<!DOCTYPE html>
<html>
<head>
<title>CSS Selectors Example</title>
<style>
h2.green
{
color: green;
}
p.red
{
color: red;
}
</style>
</head>
<body>
<h2 class="green">CSS Class Selector Tutorial</h2>
<hr/>
<p class="green">This is tutorial on CSS class
selector.</p>
<hr/>
<h2 class="red">CSS Class Selector Example</h2>
<p class="red">This is example on CSS class
selector.</p>
</body>
</html>
Now, the above CSS class selector example code will produce the
following output:
CSS ID Selector
The value of the id attribute is unique within a document; therefore, the
selector is applied only to the content of one element.
In the above code fragment, myHeader is the value of the id attribute. The
CSS rule is applied to the value of the id attribute.
Example
Let's look at the following example. This example shows how to select
HTML elements using the CSS ID selector.
<!DOCTYPE html>
<html>
<head>
<title>CSS Class ID Selectors Example</title>
<style>
#green
{
color: green;
}
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>CSS Selectors Example</title>
<style>
h2#green
{
color: green;
}
p#red
{
color: red;
}
</style>
</head>
<body>
</body>
</html>