CSS Selectors in HTML : Part 1

CSS Selectors in HTML: Part 1

 
Today, we are going to explore CSS Selectors.
 
CSS Universal Selector example
 
The CSS universal selector ( * ) is used to target all elements to apply your specified CSS properties to them.
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" >  
  3. <head>  
  4.     <title>CSS Selectors</title>  
  5.     <style type="text/css">  
  6.         *{ color:red;}  
  7.     </style>  
  8. </head>  
  9. <body>  
  10.     <h2>Heading Content....</h2>  
  11.     <p>Paragraph Content....</p>  
  12. </body>  
  13. </html>  
universal-selector-in-HTML.png

CSS type Selector example
 
The CSS type selector ( X { } ) is used to target elements in the document by specific type and apply your specified CSS properties to that type.
  1. <!DOCTYPE html >  
  2. <html>  
  3. <head>  
  4.     <title>CSS Selectors</title>  
  5.     <style type="text/css">  
  6.         h2 { color:#06C; }  
  7.         p { color:#090; }  
  8.     </style>  
  9. </head>  
  10. <body>  
  11.     <h2>Some Stuff</h2>  
  12.     <p>Stuff inside my paragraph...</p>  
  13.     <h2>More Stuff</h2>  
  14.     <p>Stuff inside my paragraph...</p>  
  15. </body>  
  16. </html>  
type-selector-in-HTML.png

CSS group Combinator example
 
The CSS group combinator ( X, Y { } ) is used to target and style a comma-separated list of elements in a grouped fashion. This allows targeting a group of elements by their native type or by their id and class names.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.      <title>CSs Selectors</title>  
  5.      <style type="text/css">  
  6.          h2, p { background:#CAFBE0;}  
  7.      </style>  
  8. </head>  
  9. <body>  
  10.     <h2>Some Stuff</h2>  
  11.     <p>Stuff inside my paragraph...</p>  
  12.     <h3>More Stuff</h3>  
  13.     <p>Stuff inside my paragraph...</p>  
  14. </body>  
  15. </html> 
group-combinator-in-HTML.png

CSS descendant Combinator example
 
The CSS descendant combinator ( X Y { } ) is used to target and style descendants of elements using hierarchy logic.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.      <title>CSs Selectors</title>  
  5.      <style type="text/css">  
  6.         div p span { color:green;}  
  7.      </style>  
  8. </head>  
  9. <body>  
  10.     <div>  
  11.         <h3>Some Stuff</h3>  
  12.         <p>Stuff inside my paragraph...<span>Green Text....</span></p>  
  13.     </div>  
  14. </body>  
  15. </html> 
descendant-combinator-in-HTML.png

CSS adjacent sibling Combinator example
 
The CSS adjacent sibling combinator ( X + Y { } ) is used to target and style the first elements of a specified type if they happen to be preceded by other specified elements.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>CSS Selectors</title>  
  5.     <style type="text/css">  
  6.         h3 + p { font-family:"Comic Sans MS", cursive; text-decoration:underline;}  
  7.     </style>  
  8. </head>  
  9. <body>  
  10.     <h3>Some Stuff</h3>  
  11.     <p>Stuff inside my paragraph...</p>  
  12.     <p>Stuff inside my paragraph...</p>  
  13.     <p>Stuff inside my paragraph...</p>  
  14.     <h3>More Stuff</h3>  
  15.     <p>Stuff inside my paragraph...</p>  
  16.     <p>Stuff inside my paragraph...</p>  
  17.     <p>Stuff inside my paragraph...</p>  
  18. </body>  
  19. </html> 
adjacent-sibling-combinator-in-HTML.png

CSS general sibling Combinator example
 
The CSS general sibling combinator ( X ~ Y { } ) is used to target and style all elements specified if they happen to be preceded by other specified elements.
  1. <html>  
  2. <head>  
  3.      <title>CSS Selector</title>  
  4.      <style type="text/css">  
  5.          h3 ~ p{ font-style:italic; color:Maroon;}  
  6.      </style>  
  7. </head>  
  8. <body>  
  9.     <h2>Some Stuff....</h2>  
  10.     <p>Stuff inside my paragraph...</p>  
  11.     <p>Stuff inside my paragraph...</p>  
  12.     <h3>Some Stuff....</h3>  
  13.     <p>Stuff inside my paragraph...</p>  
  14.     <p>Stuff inside my paragraph...</p>  
  15. </body>  
  16. </html> 
general-sibling-combinator-in-HTML.png

CSS class Selector example
 
The CSS class selector ( X.class { } ) is used to target and style elements that have a common HTML class attribute value in the document.
  • Using it without specifying an element type in front of it will be a universal element type selection for the class.
  • Using it with the element type in front of it will affect only that specified type and not others.
All elements class selection example
  1. <html>  
  2. <head>  
  3.     <title>CSS Selectors</title>  
  4.     <style type="text/css">  
  5.         .myclass{ background-color:#bfdfff; padding:10px; border:#06c 1px solid;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.  <div class="myclass">Content inside my div....</div>  
  10.  <p class="myclass">Content inside my paragraph....</p>  
  11.  <div>Content inside my div....</div>  
  12.  <div class="myclass">Content inside my div....</div>  
  13. </body>  
  14. </html> 
all-elements-class-selection-in-HTML.png

Element type class selection example
  1. <html>  
  2. <head>  
  3.     <title>CSS Selectors</title>  
  4.     <style type="text/css">  
  5.         div.myclass{ background-color:#bfdfff; padding:10px; border:#06c 1px solid;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.  <div class="myclass">Content inside my div....</div>  
  10.  <p class="myclass">Content inside my paragraph....</p>  
  11.  <div>Content inside my div....</div>  
  12.  <div class="myclass">Content inside my div....</div>  
  13. </body>  
  14. </html> 
element-type-class-selection-in-HTML.png

Notes: Even though the paragraph element has the same class name it does not get styled because it is not the specified element while DIV is the specified element to target only if it has that class name. Also, notice how the other DIV element on the page does not get styled because it does not have the class name of "myClass".
 
CSS id Selector example
 
The CSS id selector ( X#id { } or #id { } ) is used to target a unique element in the document. Only one HTML element may be given a unique ID attribute value in your web document, so naturally, this is meant for unique element targeting as opposed to grouped or class styling of elements. If the element type is not specified in front of the "#" symbol then any type of element in the document given this ID attribute value will be styled.
 
If the element type is specified, only that type of element in the document can be styled, while other types of elements that may be given the unique id will be excluded from styling. The ID selectors can be used in conjunction with other selectors to extend its reach into the document.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selectors</title>  
  4.     <style type="text/css">  
  5.         h3#myUnique1{ background-color:#bfdfff; padding-left:10px; border:#06c 1px solid;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.  <h3 id="myUnique1">My heading content....</h3>  
  10.  <h3>My heading content....</h3>  
  11. </body>  
  12. </html> 
 
id-selector1-in-HTML.png
 
The following example demonstrates how you can target any tag by the unique ID attribute value, regardless of the element type, by excluding the type specifier before the "#" symbol. In the example above, the element type must be "h3" in order to be styled.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selectors</title>  
  4.     <style type="text/css">  
  5.         #myUnique1{ background-color:#bfdfff; padding-left:10px; border:#06c 1px solid;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.  <h3>My heading content....</h3>  
  10.  <p id="myUnique1">My heading content....</p>  
  11. </body>  
  12. </html> 
 
id-selector2-in-HTML.png

CSS after Selector example
 
The CSS after pseudo-element selector ( X:: after { } ) is used to generate content to the targeted element or elements directly after the content inside of them renders. Reference the content property to see possible values of content types that can be generated, you can generate more than just string content.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selectors</title>  
  4.     <style type="text/css">  
  5.         h3::after{ content:" is love";}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.  <h3>Joy</h3>  
  10.  <h3>Fun</h3>  
  11.  <h3>Respect</h3>  
  12. </body>  
  13. </html> 
 
after-selector-in-HTML.png

CSS before Selector example
 
The CSS before pseudo-element selector ( X:: before { } ) is used to generate content to the targeted elements directly before the content inside the element is rendered. Reference the content property to see possible values of content types that can be generated, you can generate more than just string content.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selectors</title>  
  4.     <style type="text/css">  
  5.         p::before{ content:"String....";}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.  <p>Paragraph Content....</p>  
  10.  <p>Paragraph Content....</p>  
  11.  <p>Paragraph Content....</p>  
  12. </body>  
  13. </html> 
 
before-selector-in-HTML.png

CSS first-line Selector example
 
The CSS first-line pseudo-element selector ( X::first-line { } ) is used to target the first line within elements.
  1. <!DOCTYPE html>  
  2. <style type="text/css">  
  3. p::first-line {  
  4.     color:#F00;  
  5. }  
  6. </style>  
  7. <p>My paragraph content...<br /> keeps going...<br />and going...</p>  
  8. <ul>  
  9.   <li>List item</li>  
  10.   <li>List item</li>  
  11.   <li>List item</li>  
  12. </ul> 
 
first-line-selector-in-HTML.png

CSS first-letter example
 
The CSS first-letter pseudo-element selector ( X::first-letter { } ) is used to target and style the first letter within elements.
  1. <!DOCTYPE html>  
  2. <style type="text/css">  
  3. p::first-letter {  
  4.     font-family: "Times New Roman", Times, serif;  
  5.     font-size: 24px;  
  6.     color: #09C;  
  7. }  
  8. </style>  
  9. <p>Once upon a time on a website...</p>  
  10. <p>Later that same afternoon a great big...</p> 
 
first-letter-selector-in-HTML.png

CSS not Selector example
 
The CSS not negation pseudo-class selector ( X:not(Y) ) is used to NOT select a specified element type. Use it to exclude a specified element in an otherwise full sibling selection.
  1.  <!DOCTYPE html>  
  2. <style type="text/css">  
  3. #myDiv *:not(p)  
  4. {  
  5.     color:#09C;  
  6. }  
  7. </style>  
  8. <div id="myDiv">  
  9.   <h3>My heading content</h3>  
  10.   <h4>My heading content</h4>  
  11.   <p>My paragraph content...</p>  
  12.   <p>My paragraph content...</p>  
  13. </div> 
 
not-selector-in-HTML.png

CSS active Selector example
 
The CSS active user action pseudo-class selector ( X: active { } ) is used to alter the style of an element at the time of user interaction, usually a click action. The active selector is most commonly used to change the way a menu item or button appears the exact instant it is clicked and remains that style until the user releases their mouse button.
  1. <!DOCTYPE html>  
  2. <style type="text/css">  
  3. .myClass:active {  
  4.     font-size:24px;  
  5.     color:Red;  
  6. }  
  7. </style>  
  8. <p class="myClass">My paragraph content...</p>  
  9. <p>My paragraph content...</p> 
Demo Hint: To see the effect of this click my first paragraph below and watch it grow.
 
CSS hover Selector example
 
The CSS hover user action pseudo-class selector ( X: hover { } ) is used to alter the style of an element when the user mouse hovers over an element on the page. The hover selector is most commonly used to change the way a menu item or button appears when the user mouse hovers over it.
  1. <!DOCTYPE html>  
  2. <style type="text/css">  
  3. .myList li {  
  4.     background:#000;  
  5.     padding:12px;  
  6.     margin:4px;  
  7.     color:#fff;  
  8. }  
  9. .myList li:hover {  
  10.     background:red;  
  11.     padding:12px;  
  12.     margin:4px;  
  13.     color:#000;  
  14. }  
  15. </style>  
  16. <ul class="myList">  
  17.   <li>List Item 1</li>  
  18.   <li>List Item 1</li>  
  19.   <li>List Item 1</li>  
  20. </ul> 
 
hover-selector1-in-HTML.png
 
hover-selector2-in-HTML.png

Demo Hint: To see the effect of this hover your mouse over my list elements.
 

Summary

 
This article enriches the knowledge of CSS.