CSS Selectors in HTML : Part 2

CSS Selectors in HTML : Part 2

 
Today, we are going to explore CSS Selectors.
 
This is the second part of my article. If you have not read the previous article then please go through the first part of that article:
  1.  CSS Selectors in HTML : Part 1
CSS language selector example
 
The CSS language pseudo-class selector ( X:lang() { } ) is used to target and style elements where their HTML "lang" attribute value matches the selectors defined language code.
 
In the example below only the Spanish language, HTML elements will be selected and styled independently because they have the HTML language attribute set for that particular language.
  1. <html>  
  2. <head>  
  3.     <title>CSS-Selector</title>  
  4.     <link href="style.css" rel="Stylesheet" type="text/css" />  
  5.     <style>  
  6.             *:lang(es) {color:#090; font-style:italic;}  
  7.     </style>  
  8. </head>  
  9. <body>  
  10. <p>My paragraph content....</p>  
  11. <p lang="es">Mi punto contenido....</p>  
  12. <p>My paragraph content....</p>  
  13. <p lang="es">Mi punto contenido....</p>  
  14. </body>  
  15. </html> 
CSS disabled Selector example
 
The CSS disabled UI element states selector ( X:disabled { } ) is used to target and style disabled HTML form input elements. Where applicable form input elements are set to "disabled" they will receive your specified style properties.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style type="text/css">  
  5.          *:disabled {background-color:#000000; border:red 1px dashed;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.     <input type="text" name="firstname" disabled="disabled">  
  10.     <br /><br />  
  11.     <textarea name="comment" disabled="disabled"></textarea>  
  12. </body>  
  13. </html> 
In the code example above we are using the Universal Selector together with the Disabled Selector to style any and all elements that happened to be set as disabled in the document.
 
CSS enabled Selector example
 
The CSS enabled UI element states selector ( X:enabled { } ) is used to target and style enabled HTML form elements. Form elements are enabled by default unless they have the HTML "disabled" attribute set on them.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style type="text/css">  
  5.          input, textarea:enabled {background-color:#def; border:#80bfff 1px solid; padding:5px;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.     <input type="text" name="firstname">  
  10.     <br /><br />  
  11.     <textarea name="comment"></textarea>  
  12.     <br />  
  13.     <br />     
  14.     <button type="button">Submit</button>  
  15. </body>  
  16. </html> 
enabled-selector-in-HTML.png
 
CSS checked Selector example
 
The CSS checked pseudo selector ( X:checked { } ) is used to target elements that have a "checked" state.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style type="text/css">  
  5.          input:checked { cursor: help;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.     <input type="checkbox" name="cb1">  
  10.     <input type="checkbox" name="cb12" checked="checked">  
  11. </body>  
  12. </html> 
 
checked-selector-in-HTML.png
 
CSS nth-child Selector example
 
The CSS nth-child structural pseudo-class selector ( X:nth-child() { } ) is used to target and style child elements according to their position in their specified parent element.
 
Define the numeric value for position in between the parenthesis; see:
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style type="text/css">  
  5.         div p:nth-child(3) { color:Red;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.     <div>  
  10.         <p>Paragraph Content....</p>  
  11.         <p>Paragraph Content....</p>  
  12.         <p>Paragraph Content....</p>  
  13.     </div>  
  14.    
  15. </body>  
  16. </html> 
 
nth-child-selector1-in-HTML.png
 
Using numeric values with "n", "+" and "-" will allow you to set style logic for many selection methods. You can use the keywords of "odd" and "even" for alternating styles every other element.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style type="text/css">  
  5.         .myTable { width:100%; border-collapse:collapse; }  
  6.         .myTable td { padding:8px; border:#999 1px solid; }  
  7.         .myTable tr:nth-child(2n+0) { background:red;}  
  8.         .myTable tr:nth-child(2n+1) {background: green;}  
  9.     </style>  
  10. </head>  
  11. <body>  
  12. <table class="myTable">  
  13.   <tr>  
  14.     <td>Row 1 content</td>  
  15.   </tr>  
  16.   <tr>  
  17.     <td>Row 2 content</td>  
  18.   </tr>  
  19.   <tr>  
  20.     <td>Row 3 content</td>  
  21.   </tr>  
  22.   <tr>  
  23.     <td>Row 4 content</td>  
  24.   </tr>  
  25. </table>  
  26. </body>  
  27. </html> 
 
nth-child-selector2-in-HTML.png
 
CSS nth-last-child Selector example
 
The CSS nth-last-child structural pseudo-class selector ( X:nth-last-child() { } ) is very similar to the X:nth-child() selector except that it counts inward from the last element of type.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style>  
  5.         ul li:nth-last-child(2) { color:red; font-weight:bold;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9. <ul>  
  10.     <li>list item</li>  
  11.     <li>list item</li>  
  12.     <li>list item</li>  
  13.     <li>list item</li>  
  14.     <li>list item</li>  
  15.     <li>list item</li>  
  16. </ul>  
  17. <hr />  
  18. <ul>  
  19.     <li>list item</li>  
  20.     <li>list item</li>  
  21.     <li>list item</li>  
  22.     <li>list item</li>  
  23.     <li>list item</li>  
  24.     <li>list item</li>  
  25. </ul>  
  26. </body>  
  27. </html> 
 
nth-last-child-selector-in-HTML.png
 
CSS nth-of-type Selector example
 
The CSS nth-of-type structural pseudo-class selector ( X:nth-of-type() { } ) is used to target and style specified child elements according to their position in their specified parent element. This selector is similar to the X:nth-child() selector, but differs by only counting specified elements to make selections and it does not count other element types in the parent.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style type="text/css">  
  5.         div p:nth-of-type(3) { color:#F0F; font-weight:bold;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.     <div>  
  10.       <h3>Heading content</h3>  
  11.       <p>Paragraph content...</p>  
  12.       <p>Paragraph content...</p>  
  13.       <p>Paragraph content...</p>  
  14.     </div>  
  15.     <hr />  
  16.     <div>  
  17.       <h3>Heading content</h3>  
  18.       <p>Paragraph content...</p>  
  19.       <p>Paragraph content...</p>  
  20.       <p>Paragraph content...</p>  
  21.     </div>  
  22. </body>  
  23. </html> 
 
nth-of-type-selector-in-HTML.png
 
CSS nth-last-of-type Selector example
 
The CSS nth-last-of-type structural pseudo-class selector ( X:nth-last-of-type() { } ) is used to target and style specified child elements according to their position in their specified parent element. This selector is similar to the X:nth-of-type() selector, except that it starts counting from the last element instead of counting from the first element.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style type="text/css">  
  5.          div p:nth-last-of-type(3) { color:#F0F; font-weight:bold;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.     <div>  
  10.       <h3>Heading content</h3>  
  11.       <p>Paragraph content...</p>  
  12.       <p>Paragraph content...</p>  
  13.       <p>Paragraph content...</p>  
  14.     </div>  
  15.     <hr />  
  16.     <div>  
  17.       <h3>Heading content</h3>  
  18.       <p>Paragraph content...</p>  
  19.       <p>Paragraph content...</p>  
  20.       <p>Paragraph content...</p>  
  21.     </div>  
  22. </body>  
  23. </html> 
 
nth-last-of-type-selector-in-HTML.png
 
CSS nth-first-child Selector example
 
The CSS first-child structural pseudo-class selector ( X:first-child { } ) is used to target and style the first child of its parent element.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style>  
  5.             div:first-child { color:Red; font-weight:bold;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.     <div>  
  10.         <p>Paragraph Content....</p>  
  11.         <p>Paragraph Content....</p>  
  12.     </div>  
  13.     <div>  
  14.         <p>Paragraph Content....</p>  
  15.         <p>Paragraph Content....</p>  
  16.     </div>  
  17.    
  18. </body>  
  19. </html> 
 
first-child-selector1-in-HTML.png
 
or
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style>  
  5.             div p:first-child { color:#f0f; font-weight:bold;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.     <div>  
  10.         <p>Paragraph Content....</p>  
  11.         <p>Paragraph Content....</p>  
  12.     </div>  
  13.     <div>  
  14.         <p>Paragraph Content....</p>  
  15.         <p>Paragraph Content....</p>  
  16.     </div>  
  17.    
  18. </body>  
  19. </html> 
 
first-child-selector2-in-HTML.png
 
CSS last-child Selector example
 
The CSS last-child structural pseudo-class selector ( X:last-child { } ) is used to target and style the last child of its parent element.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style type="text/css">  
  5.         #mydiv p:last-child {color:#F0F;font-weight:bold;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9.     <div id="mydiv">  
  10.       <p>Paragraph content...</p>  
  11.       <p>Paragraph content...</p>  
  12.       <p>Paragraph content...</p>  
  13.       <p>Paragraph content...</p>  
  14.     </div>  
  15. </body>  
  16. </html> 
 
last-child-selector-in-HTML.png
 
CSS first-of-type Selector example
 
The CSS first-of-type structural pseudo-class selector ( X:first-of-type ) is used to target and style the first occurrence of the specified element inside of a specified parent element. It is exactly the same as using X:nth-of-type(1) with a value of "1".
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style>  
  5.         div p { background-color:blue;}  
  6.         div p:first-of-type { background-color:yellow;}  
  7.     </style>  
  8. </head>  
  9. <body>  
  10. <div>  
  11.       <h3>Heading content...</h3>  
  12.       <p>Paragraph content...</p>  
  13.       <p>Paragraph content...</p>  
  14.       <p>Paragraph content...</p>  
  15.     </div>  
  16. </body>  
  17. </html> 
 
first-of-type-selector-in-HTML.png
 
CSS last-of-type Selector example
 
The CSS last-of-type structural pseudo-class selector ( X:last-of-type ) is used to target and style the last occurrence of the specified element inside of a specified parent element. It is exactly the same as using X:nth-last-of-type(1) with a value of "1".
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style>  
  5.         ol li{ background-color:blue;}  
  6.         ol li:last-of-type { background-color:yellow;}  
  7.     </style>  
  8. </head>  
  9. <body>  
  10. <ol>  
  11.     <li>list item</li>  
  12.     <li>list item</li>  
  13.     <li>list item</li>  
  14. </ol>  
  15. </body>  
  16. </html>
last-of-type-selector-in-HTML.png
 
CSS only-child Selector example
 
The CSS only-child structural pseudo-class selector ( X:only-child ) is used to target and style an element if it is an only child of its parent element.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style>  
  5.         div p:only-child{ background-color:#fca7e7;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9. <div>  
  10.     <p>Content....</p>  
  11.     <span>Content....</span>  
  12. </div>  
  13. <div>  
  14.     <p>Content....</p>  
  15. </div>  
  16. </body>  
  17. </html> 
 
only-child-selector-in-HTML.png
 
CSS empty Selector example
 
The CSS empty structural pseudo-class selector ( X:empty ) is used to target and style an empty element that has no child elements including text nodes.
  1. <html>  
  2. <head>  
  3.     <title>CSS Selector</title>  
  4.     <style>  
  5.         ul li:empty { background-color:#ccc;}  
  6.     </style>  
  7. </head>  
  8. <body>  
  9. <ul>  
  10.     <li>list item....</li>  
  11.     <li></li>  
  12.     <li>list item....</li>  
  13. </ul>  
  14. </body>  
  15. </html> 
 
empty-selector-in-HTML.png
 

Summary

 
This article is useful in styling elements using the CSS tag in HTML.