Pseudo-classes allow adding CSS rules to certain HTML items in a group of matched selectors.
In this tutorial there are presented the pseudo-classes added in CSS3.
To understand CSS3 pseudo-classes, how they work and what is their effect, study the following examples.
nth-child()
pseudo-class matches every element that is the 'nth' child (regardless of type) of its parent. 'n' can be a keyword (odd, even), an integer number, or a number expression of the form (an+b).<style> ul.cls li:nth-child(odd) { background: #abedcd; font-weight: 800; } </style> <h4>Example nth-child(odd)</h4> <ul class='cls'> <li>https://CoursesWeb.net/</li> <li>https://marplo.net/</li> <li>https://gamv.eu/</li> <li>www.google.com/</li> </ul>
<style> ul.cls li:nth-child(-n + 2) { background: #abedcd; font-weight: 800; } </style> <h4>Example nth-child(-n + 2)</h4> <ul class='cls'> <li>https://CoursesWeb.net/</li> <li>https://marplo.net/</li> <li>https://gamv.eu/</li> <li>www.google.com/</li> </ul>
<style> ul li:nth-child(3) { background: #abedcd; font-weight: 800; } </style> <h4>Example nth-child(3)</h4> <ul> <li>https://CoursesWeb.net/</li> <li>https://marplo.net/</li> <li>https://gamv.eu/</li> <li>www.google.com/</li> </ul>
nth-last-child()
pseudo-class matches every element that is the 'nth' child (regardless of type) of its parent, counting from the last child. 'n' can be a keyword (odd, even), an integer number, or a number expression of the form (an+b).<style> ul.cls li:nth-last-child(-n + 2) { background: #bddef8; color: #da0000; } </style> <h4>Example nth-last-child(-n + 2)</h4> <ul class='cls'> <li>https://CoursesWeb.net/</li> <li>https://marplo.net/</li> <li>https://gamv.eu/</li> <li>www.google.com/</li> </ul>
<style> ul li:nth-last-child(1) { background: #abedcd; font-weight:800; } </style> <h4>Example nth-last-child(1)</h4> <ul> <li>https://CoursesWeb.net/</li> <li>https://marplo.net/</li> <li>https://gamv.eu/</li> <li>www.google.com/</li> </ul>
nth-of-type()
pseudo-class matches every element that is the 'nth' child (of a particular type) of its parent, ignoring any children that aren’t of that type. 'n' can be a keyword (odd, even), an integer number higher than 0, or a number expression of the form (an+b).<style> div.cls span:nth-of-type(odd) { background: #01fb00; color: #0001ef; } </style> <h4>Example nth-of-type(odd)</h4> <div class='cls'> <span>CSS pseudo-classes</span> <div>Web Development Courses</div> <span>CSS course</span><br/> <span>HTML - CSS3 tutorials</span> </div>
<style> #theid p:nth-of-type(2n + 0) { background: #01fb00; color: #0001ef; } </style> <h4>Example nth-of-type(2n + 0)</h4> <div id='theid'> <p>CSS pseudo-classes</p> <div>Web Development Courses</div> <p>CSS course</p> <p>HTML - CSS3 tutorials</p> </div>
nth-last-of-type()
pseudo-class matches every element that is the 'nth' child (of a particular type) of its parent, ignoring any children that aren’t of that type; and counting from the last child. 'n' can be a keyword (odd, even), an integer number higher than 0, or a number expression of the form (an+b).<style> #theid p:nth-last-of-type(-n + 2) { background: #01fb00; } </style> <h4>Example nth-last-of-type(-n + 2)</h4> <div id='theid'> <p>CSS pseudo-classes</p> <div>Web Development Courses</div> <p>CSS course</p> <p>HTML - CSS3 tutorials</p> </div>
last-child
pseudo-class matches an element that is the last child of its parent.<style> ul li:last-child { background: #01fb00; border: 2px dashed blue; } </style> <h4>Example last-child</h4> <ul class='cls'> <li>https://CoursesWeb.net/</li> <li>https://marplo.net/</li> <li>https://gamv.eu/</li> <li>www.google.com/</li> </ul>
first-of-type
pseudo-class matches the first child element of the specified type.<style> div>p:first-of-type { background: #01fb00; border: 2px dashed blue; } </style> <h4>Example first-of-type</h4> <div> <p>CSS3 pseudo-classes</p> <div>Web Development Courses</div> <p>Build and design web pages</p> <p>HTML - CSS tutorials</p> </div>
last-of-type
pseudo-class matches the last child element of the specified type.<style> <style type='text/css'> div>p:last-of-type { background: #01fb00; border: 2px dashed blue; } </style> <h4>Example last-of-type</h4> <div> <p>CSS3 pseudo-classes</p> <div>Web Development Courses</div> <p>Build and design web pages</p> <p>HTML - CSS tutorials</p> </div>
only-child
pseudo-class matches an element that’s the only child element of its type.<style> p:only-child { background: #01fb00; } </style> <h4>Example only-child</h4> <div><p>This is a Div with only a child item, a paragraph.</p></div> <div><span>This is a Div with span tag.</span><p>And a paragraph.</p></div>
only-of-type
pseudo-class matches an element that’s the only child element of its type.<style> p:only-of-type { background: #01fb00; } </style> <h4>Example only-of-type</h4> <div><p>This is a Div with two child items, two paragraphs.</p><p>Second P child.</p></div> <div><p>This is another Div with two child items, a paragraph.</p><span>And a span tag.</span></div>
root
pseudo-class matches the document’s root element. In HTML documents, this selector matches the html element.<style> :root { background: #99ef99; } </style> <h4>Example root pseudo-class</h4> <p>This is an iframe.</p>
empty
pseudo-class matches elements that have no children. A text node is considered empty if it has a data length of zero; so, for example, a text node with a single space isn’t empty.<style> ul li:empty { width: 2em; height: 1em; background: #01fb00; } </style> <h4>Example empty pseudo-class</h4> <ul> <li>https://CoursesWeb.net/</li> <li></li> <li>https://gamv.eu/</li> <li></li> </ul>
target
pseudo-class matches an element that’s the target of a fragment identifier in the page`s URI. The fragment identifier in a URI comprises a # character followed by an identifier name (https://coursesweb.net/css/css3-pseudo-classes#nthlc) that matches the value of an ID attribute of an element within the web page (<li id='nthlc'></li>).<style> :target { background: #01fb00; font-weight: 800; } </style> <h4>Example target pseudo-class</h4> <ul> <li id='other_id'>https://marplo.net/</li> <li id='target_id'>https://CoursesWeb.net/</li> <li id='another_id'>www.php.net/</li> </ul>
enabled
pseudo-class matches enabled elements. An element is enabled when it can be selected, clicked on, or accept text input (mostly used on form elements).<style> #formid input:enabled { background: #01fb00; font-weight: 800; } </style> <h4>Example enabled pseudo-class</h4> <form action='#' method='post' id='formid'> <input type='password' value='abcxyz'/> <input type='text' disabled='disabled' value='disabled' /> <input type='button' value='Button' /> </form>
disabled
pseudo-class matches disabled elements. An element is disabled when it cannot be selected, clicked on, or accept text input (mostly used on form elements).<style> #formid input:disabled { background: #01fb00; font-weight: 800; } </style> <h4>Example disabled pseudo-class</h4> <form action='#' method='post' id='formid'> <input type='text' value='enabled'/> <input type='text' disabled='disabled' value='disabled' /> <input type='button' value='Button' /> </form>
checked
pseudo-class matches elements like checkboxes, radio buttons or option in <select> that are checked.<style> input.cls { display: none; } input.cls:checked { display: block; } #lchb { cursor: pointer; } </style> <h4>Example CSS checked</h4> <blockquote>Click on the label.</blockquote> <input type='checkbox' id='chb' class='cls'/> <label id='lchb' for='chb'>Label - Show hidden checkbox</label>
not(s)
pseudo-class, also known as the negation pseudo-class; matches elements that aren’t matched by the specified selector (s).<style> ul:not(.clas) { background: #abedcd; font-style: italic; } </style> <h4>Example CSS not()</h4> <ul> <li>https://CoursesWeb.net/</li> <li>This UL has no class</li> </ul><br> <ul class='clas'> <li>https://gamv.eu/</li> <li>This UL has class='clas'</li> </ul>
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net