Introduction
This article is about CSS selectors and how they work. The following is an explanation of most of the selectors used in CSS.
.Class
Selects all the elements with the given class name.
- .MyIntro
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div class="MyIntro">
- <p>My name is Pranay Rana.</p>
- <p>I am working as Soft Engg.</p>
- </div>
#id
Selects all the elements with the given id name.
CSS
Use
Point to note: You can have more than one element having the same classname in the HTML page but you can have only one element with the ID.
- #MyIntro
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div id="MyIntro">
- <p>My name is Pranay Rana.</p>
- <p>I am working as Soft Engg.</p>
- </div>
HTMLElement
Selects all the HTML elements that the name is given.
- P
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div>
- <p>My name is Pranay Rana.</p>
- <p>I am working as Soft Engg.</p>
- </div>
HtmlElement HtmlElemnt
Selects all the HTML elements that are in a HTML element.
CSS
- div p
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div >
- <p>My name is Pranay Rana.</p>
- <Span>
- <p> data </p>
- </Span>
- </div>
- <p>I am working as Soft Engg.</p>
HtmlElement > HtmlElemnt
Selects all the HTML elements that are children of the HTML element.
CSS
Use
In this example all p elements that are children of a div are highlighted with the red color, but the p elements that are not children of the div isn't affected.
Demo
- div > p
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div >
- <p>My name is Pranay Rana.</p>
- <Span>
- <p> data </p>
- </Span>
- </div>
- <p>I am working as Soft Engg.</p>
HtmlElement + HtmlElemnt
Selects all the HTML elements that are immediately after an HTML element.
CSS
Use
In this example a p element that is immediately after a div is highlighted with the red color, in this example the text "I am working as Soft Engg." is highlighted.
Demo
- div + p
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div >
- <p>My name is Pranay Rana.</p>
- </div>
- <p>I am working as Soft Engg.</p>
- <p> data </p>
HtmlElement ~ HtmlElemnt
Selects all the HTML elements that precede a HTML element.
CSS
Use
In this example the p element that precedes a div is highlighted with the red color, in this example the text "I am working as Soft Engg." and "My Demo Page." is highlighted.
Demo
- div ~ p
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div >
- <p>My name is Pranay Rana.</p>
- <span>
- <p> data </p>
- </span>
- </div>
- <p>I am working as Soft Engg.</p>
- <p>My Demo Page.</p>
[attribute]
Selects all the HTML elements that have an attribute.
CSS
Use
In this example any element that has the attribute "data-name"div is highlighted with the red color, in this example the text "My name is Pranay Rana." and "My Demo Page." is highlighted.
Demo
- [data-name]
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div >
- <p data-name="pranay">My name is Pranay Rana.</p>
- <span>
- <p> data </p>
- </span>
- </div>
- <p>I am working as Soft Engg.</p>
- <p data-name="demo">My Demo Page.</p>
[attribute = data]
Selects all the HTML elements with an attribute value equal to data.
CSS
Use
In this example any element that has the attribute "data-name" and the value = "demo" is highlighted with the red color, in this example the text "My Demo Page." is highlighted.
Demo
- [data-name = 'demo']
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div >
- <p data-name="pranay">My name is Pranay Rana.</p>
- <span>
- <p> data </p>
- </span>
- </div>
- <p>I am working as Soft Engg.</p>
- <p data-name="demo">My Demo Page.</p>
[attribute ^= data]
Selects all the HTML elements where the attribute value begins with data.
CSS
So with the preceding HTML when applying this the text "My name is Pranay Rana." is highlighted because its "data-name" attribute value begins with "pra".
Demo
- [data-name ^= 'pra']
- {
- font:15px arial,sans-serif;
- color:red;
- }
[attribute $= data]
Selects all the HTML elements where the attribute value ends with data.
CSS
So with the preceding HTML when applying this the text "My name is Pranay Rana." is highlighted because its "data-name" attribute value ends with "nay".
Demo
- [data-name ^= 'pra']
- {
- font:15px arial,sans-serif;
- color:red;
- }
[attribute *= data]
Selects all the HTML elements where an attribute value contains data.
- [data-name *= 'rana']
- {
- font:15px arial,sans-serif;
- color:red;
- }
So with the preceding HTML when apply this the text "My name is Pranay Rana." is highlighted because its "data-name" attribute value contains "rana".
Demo
[attribute ~= data]
Selects all the HTML elements where an attribute value contains word data.
CSS

amarish kumarPosted Mar 4, 2014, 3:44 AM
nice one...