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:
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.
- <html>
- <head>
- <title>CSS-Selector</title>
- <link href="style.css" rel="Stylesheet" type="text/css" />
- <style>
- *:lang(es) {color:#090; font-style:italic;}
- </style>
- </head>
- <body>
- <p>My paragraph content....</p>
- <p lang="es">Mi punto contenido....</p>
- <p>My paragraph content....</p>
- <p lang="es">Mi punto contenido....</p>
- </body>
- </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.
- <html>
- <head>
- <title>CSS Selector</title>
- <style type="text/css">
- *:disabled {background-color:#000000; border:red 1px dashed;}
- </style>
- </head>
- <body>
- <input type="text" name="firstname" disabled="disabled">
- <br /><br />
- <textarea name="comment" disabled="disabled"></textarea>
- </body>
- </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.
- <html>
- <head>
- <title>CSS Selector</title>
- <style type="text/css">
- input, textarea:enabled {background-color:#def; border:#80bfff 1px solid; padding:5px;}
- </style>
- </head>
- <body>
- <input type="text" name="firstname">
- <br /><br />
- <textarea name="comment"></textarea>
- <br />
- <br />
- <button type="button">Submit</button>
- </body>
- </html>

CSS checked Selector example
The CSS checked pseudo selector ( X:checked { } ) is used to target elements that have a "checked" state.
- <html>
- <head>
- <title>CSS Selector</title>
- <style type="text/css">
- input:checked { cursor: help;}
- </style>
- </head>
- <body>
- <input type="checkbox" name="cb1">
- <input type="checkbox" name="cb12" checked="checked">
- </body>
- </html>
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:
- <html>
- <head>
- <title>CSS Selector</title>
- <style type="text/css">
- div p:nth-child(3) { color:Red;}
- </style>
- </head>
- <body>
- <div>
- <p>Paragraph Content....</p>
- <p>Paragraph Content....</p>
- <p>Paragraph Content....</p>
- </div>
- </body>
- </html>

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.
- <html>
- <head>
- <title>CSS Selector</title>
- <style type="text/css">
- .myTable { width:100%; border-collapse:collapse; }
- .myTable td { padding:8px; border:#999 1px solid; }
- .myTable tr:nth-child(2n+0) { background:red;}
- .myTable tr:nth-child(2n+1) {background: green;}
- </style>
- </head>
- <body>
- <table class="myTable">
- <tr>
- <td>Row 1 content</td>
- </tr>
- <tr>
- <td>Row 2 content</td>
- </tr>
- <tr>
- <td>Row 3 content</td>
- </tr>
- <tr>
- <td>Row 4 content</td>
- </tr>
- </table>
- </body>
- </html>

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.
- <html>
- <head>
- <title>CSS Selector</title>
- <style>
- ul li:nth-last-child(2) { color:red; font-weight:bold;}
- </style>
- </head>
- <body>
- <ul>
- <li>list item</li>
- <li>list item</li>
- <li>list item</li>
- <li>list item</li>
- <li>list item</li>
- <li>list item</li>
- </ul>
- <hr />
- <ul>
- <li>list item</li>
- <li>list item</li>
- <li>list item</li>
- <li>list item</li>
- <li>list item</li>
- <li>list item</li>
- </ul>
- </body>
- </html>











Sonia Bhadouria VishvkarmaPosted Nov 8, 2012, 11:21 PM
Yes....
Anil KumarPosted Nov 7, 2012, 4:56 AM
Does it works in all version?