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
- [data-name ~= 'page']
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div >
- <p data-name="pranay_page">My name is Pranay Rana.</p>
- <span>
- <p> data </p>
- </span>
- </div>
- <p>I am working as Soft Engg.</p>
- <p data-name="demo page">My Demo Page.</p>
:first-child
Select the first element (first child) of a parent.
CSS
Use
In this example the "item1" element is highlighted with the red color.
Demo
- li:first-child
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <ul>
- <li>item1</li>
- <li>item2</li>
- <li>item3</li>
- <li>item4</li>
- </ul>
:last-child
Select the last element (last-child) of a parent.
CSS
Use
In this example the "item4" element is highlighted with the red color.
Demo
- li:last-child
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <ul>
- <li>item1</li>
- <li>item2</li>
- <li>item3</li>
- <li>item4</li>
- </ul>
:nth-child(n)
Select each element that is the number n child of a parent.
CSS
Use
In this example the "second li" element is highlighted with the red color, in this example the text "item2" is highlighted.
Demo
- li:nth-child(2)
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <ul>
- <li>item1</li>
- <li>item2</li>
- <li>item3</li>
- <li>item4</li>
- </ul>
:nth-last-child(n)
Select each element of the last n children of a parent counting from the bottom.
- li:nth-last-child(2)
- {
- font:15px arial,sans-serif;
- color:red;
- }
Use
- <ul>
- <li>item1</li>
- <li>item2</li>
- <li>item3</li>
- <li>item4</li>
- </ul>
In this example the "second last li" element is highlighted with the red color, in this example the text "item3" is highlighte
:only-child
Select the child element that is the only child of the parent.
CSS
Use
In this example the "paraghaph 1" element is highlighted with the red color that is the only child of the div element.
Demo
- p:only-child
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div>
- <p> pragraph 1</p>
- </div>
- <div>
- <p> pragraph 2</p>
- <p> pragraph 3</p>
- <p> pragraph 4</p>
- </div>
:first-of-type
Select the first element of a given type that is the first under the parent element.
CSS
Use
In this example the "paragraph 1" element is highlighted with the red color.
Demo
- p:first-of-type
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div>
- <p> pragraph 1</p>
- <p> pragraph 2</p>
- <p> pragraph 3</p>
- </div>
:last-of-type
Selects the last element of a given type that is the last under a parent element.
CSS
Use
In this example the "paragraph 1" element is highlighted with the red color, that means here "pragraph 1" is the first element of the type p under the div.
Demo
- p:last-of-type
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div>
- <p> pragraph 1</p>
- <p> pragraph 2</p>
- <p> pragraph 3</p>
- </div>
:nth-of-type(n)
Select the nth element of a given type that is at the nth place under a parent element.
CSS
- p:nth-of-type(2)
- {
- font:15px arial,sans-serif;
- color:red;
- }
Use
In this example the "paragraph 2" element is highlighted with the red color, which means here "paragraph 2" is the second element of type p under the div.
- <div>
- <p> pragraph 1</p>
- <p> pragraph 2</p>
- <p> pragraph 3</p>
- <p> pragraph 4</p>
- </div>
:nth-last-of-type(n)
Select the nth last element of a given type that is at the nth place under a parent element from the last.
CSS
Use
In this example, the "paragraph 3" element is highlighted with the red color, which means here "paragraph 3" is the last element of type p under the div from the last.
Demo
- p:nth-last-of-type(2)
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div>
- <p> pragraph 1</p>
- <p> pragraph 2</p>
- <p> pragraph 3</p>
- <p> pragraph 4</p>
- </div>
:only-of-type
Select only an element of a given type that is under a parent element.
CSS
Use
In this example the "paragraph 1" element is highlighted with the red color, because p is the only element of the given type.
Demo
- p:only-of-type
- {
- font:15px arial,sans-serif;
- color:red;
- }
- <div>
- <p> pragraph 1</p>
- </div>
- <div>
- <p> pragraph 2</p>
- <p> pragraph 3</p>
- <p> pragraph 4</p>
- </div>
:empty
Selects an element that is empty, in other words, doesn't have a child.
CSS
Use
In this all the text in the document is highlighted with the red color.
Demo
- div:empty
- {
- width:100px;
- height:20px;
- background:red;
- }
- <div></div>
- <div>
- <p> pragraph 2</p>
- <p> pragraph 3</p>
- <p> pragraph 4</p>
- </div>
::selection
Highlight the text with the color selected.
CSS
Use
In this all the text is highlighted with green that is selected in the document.
Demo
- ::selection
- {
- background:green;
- }
- <div> <p> pragraph 1</p> </div>
- <div>
- <p> pragraph 2</p>
- <p> pragraph 3</p>
- <p> pragraph 4</p>
- </div>
:not(HTMLElement)
does not apply the style to the specified HTMLElement.
- :not(span)
- {
- font:15px arial,sans-serif;
- color:red;
- }
- span
- {
- color:black;
- }
- <p> pragraph 1</p>
- <p> pragraph 2</p>
- <p> pragraph 3</p>
- <p> pragraph 4</p>
- <span> span data</span>
:enable
select all enabled elements.
:disable
select all disabled elements.
- :input[type="text"]:enabled
- {
- background:green;
- }
- input[type="text"]:disabled
- {
- background:red;
- }
- Name: <input type="text" value="Pranay Rana" /><br>
- Country: <input type="text" disabled="disabled" value="India" />

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