This article explains one of the CSS rules, “selectors”, with suitable examples for a better understanding.
Selectors
Selectors are one of the most important aspects of CSS that allow you to select and manipulate HTML elements. Basically selectors are the CSS rule set that actually find or select the HTML elements you want to style based on their id, classes, attributes, types, values of attributes and much more.
In other words CSS selectors are the rule set that are used to select the HTML content on HTML pages so that they can be styled as we choose..
Types of CSS Selectors
Basically there are five types of selectors that are as follows:
Universal Selector
The universal selector plays the role of a wild card character that selects all the elements on the page. The HTML pages are made up of various tags and using this selector, all the tags are affected according to the style specified in the style block.
As you can see, the universal selector is declared using an asterisk (*). You can also use this selector in combination with another selector.
The “syntax” of the universal selector is given below.
Selectors
Selectors are one of the most important aspects of CSS that allow you to select and manipulate HTML elements. Basically selectors are the CSS rule set that actually find or select the HTML elements you want to style based on their id, classes, attributes, types, values of attributes and much more.
In other words CSS selectors are the rule set that are used to select the HTML content on HTML pages so that they can be styled as we choose..
Types of CSS Selectors
Basically there are five types of selectors that are as follows:
Universal Selector
The universal selector plays the role of a wild card character that selects all the elements on the page. The HTML pages are made up of various tags and using this selector, all the tags are affected according to the style specified in the style block.
As you can see, the universal selector is declared using an asterisk (*). You can also use this selector in combination with another selector.
The “syntax” of the universal selector is given below.
- *{ color: green;
- font-family: arial black;
- text-align: center; }
Let's see the example to implement this selector.
- <html>
- <head>
- <title>Universal selector</title>
- <style>
- *{color:green;
- font-family:arial black;
- text-align:center;}
- </style>
- </head>
- <body>
- <p>This selector will affect all the tags..</p>
- <h1>This is Universal selector</h1>
- <h4>All tags are affected</h4>
- </body>
- </html>
Output
Element type selector
This selector is also known as a “type selector” that selects the elements based on the same name of elements.
Thus, the selector name <p> would match all the HTML <p> elements or selector name <ul> would match all the HTML <ul> elements.
The syntax of an element type selector is given below.
- p{color:green;
- font-family:arial black;}
- ul{list-style-type: circle;
- border: solid 1px #ccc;
- color: red;}
Let's understand this selector with an example.
- <html>
- <head>
- <title>Element type selector</title>
- <style>
- p{color:green;
- font-family:arial black;}
- ul{list-style-type: circle;
- border: solid 1px #ccc;
- color: red;}
- </style>
- </head>
- <body>
- <p>This selector will affect the tags which have the same name of elements..</p>
- <p>This tag is very helpful</p>
- <h1>This is Element type selector</h1>
- <ul>
- <li>Animals</li>
- <li>Fruits</li>
- <li>Electronics</li>
- </ul>
- <ul>
- <li>Boy</li>
- <li>Girl</li>
- </ul>
- </body>
- </html>
In the preceding example there are two type of elements selected, <p> and <ul>, and based on that the following is the output.
Output
Output
ID selector
The id selector is declared using the “hash” character followed by the id of the element. Yes, the id selector uses the id attribute of an HTML tags to find the specific element. This selector searches the id attribute that should match the id specified in the style block. An id should be unique within the page so that you can use the id selector when you want to find the single and unique element. The “hash” symbol is ignored during matching of the element.
Note: Do not start an id name with a number.
The syntax of the id selector is given below.
- #para2{color:green;
- font-family:arial black;}
- #list{list-style-type: circle;
- border: solid 1px #ccc;
- color: red;}
Now let's have an example.
- <html>
- <head>
- <title>ID selector</title>
- <style>
- #para1{color:blue;
- font-family:arial black;}
- #para2{color:green;
- font-family:arial black;}
- #list{list-style-type: circle;
- border: solid 1px #ccc;
- color: red;}
- </style>
- </head>
- <body>
- <p id="para1">This selector will only affect the tags which have the id attribute of elements..</p>
- <p id="para2">This tag is also very helpful</p>
- <p>This paragraph is not affected..</p>
- <h1>This is ID selector</h1>
- <ul id="list">
- <li>Animals</li>
- <li>Fruits</li>
- <li>Electronics</li>
- </ul>
- <ul>
- <li>Boy</li>
- <li>Girl</li>
- </ul>
- </body>
- </html>
In the preceding example there are two id attributes, one for a paragraph and the other for a list and based on that the following is the output.
Output
Class selector
The class selector is one of the most useful selectors. Its declaration is done with a dot preceding a string of one or more characters. Just as is the case with an ID selector, this string of characters is defined by the developer. The class selector also matches all elements on the page that have their class attribute set to the same value as the class, minus the dot.
Note: Do not start a class name with a number.
The syntax of the class selector is given below.
Output
Class selector
The class selector is one of the most useful selectors. Its declaration is done with a dot preceding a string of one or more characters. Just as is the case with an ID selector, this string of characters is defined by the developer. The class selector also matches all elements on the page that have their class attribute set to the same value as the class, minus the dot.
Note: Do not start a class name with a number.
The syntax of the class selector is given below.
- .center{color:blue;
- font-family:arial black;
- text-align: center;}
- .list{list-style-type: circle;
- border: solid 1px #ccc;
- color: red;}
Let's have an example of this selector.
- <html>
- <head>
- <title>Class selector</title>
- <style>
- .center{color:blue;
- font-family:arial black;
- text-align: center;}
- .list{list-style-type: circle;
- border: solid 1px #ccc;
- color: red;}
- </style>
- </head>
- <body>
- <p class="center">This selector will only affect the tags which have the class attribute of elements..</p>
- <p class="center">This tag is also very helpful</p>
- <p>This paragraph is not affected..</p>
- <h1 class="center">This is Class selector</h1>
- <ul class="list">
- <li>Animals</li>
- <li>Fruits</li>
- <li>Electronics</li>
- </ul>
- <ul>
- <li>Boy</li>
- <li>Girl</li>
- </ul>
- </body>
- </html>
Output
Note: You can also specify that only specific HTML elements should be affected by a class.
Syntax
- p.center{color:blue;
- font-family:arial black;
- text-align: center;}
- ul.list{list-style-type: circle;
- border: solid 1px #ccc;
- color: red;}
See the demo in the following example.
- <html>
- <head>
- <title>Class selector</title>
- <style>
- p.center{color:blue;
- font-family:arial black;
- text-align: center;}
- ul.list{list-style-type: circle;
- border: solid 1px #ccc;
- color: red;}
- </style>
- </head>
- <body>
- <p class="center">This selector will only affect the tags which have the class attribute of elements..</p>
- <p class="center">This tag is also very helpful</p>
- <p>This paragraph is not affected..</p>
- <h1 class="center">This is Class selector</h1>
- <ul class="list">
- <li>Animals</li>
- <li>Fruits</li>
- <li>Electronics</li>
- </ul>
- <ol class="list">
- <li>Boy</li>
- <li>Girl</li>
- </ol>
- </body>
- </html>
Output
You can see that the class “center ” is also defined in a <h1> but it is not affected, similarly in an <ol> is not affected since in the style there is no specification of <h1> and <ol>.
Grouping selector
Very often you might have seen elements with the same style in the style sheet. For example:
You can see that the class “center ” is also defined in a <h1> but it is not affected, similarly in an <ol> is not affected since in the style there is no specification of <h1> and <ol>.
Grouping selector
Very often you might have seen elements with the same style in the style sheet. For example:
- p{color:blue;
- font-family:arial black;
- text-align: center;}
- h1{color:blue;
- font-family:arial black;
- text-align: center;}
- h4{color:blue;
- font-family:arial black;
- text-align: center;}
In this case, there is a need for minimization of the code. To minimize the code, you can make the group selectors by separating them with a comma.
Let's look at that with an example.
Let's look at that with an example.
- <html>
- <head>
- <title>Grouping selector</title>
- <style>
- p,h1,h5{color:blue;
- font-family:arial black;
- text-align: center;}
- ol,ul{list-style-type: circle;
- border: solid 1px #ccc;
- color: red;}
- </style>
- </head>
- <body>
- <p>There are two groups of selectors..</p>
- <p>This tag is helpful to minimize the code</p>
- <h5>Hello grouped selectors..!!</h5>
- <h1>This is Grouping selector</h1>
- <ul>
- <li>Animals</li>
- <li>Fruits</li>
- <li>Electronics</li>
- </ul>
- <ol>
- <li>Boy</li>
- <li>Girl</li>
- </ol>
- </body>
- </html>
In the preceding example we have two groups of selectors that are affected by the specified group style that can be seen in the following output.
Output
Output
No comments :
Post a Comment