CSS - tutorial - 08 - pseudo-classes

Revision:


Content

a pseudo-class is used to define a special state of an element. CSS pseudo-classes: anchor pseudo-classes linguistic pseudo-classes location pseudo-classes user action pseudo-classes time-dimensional pseudo-classes resource state pseudo-classes input pseudo-classes tree-structural pseudo-classes :is() pseudo-class and :where() pseudo-class :not() pseudo-class :focus() pseudo-class :nt-of microsyntax: pre-filter a set of child elements :has() pseudo-class: a powerfull new CSS capability styling without JS, using pseudo-classes :scope pseudo-class :hover pseudo-class select any child :target pseudo-class link-related pseudo-classes focus-related pseudo-classes :autofill :lang :checked :default pseudo-class :fullscreen :indeterminate :invalid some practical examples


a pseudo-class is used to define a special state of an element.

top

The ":" pseudo allows the selection of elements based on state information that is not contained in the document tree.

For example, "a:visited" will match all <a> elements that have been visited by the user.

While defining pseudo-classes in a <style>...</style> block, following points should be noted:

"a:hover" MUST come after "a:link" and "a:visited" in the CSS definition in order to be effective.
"a:active" MUST come after "a:hover" in the CSS definition in order to be effective.
Pseudo-class names are not case-sensitive.

Pseudo-classes are different from CSS classes but they can be combined.

Syntax: selector:pseudo-class {property: value;}

using CSSS classes with pseudo-classes : syntax : selector.class:pseudo-class {property: value}

Like regular classes, pseudo-classes can be chained together in a selector, as many as needed.


CSS pseudo-classes:

top

:active : selects the active link

The :active selector is used to select and style the active link. A link becomes active when you click on it. The :active selector can be used on all elements, not only links.

Syntax: :active {css declarations;}

P.S. Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :hover selector to style links when you mouse over them.

Note: ":active" MUST come after ":hover" (if present) in the CSS definition in order to be effective!

examples

my website: lwitters.com wikipedia.org
code:
                    <style>
                        a:active {background-color: yellow;}
                    </style>
                    <div style="margin-left:2vw;">
                        <a style="color:blue" href="https://www.lwitters.com">my website: lwitters.com</a>           
                        <a style="color:blue" href="http://www.wikipedia.org">wikipedia.org</a>
                    </div>  
                

:checked : selects every checked element

The :checked selector matches every checked <input> element (only for radio buttons and checkboxes) and <option> element.

Syntax: :checked {css declarations;}

examples

Male
Female
I have a bike
I have a car
code:
                    <style>
                        input.yes:checked { height: 3vw; width: 3vw;}
                    </style>
                    <div>
                        <form action="">
                            <input class="yes" type="radio" checked="checked" value="male" 
                            name="gender"> Male<br>
                            <input class="yes" type="radio" value="female" name="gender"> Female<br>
                            <input class="yes" type="checkbox" checked="checked" value="Bike"> 
                            I have a bike<br>
                            <input class="yes" type="checkbox" value="Car"> I have a car 
                        </form>
                    </div>
                

:disabled : selects every disabled element

The :disabled selector matches every disabled element (mostly used on form elements).

Syntax: :disabled {css declarations;}

examples

First name:
Last name:
Country:
code:
                    <style>
                        input[type=text]:enabled {background: #ffff00;}
                        input[type=text]:disabled {background: #dddddd;}
                    </style>
                    <div>
                        <form action="">
                            First name: <input type="text" value="Mickey"><br>
                            Last name: <input type="text" value="Mouse"><br>
                            Country: <input type="text" disabled="disabled" value="Disneyland">
                        </form>
                    </div>
                

:empty : selects every element that has no children

The :empty selector matches every element that has no children (including text nodes).

Syntax: :empty {CSS declarations}

examples

A paragraph.

Another paragraph.

code:
                    <style>
                        p:empty { width: 10vw; height: 2vw; background: #ff0000;}
                    </style>  
                    <div>
                        <p class="spec-1"></p>
                        <p class="spec-1">A paragraph.</p>
                        <p class="spec-1">Another paragraph.</p>
                    </div>  
                

:enabled : selects every enabled element

The :enabled selector matches every enabled element (mostly used on form elements).

Syntax: :enabled {CSS declarations}

examples

First name:
Last name:
Country:
code:
                    <style>
                        input.five[type=text]:enabled {background: #ffff00;}
                        input.five[type=text]:disabled {background: #dddddd;}
                    </style>  
                    <div>
                        <form action="">
                            First name: <input class="five" type="text" value="Mickey"><br>
                            Last name: <input class="five" type="text" value="Mouse"><br>
                            Country: <input type="text" disabled="disabled" value="Disneyland">
                        </form>
                    </div>
                

:first-child : selects every element that is the first child of its parent

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

Syntax: :first-child {CSS declarations}

examples

I am a strong man. I am a strong man.

I am a strong man. I am a strong man.

code:
                    <style>
                        p:first-child i {background: lightcyan;}
                    </style>  
                    <div>
                        <p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p>
                        <p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p>
                    </div>
                

:first-of-type : selects every element that is the first element of its parent ;

The :first-of-type selector matches every element that is the first child, of a particular type, of its parent.

This is the same as :nth-of-type(1).

Syntax: :first-of-type {CSS declarations}

examples

The first paragraph.

The second paragraph.

The third paragraph.

The fourth paragraph.

code:
                <style>
                    .special1 > p.three:first-of-type {background: lightblue;}
                </style>  
                <div class="special1">
                    <p class="three">The first paragraph.</p>
                    <p class="three">The second paragraph.</p>
                    <p class="three">The third paragraph.</p>
                    <p class="three">The fourth paragraph.</p>
                </div>
            

:focus : selects the element that has focus

The :focus selector is used to select the element that has focus.

The :focus selector is allowed on elements that accept keyboard events or other user inputs.

Syntax: :focus {CSS declarations}

examples

Click inside the text fields to see a lightbrown background:

First name:
Last name:
code:
                    <style>
                        input.four:focus {background-color: burlywood;}
                    </style>  
                    <div>
                        <p class="spec-1">Click inside the text fields to see a lightbrown background:</p>
                        <form>
                            First name: <input class="four" type="text" name="firstname"><br>
                            Last name: <input class="four" type="text" name="lastname">
                        </form>
                    </div>
                

:hover : selects links on mouse over

The :hover selector is used to select elements when you mouse over them. The :hover selector can be used on all elements, not only on links.

Syntax: :hover {CSS declarations}

P.S. use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :active selector to style the active link.

Note: ":hover" MUST come after ":link" and ":visited" (if they are present) in the CSS definition, in order to be effective!

examples

Mouse over the links to see them change layout.

This link changes color

This link changes font-size

This link changes background-color

This link changes font-family

This link changes text-decoration

code
                    <style>
                        a.ex1:hover, a.ex1:active {color: red;}
                        a.ex2:hover, a.ex2:active {font-size: 150%;}
                        a.ex3:hover, a.ex3:active {background: red;}
                        a.ex4:hover, a.ex4:active {font-family: monospace;}
                        a.ex5:visited, a.ex5:link {text-decoration: none;}
                        a.ex5:hover, a.ex5:active {text-decoration: underline;}
                    </style>  
                    <div>
                        <p class="spec-1">Mouse over the links to see them change layout.</p>
                        <p class="spec-1"><a class="ex1" href="default.asp">This link changes color</a></p>
                        <p class="spec-1"><a class="ex2" href="default.asp">This link changes font-size</a></p>
                        <p class="spec-1"><a class="ex3" href="default.asp">This link changes background-color</a></p>
                        <p class="spec-1"><a class="ex4" href="default.asp">This link changes font-family</a></p>
                        <p class="spec-1"><a class="ex5" href="default.asp">This link changes text-decoration</a></p>
                    </div>
                

:in-range : selects elements with a value within a specified range

The :in-range selector selects all elements with a value that is within a specified range. The :in-range selector only works for input elements with min and/or max attributes!

Syntax: :in-range {CSS declarations}

P.S. use the :out-of-range selector to select all elements with a value that is outside a specified range.

examples

Try typing a number out of range (less than 5 or higher than 10), to see the styling disappear.

code:
                    <style>
                        input:in-range { border: 1vw dashed blue;}
                    </style>  
                    <div>
                        <input style="margin-left:2vw;" type="number" min="5" max="10" value="7">
                        <p class="spec-1">Try typing a number out of range (less than 5 or higher than 10), to see the styling disappear.</p>
                    </div>
                

:invalid : selects all elements with an invalid value

The :invalid selector selects form elements with a value that does not validate according to the element's settings.

Syntax: :invalid {CSS declarations}

The :invalid selector only works for form elements with limitations, such as input elements with min and max attributes, email fields without a legal email, or number fields without a numeric value, etc.

P.S. use the :valid selector to select form elements with a value that validates according to the element's settings.

examples

Try typing a legal e-mail address, to see the styling disappear.

code:
                <style>
                    input:invalid { border: 1vw solid red;}
                </style>  
                <div>
                    <input style="margin-left:2vw;" type="email" value="supportEmail">
                    <p>Try typing a legal e-mail address, to see the styling disappear.</p>
                </div>  
            

:lang(language) : selects every element with a specified lang attribute value

The :lang() selector is used to select elements with a lang attribute with the specified value.

Syntax: :lang(language code) {CSS declarations}

Note: The lang attribute value is most often a two-letter language code, like lang="fr" (for French), or two language codes combined, like lang="fr-ca" (for Canadian French).

examples

Ik ben geboren in Belgie.

Goedendag

code:
                    <style>
                        p:lang(nl) {background: lightblue;}
                    </style>  
                    <div>
                        <p class="spec-1">Ik ben geboren in Belgie.</p>                                                   
                        <p class="spec-1" lang="nl">Goedendag</p>
                    </div>  
                

:last-child :selects every element that is the last child of its parent

The :last-child selector matches every element that is the last child of its parent.

Syntax: :last-child {CSS declarations}

Tip: p:last-child is equal to p:nth-last-child(1).

examples

The first paragraph.

The second paragraph.

The third paragraph.

The fourth paragraph.

code:
                    <style>
                        p:last-child {background: lightgreen;}
                    </style>  
                    <div>
                        <p class="ten">The first paragraph.</p>
                        <p class="ten">The second paragraph.</p>
                        <p class="ten">The third paragraph.</p>
                        <p class="ten">The fourth paragraph.</p>
                    </div>  
                

:last-of-type : selects every element that is the last element of its parent

The:last-of-type selector matches every element that is the last child, of a particular type, of its parent.

Syntax: :last-of-type {CSS declarations}

Tip: this is the same as :nth-last-of-type(1).

examples

The first paragraph.

The second paragraph.

The third paragraph.

The fourth paragraph.

code:
                    <style>
                        p.eleven:last-of-type {background: lightcyan;}
                    </style>  
                    <div>
                        <p class="eleven">The first paragraph.</p>
                        <p class="eleven">The second paragraph.</p>
                        <p class="eleven">The third paragraph.</p>
                        <p class="eleven">The fourth paragraph.</p>                                          
                    </div>  
                

:link : selects all unvisited links

The :link selector is used to select unvisited links.

Syntax: :link {CSS declarations}

Note: The :link selector does not style links you have already visited.

P.S.: use the :visited selector to style links to visited pages, the :hover selector to style links when you mouse over them, and the :active selector to style links when you click on them.

examples

my website Wikipedia
code:
                    <style>
                        a.twelve:link { background-color: yellow;}
                    </style>  
                    <div style="margin-left: 2vw;">
                        <a class="twelve" href="https://www.lwitters.com">my website</a>                   
                        <a class="twelve" href="http://www.wikipedia.org">Wikipedia</a>
                    </div>  
                

:not(selector) : selects every element that is not a specified element

The :not(selector) selector matches every element that is NOT the specified element/selector.

Syntax: :not(selector) {CSS declarations}

examples

This is a heading

This is a paragraph.

This is another paragraph.

This is some text in a div element.
Link to my website!
code:
                    <style>
                        p.one {color: #000000;}
                        .one:not(p) {color: #ff0000;}
                    </style>  
                    <div>
                        <h3 class="one">This is a heading</h3>
                        <p class="one">This is a paragraph.</p>
                        <p class="one">This is another paragraph.</p>
                        <div style="margin-left:2vw;" class="one">This is some text in a div element.</div>
                        <a class="one" style="margin-left:2vw;" href="https://www.lwitters.com" target="_blank">
                        Link to my website!</a>
                    </div>  
                

:nth-child(n) : selects every element that is the second child of its parent

The :nth-child(n) selector matches every element that is the nth child, regardless of type, of its parent. n can be a number, a keyword, or a formula.

Syntax: :nth-child(number){CSS declarations}

P.S.: look at the :nth-of-type() selector to select the element that is the nth child, of a particular type, of its parent.

examples

The first paragraph.

The second paragraph.

The third paragraph.

code:
                    <style>
                        .special3 > p.two2:nth-child(odd) {background: lightcyan;}
                        .special3 > p.two2:nth-child(even) {background: lightblue;}
                    </style>  
                    <div class="special3">
                        <p class="two2">The first paragraph.</p>
                        <p class="two2">The second paragraph.</p>
                        <p class="two2">The third paragraph.</p>
                    </div>
                

The first paragraph.

The second paragraph.

The third paragraph.

The fourth paragraph.

The fifth paragraph.

The sixth paragraph.

The seventh paragraph.

The eight paragraph.

The ninth paragraph.

code:
                    <style>
                        .special4 > p.three:nth-child(3n+0) {background: red;}
                    </style>  
                    <div class="special4">
                        <p class="three">The first paragraph.</p>
                        <p class="three">The second paragraph.</p>
                        <p class="three">The third paragraph.</p>
                        <p class="three">The fourth paragraph.</p>
                        <p class="three">The fifth paragraph.</p>
                        <p class="three">The sixth paragraph.</p>
                        <p class="three">The seventh paragraph.</p>
                        <p class="three">The eight paragraph.</p>
                        <p class="three">The ninth paragraph.</p>
                    </div>
                

:nth-last-child(n) : selects every element that is the second child of its parent, counting from the last child

Syntax: :nth-last-child(number){CSS declarations}

examples

The first paragraph.

The second paragraph.

The third paragraph.

The fourth paragraph.

The fifth paragraph.

The sixth paragraph.

The seventh paragraph.

The eight paragraph.

The ninth paragraph.

code:
                    <style>
                        .special4A > p.three3:nth-child(2n+0) {background: red;}
                    </style>  
                    <div class="special4A">
                        <p class="three3">The first paragraph.</p>
                        <p class="three3">The second paragraph.</p>
                        <p class="three3">The third paragraph.</p>
                        <p class="three3">The fourth paragraph.</p>
                        <p class="three3">The fifth paragraph.</p>
                        <p class="three3">The sixth paragraph.</p>
                        <p class="three3">The seventh paragraph.</p>
                        <p class="three3">The eight paragraph.</p>
                        <p class="three3">The ninth paragraph.</p>
                    </div>
                

:nth-last-of-type(n) : selects every element that is the second element of its parent, counting from the last child

The :nth-last-of-type(n) selector matches every element that is the nth child, of a particular type, of its parent, counting from the last child. n can be a number, a keyword, or a formula.

Syntax: :nth-last-of-type(number) {CSS declarations}

P.S.: look at the :nth-last-child() selector to select the element that is the nth child, regardless of type, of its parent, counting from the last child.

examples

The first paragraph.

The second paragraph.

The third paragraph.

The fourth paragraph.

code:
                    <style>
                        p.six:nth-last-of-type(2) {background: lightgreen;}
                    </style>  
                    <div>
                        <p class="six">The first paragraph.</p>
                        <p class="six">The second paragraph.</p>
                        <p class="six">The third paragraph.</p>
                        <p class="six">The fourth paragraph.</p>
                    </div>  
                

The first paragraph.

The second paragraph.

The third paragraph.

The fourth paragraph.

The fifth paragraph.

The sixth paragraph.

The seventh paragraph.

The eight paragraph.

The ninth paragraph.

code:
                    <style>
                        p.seven:nth-last-of-type(3n+0) {background: lightblue;}
                    </style>  
                    <div>
                        <p class="seven">The first paragraph.</p>
                        <p class="seven">The second paragraph.</p>
                        <p class="seven">The third paragraph.</p>
                        <p class="seven">The fourth paragraph.</p>
                        <p class="seven">The fifth paragraph.</p>
                        <p class="seven">The sixth paragraph.</p>
                        <p class="seven">The seventh paragraph.</p>
                        <p class="seven">The eight paragraph.</p>
                        <p class="seven">The ninth paragraph.</p>
                    </div>
                

:nth-of-type(n) : selects every element that is the second element of its parent

The :nth-of-type(n) selector matches every element that is the nth child, of a particular type, of its parent. n can be a number, a keyword, or a formula.

Syntax: :nth-of-type(number) {CSS declarations}

P.S.: look at the :nth-child() selector to select the element that is the nth child, regardless of type, of its parent.

examples

The first paragraph.

The second paragraph.

The third paragraph.

The fourth paragraph.

code:
                    <style>
                        p.eight:nth-of-type(2) {background: lightcyan;}
                    </style>  
                    <div>
                        <p class="eight">The first paragraph.</p>
                        <p class="eight">The second paragraph.</p>
                        <p class="eight">The third paragraph.</p>
                        <p class="eight">The fourth paragraph.</p>
                    </div>
                

The second paragraph.

The third paragraph.

The fourth paragraph.

The fifth paragraph.

The sixth paragraph.

The seventh paragraph.

The eight paragraph.

The ninth paragraph.

code:
                    <style>
                        p.nine:nth-of-type(3n+0) {background: lightblue;}
                    </style>  
                    <div>
                        <p class="nine">The first paragraph.</p>
                        <p class="nine">The second paragraph.</p>
                        <p class="nine">The third paragraph.</p>
                        <p class="nine">The fourth paragraph.</p>
                        <p class="nine">The fifth paragraph.</p>
                        <p class="nine">The sixth paragraph.</p>
                        <p class="nine">The seventh paragraph.</p>
                        <p class="nine">The eight paragraph.</p>
                        <p class="nine">The ninth paragraph.</p>
                    </div>
                

:only-of-type : selects every element that is the only element of its parent

The :only-of-type selector matches every element that is the only child of its type, of its parent.

Syntax: :only-of-type {CSS declarations}

examples

This is a paragraph.

This is a paragraph.

This is a paragraph.

code:
                    <style>
                        p.ten:only-of-type {background: burlywood;}
                    </style>  
                    <div>
                        <div><p class="ten">This is a paragraph.</p></div>
                        <div><p class="ten">This is a paragraph.</p><p class="ten">This is a paragraph.</p></div>
                    </div>  
                

:only-child" : selects every element that is the only child of its parent

The :only-child selector matches every element that is the only child of its parent.

Syntax: :only-child {CSS declarations}

examples

This is a paragraph.

This is a paragraph.

This is a paragraph.

code:
                    <style>
                        p.eleven:only-child {background: brown;}
                    </style>  
                    <div>
                        <div><p class="eleven">This is a paragraph.</p></div>
                        <div><p class="eleven">This is a paragraph.</p><p class="spec eleven">This is a paragraph.</p></div>
                    </div>  
                

:optional : selects elements with no "required" attribute

The :optional selector selects form elements which are optional. Form elements with no "required" attribute are defined as optional.

Syntax: :optional {CSS declarations}

Note: the :optional selector only applies to the form elements: input, select and textarea.

Tip: use the :required selector to select form elements which are required.

examples

An optional input element:

A required input element:

code:
                    <style>
                        input:optional {background-color: yellow;} 
                    </style>  
                    <div>
                        <p class="thirteeen">An optional input element: <input></p>
                        <p class="thirteen">A required input element: <input required></p>                            
                    </div>  
                

:out-of-range : selects elements with a value outside a specified range

The :out-of-range selector selects all elements with a value that is outside a specified range. The :out-of-range selector only works for input elements with min and/or max attributes!

Syntax: :out-of-range {CSS declarations}

Tip: use the :in-range selector to select all elements with a value that is within a specified range.

examples

Try typing a number within the given range (between 5 and 10), to see the styling disappear.

code:
                    <style>
                        input:out-of-range {border: 1vw solid blue;}
                    </style>  
                    <div>
                        <input class="fourteen" style="margin-left:2vw;" type="number" min="5" max="10" value="17">
                        <p>Try typing a number within the given range (between 5 and 10), to see the styling disappear.</p>
                    </div>
                

:read-only : selects elements with a "readonly" attribute specified

The :read-only selector selects elements which are "readonly". Form elements with a "readonly" attribute are defined as "readonly".

Syntax: :read-only {CSS declarations}

examples

A normal input element:

A readonly input element:

code:
                    <style>
                        input:read-only {background-color: yellow;}
                    </style>  
                    <div>
                        <p class="fourteen">A normal input element: <input value="hello"></p>
                        <p class="fourteen">A readonly input element: <input readonly value="hello"></p>
                    </div>  
                

:read-write : selects with no "readonly" attribute

The :read-write selector selects form elements which are "readable" and "writeable". Form elements with no "readonly" attribute, and no "disabled" attribute are defined as "read-" and "write-able".

Syntax: :read-write {CSS declarations}

examples

A normal input element:

A readonly input element:

code:
                    <style>
                        .fifteen input:read-write { background-color: lightgreen;}
                    </style>  
                    <div>
                        <p class="fifteen">A normal input element: <input value="hello"></p>
                        <p class="fifteen">A readonly input element: <input readonly value="hello"></p>
                    </div>  
                

:required : selects elements with a "required" attribute specified

The :required selector selects form elements which are required. Form elements with a required attribute are defined as required.

Syntax: :required {CSS declarations}

Note: the :required selector only applies to the form elements: input, select and textarea.

Tip: use the :optional selector to select form elements which are optional.

examples

An optional input element:

A required input element:

code:
                    <style>
                        .special5 > .sixteen input:required {background-color: orange;}
                    </style>  
                    <div  class="special5">
                        <p class="sixteen">An optional input element: <input></p>
                        <p class="sixteen">A required input element: <input required></p>                        
                    </div>  
                

:root : selects the document's root element

The :root selector matches the document's root element. In HTML, the root element is always the html element.

Syntax: :root {CSS declarations}

examples

code:
                    <style>
                        :root {background: #ff0000;}
                    </style>  
                    <div>
                        <h1>This is a heading</h1>
                    </div>
                

:target : selects the current active element (clicked on a URL containing that anchor name)

URLs with an # followed by an anchor name link to a certain element within a document. The element being linked to is the target element. The :target selector can be used to style the current active target element.

Syntax: :target {CSS declarations}

examples

Jump to New content 1

Jump to New content 2

Click on the links above and the :target selector highlight the current active HTML anchor.

New content 1...

New content 2...

code:
                    <style>
                        :target {border: 2px solid #D4D4D4; background-color: #e5eecc;}
                    </style>  
                    <div>
                        <p><a href="#news1">Jump to New content 1</a></p>
                        <p><a href="#news2">Jump to New content 2</a></p>
                        <p>Click on the links above and the :target selector highlight the current active HTML anchor.</p>
                        <p id="news1"><b>New content 1...</b></p>
                        <p id="news2"><b>New content 2...</b></p>
                    </div>
                    
                

:valid : selects all elements with a valid value

The :valid selector selects form elements with a value that validates according to the element's settings.

Syntax: :valid {CSS declarations}

Note: the :valid selector only works for form elements with limitations, such as input elements with min and max attributes, email fields with a legal email, or number fields with a numeric value, etc.

Tip: use the :invalid selector to select form elements with a value that does not validate according to the element's settings.

examples

code:
                    <style>
                        input:valid {background-color: yellow;}
                    </style>  
                    <div>
                        <input style="margin-left:2vw;" type="email" value="[email protected]">                           
                    </div>  
                

:visited : selects all visited links

The :visited selector is used to select visited links.

Syntax: :visited {CSS declarations}

P.S.: use the :link selector to style links to unvisited pages, the :hover selector to style links when you mouse over them, and the :active selector to style links when you click on them.

Browsers limit the styles that can be set for a:visited links, due to security issues. Allowed styles are: color, background-color, border-color (and border-color for separate sides), outline color, column-rule-color, the color parts of fill and stroke. All other styles are inherited from a:link.

examples

W3Schools Home
W3Schools HTML Tutorial
W3Schools CSS Tutorial
code:
                    <style>
                        a:visited {color: pink;}
                    </style>  
                    <div>
                        <a style="margin-left:2vw;" href="https://www.w3schools.com">W3Schools Home</a><br>
                        <a style="margin-left:2vw;" href="https://www.w3schools.com/html/">W3Schools HTML Tutorial</a><br>
                        <a style="margin-left:2vw;" href="https://www.w3schools.com/css/">W3Schools CSS Tutorial</a> 
                    </div>
                

anchor pseudo-classes

top

:link, :visited, :hover, :active , :focus

visit my website

my website

Your favourite sports?

football basketball
first name:
last name:
code:
                <div>
                    <div id="b"><p>visit my website</p>
                        <a href="https://lwitters.com" target="_blank">my website</a>
                    </div>
                    <div id="ba"><p>Your favourite sports?</p>
                        <a href="#">football</a>
                        <a href="">basketball</a>
                    </div>
                    <form class="spec-1" action="#" method="get">
                        first name: <input type="text" name="fname"><br>
                        last name: <input type="text" name="lname"><br>
                        <input type="submit" value="Submit">
                    </form>
                </div>
                <style>
                    #b, #ba {display: flex; flex-flow: row nowrap; justify-content: left;margin-left: 1vw;}
                    #b a, #ba a {margin: 2vw;padding: 1vw;border: 2px solid black; text-shadow: -1px 0px black, 
                        0px -1px black, 0px 1px black, 1px 0px black; font-size: 1vw;}
                    #b a:link, #ba a:link {text-decoration: none;}
                    #b a:visited, #ba a:visited {color: blueviolet;}
                    #b a:hover, #ba a:hover {color: orange; font-size: 150%; font-weight: bold; box-shadow: 0 0 
                        5px 1px grey;}
                    #b a:active, #ba a:active {color: red; box-shadow: inset 0 0 15px red;}
                    form{margin-left: 1vw; width: 35vw;}
                    input:focus {background-color:green; color:yellow; }
                </style>
            


linguistic pseudo-classes

top

These pseudo-classes reflect the document language, and enable the selection of elements based on language or script direction.

:dir: the directionality pseudo-class selects an element based on its directionality as determined by the document language.

Syntax : :dir([ltr | rtl]) { /* ... */ }

Parameters:

ltr : target left-to-right elements

rtl : target right-to-left elements

examples

test1
test2
ففي
code:
                    <div>
                        <div id="item1" dir="rtl">
                            <span>test1</span>
                            <div id="item2" dir="ltr">
                            test2
                            <div id="item3" dir="auto"> ففي </div>
                            </div>
                        </div>
                    </div>
                    <style>
                        #item1:dir(ltr) {background-color: #1c87c9;}
                        #item2:dir(rtl) {background-color: #8ebf42;}
                    </style>
                

:lang: selects an element based on its content language.

example: see above


location pseudo-classes

top

These pseudo-classes relate to links and to targeted elements within the current document.

:any-link: matches an element if the element would match either :link or :visited.

This pseudo-class selector represents an element that acts as the source anchor of a hyperlink, independent of whether it has been visited. In other words, it matches every <a> or <area> element that has an "href" attribute. Thus, it matches all elements that match :link or :visited.

Syntax : :any-link { /* ... */ }

examples

External link
Internal target link
Placeholder link (won't get styled)
code:
                    <div>
                        <a class="item-A" href="https://lwitters.com.com">External link</a><br />
                        <a class="item-A" href="#">Internal target link</a><br />
                        <a>Placeholder link (won't get styled)</a>
                    </div>
                    <style>
                        .item-A:any-link { border: 0.1vw solid blue; color: orange;}
                        /* WebKit browsers */
                        .item-A:-webkit-any-link {border: 1px solid blue; color: orange;}
                    </style>
                

:link : matches links that have not yet been visited

example: see above

:visited : matches links that have been visited

example: see above

:local-link : matches links whose absolute URL is the same as the target URL, for example anchor links to the same page.

It represents a link to the same document. Therefore an element that is the source anchor of a hyperlink whose target's absolute URL matches the element's own document URL.

Syntax: :local-link {/* ... */}

examples

This is a link on the current page.
This is an external link
code:
                    <div>
                        <a class="item-BB" href="#target">This is a link on the current page.</a><br />
                        <a class="item-BB" href="https://lwitters.com">This is an external link</a><br />
                    </div>
                    <style>
                        a.item-BB:local-link {color: green;}
                    </style>
                

:target : matches the element which is the target of the document URL.

example: see above

:target-within : matches elements which are the target of the document URL, but also elements which have a descendant which is the target of the document URL.

This pseudo-class represents an element that is a target element or contains an element that is a target. A target element is a unique element with an id matching the URL's fragment. In other words, it represents an element that is itself matched by the ":target pseudo-class" or has a descendant that is matched by ":target".

Syntax: :target-within { /* ... */}

examples

Table of Contents

  1. Jump to the first paragraph!
  2. Jump to the second paragraph!

My Fun Article

You can target this paragraph using a URL fragment. Click on the link above to try out!

This is another paragraph, also accessible from the links above. Isn't that delightful?

code:
                    <div>
                        <h4>Table of Contents</h4>
                        <ol>
                            <li><a class="item-CC" href="#p1">Jump to the first paragraph!</a></li>
                            <li><a class="item-CC" href="#p2">Jump to the second paragraph!</a></li>
                        </ol>
                        <article class="text-CC">
                            <h4>My Fun Article</h4>
                            <p id="p1"> You can target <i>this paragraph</i> using a URL fragment. Click on the link above to try out!</p>
                            <p id="p2"> This is <i>another paragraph</i>, also accessible from the links above. Isn't that delightful?</p>
                        </article>
                    </div>
                    <style>
                        article.text-CC:target-within {background-color: gold;}
                        /* Add a pseudo-element inside the target element */
                        p#p1:target::before {font: 70% sans-serif; content: "►"; color: limegreen; margin-right: 0.25vw;}
                        /* Style italic elements within the target element */
                        p#p2:target i {color: red;}
                    </style>
                

:scope : presents elements that are a reference point for selectors to match against

Syntax : :scope {/* ... */}

examples

This is a paragraph. It is not an interesting paragraph. Sorry about that.

code:
                    <div>
                        <p id="para">This is a paragraph. It is not an interesting paragraph. Sorry about that.</p>
                        <p id="output"></p>
                    </div>
                    <script>
                        const paragraph = document.getElementById("para");
                        const output = document.getElementById("output");
                        if (paragraph.matches(":scope")) {
                            output.textContent = "Yep, the element is its own scope as expected!";
                        }
                    </script>
                

user action pseudo-classes

top

These pseudo-classes require some interaction by the user in order for them to apply, such as holding a mouse pointer over an element.

:hover : matches when a user designates an item with a pointing device, for example holding the mouse pointer over it.

example: see here above

:active : matches when an item is being activated by the user, for example clicked on.

example: see here above

:focus : matches when an element has focus

example: see here above

:focus-visible : matches when an element has focus and the user agent identifies that the element should be visibly focused.

This pseudo-class applies while an element matches the ":focus pseudo-class" and the "UA (User Agent)"" determines via heuristics that the focus should be made evident on the element. Many browsers show a "focus ring" by default in this case.

Syntax : :focus-visible {/* ... */}

examples






code:
                    <div>
                        <input class="item-DD" value="Default styles" /><br />
                        <button class="item-DE">Default styles</button><br />
                        <input class="item-DD focus-only" value=":focus only" /><br />
                        <button class="item-DE focus-only">:focus only</button><br />
                        <input class="item-DD focus-visible-only" value=":focus-visible only" /><br />
                        <button class="item-DE focus-visible-only">:focus-visible only</button>
                    </div>
                    <style>
                        input.item-DD, button.item-DE { margin: 1vw; background-color: transparent;}
                        .focus-only:focus { outline: 0.2vw solid black;}
                        .focus-visible-only:focus-visible {outline: 0.4vw dashed darkorange;}
                    </style>
                

:focus-within : matches an element to which ":focus" applies, plus any element that has a descendant to which ":focus applies".

This pseudo-class matches an element if the element or any of its descendants are focused. In other words, it represents an element that is itself matched by the :focus pseudo-class or has a descendant that is matched by :focus. (This includes descendants in shadow trees.)

Syntax : :focus-within{/* ... */}

examples

Try typing into this form.


code:
                    <div>
                        <p>Try typing into this form.</p>
                        <form class="spec item-DE">
                        <label for="given_name-A">Given Name:</label>
                        <input id="given_name-A" type="text" />
                        <br />
                        <label for="family_name-A">Family Name:</label>
                        <input id="family_name-A" type="text" />
                        </form>
                    </div>
                    <style>
                        form { border: 0.1vw solid; color: gray; padding: 0.4vw;}
                        form:focus-within {background: #ff8; color: black;}
                        input#given_name-A, input#family_name-A {margin: 0.4vw; background-color: transparent;}
                    
                    </style>
                

time-dimensional pseudo-classes

top

These pseudo-classes apply when viewing something which has timing, such as a WebVTT caption track.

:current : represents the element or ancestor of the element that is being displayed.

This pseudo-class selector is a time-dimensional pseudo-class that represents an element or the ancestor of an element that is currently being displayed. For example, this pseudo-class can be used to represent a video that is being displayed with captions by WebVTT.

Syntax : :current{/*...*/}

:past : represents an element that occurs entirely before the ":current" element.

The "":past CSS pseudo-class selector" is a time-dimensional pseudo-class that will match for any element which appears entirely before an element that matches "":current". For example in a video with captions which are being displayed by WebVTT.

Syntax : :past {/* ... */}

:future : represents an element that occurs entirely after the ":current element.<

This selector is a time-dimensional pseudo-class that will match for any element which appears entirely after an element that matches ":current". For example in a video with captions which are being displayed by WebVTT.

Syntax : :future {/* ... */}


resource state pseudo-classes

top

These pseudo-classes apply to media that is capable of being in a state where it would be described as playing, such as a video.

:playing : represents a media element that is capable of playing - such as "audio" or "video" - when that element is playing.

A resource is considered to be playing if the user explicitly initiates playback. It is also considered playing when the element is explicitly in a playing state but temporarily paused due to reasons unrelated to user intent. In such cases, the playback will automatically resume once the underlying reason, such as a "buffering" or "stalled" state, is resolved.

Syntax : :playing{/*...*/}

:paused : represents a media element that is capable of playing - such as "audio" or "video" - when that element is paused.

A resource is paused if the user explicitly paused it, or if it is in a non-activated or other non-playing state, like "loaded, hasn't been activated yet."

Syntax : :paused{/*...*/}


input pseudo-classes

top

These pseudo-classes relate to form elements, and enable selecting elements based on HTML attributes and the state that the field is in before and after interaction.

:autofill : matches when an <input> has been autofilled by the browser.

The class stops matching if the user edits the field.

Syntax : :autofill {/* ... */}

examples

code:
                    <div>
                        <form id="form-AA" method="post" action="">
                            <label for="email">Email</label>
                            <input type="email" name="email" id="email" autocomplete="email" />
                        </form>
                    </div>
                    <style>
                        #form-AA #email {border: 0.3vw solid grey; border-radius: 0.3vw;}
                        #form-AA #email:-webkit-autofill {border: 0.3vw solid blue;}
                        #form-AA #email:autofill {border: 0.3vw solid blue;}
            
                    </style>
                

:enabled : represents a user interface element that is in an enabled state.

example, see above

:disabled :represents a user interface element that is in a disabled state.

example, see above

:read-only : represents any element that cannot be changed by the user.

example, see above

:read-write : represents any element that is user-editable.

example, see above

:placeholder-shown : matches an input element that is displaying placeholder text, for example from the HTML5 "placeholder" attribute.

Syntax : :placeholder-shown{/*...*/}

examples

code:
                    <div>
                        <input placeholder="Type something here!" />
                    </div>
                    <style>
                        input {border: 0.1vw solid black; padding: 0.3vw;}
                        input:placeholder-shown { border-color: teal; color: purple;font-style: italic;}
                    </style>
                

:default : matches one or more UI elements that are the default among a set of elements.

Syntax : :default{/*...*/}

examples

Favorite season
code:
                    <div>
                        <fieldset>
                            <legend>Favorite season</legend>
                            <input type="radio" name="season" id="spring" value="spring" />
                            <label for="spring">Spring</label>
                            <input type="radio" name="season" id="summer" value="summer" checked />
                            <label for="summer">Summer</label>
                            <input type="radio" name="season" id="fall" value="fall" />
                            <label for="fall">Fall</label>
                            <input type="radio" name="season" id="winter" value="winter" />
                            <label for="winter">Winter</label>
                        </fieldset>
                    </div>
                    <style>
                        input:default {box-shadow: 0 0 0.2vw 0.1vw coral;}
                        input:default + label {color: coral;}
            
                    </style>
                

:checked : matches when elements such as checkboxes and radiobuttons are toggled on.

example, see above

:indeterminate : matches when UI elements are in an indeterminate state

This pseudo-class represents any form element whose state is indeterminate, such as checkboxes which have their HTML "indeterminate attribute" set to true, radio buttons which are members of a group in which all radio buttons are unchecked, and indeterminate <progress> elements.

Syntax : :indeterminate{/*...*/}

examples

Checkbox
Radio
code:
                    <div>
                        <fieldset>
                            <legend>Checkbox</legend>
                            <div>
                            <input class="item-ABC" type="checkbox" id="checkbox" />
                            <label for="checkbox">This checkbox label starts out lime.</label>
                            </div>
                        </fieldset>
                        
                        <fieldset>
                            <legend>Radio</legend>
                            <div>
                            <input class="item-ABC" type="radio" id="radio1" name="radioButton" value="val1" />
                            <label for="radio1">First radio label starts out lime.</label>
                            </div>
                            <div>
                            <input  class="item-ABC" type="radio" id="radio2" name="radioButton" value="val2" />
                            <label for="radio2">Second radio label also starts out lime.</label>
                            </div>
                        </fieldset>
                    </div>
                    <style>
                        input:indeterminate + label {background: lime;}
                    </style>
                    <script>
                        const inputs = document.getElementsByClassName("item-ABC");
                        for (let i = 0; i < inputs.length; i++) {
                            inputs[i].indeterminate = true;
                        }
                    </script>
                

:blank : matches a user-input element which is empty, containing an empty string or other null input.

Syntax : :blank{/*...*/}

examples

code:
                    <div>
                        <textarea id="item-BCD"></textarea>
                    </div>
                    <style>
                        #item-BCD:blank {border: 0.3vw solid red;}
                    </style>
                

:valid : matches an element with valid contents.

For example an input element with type 'email' which contains a validly formed email address.

example, see above

:invalid : matches an element with invalid contents.

For example an input element with type 'email' with a name entered.

example, see above

:in-range : applies to elements with range limitations.

For example a slider control, when the selected value is in the allowed range.

example, see above

:out-of-range : applies to elements with range limitations.

For example a slider control, when the selected value is outside the allowed range.

example, see above

:required : matches when a form element is required.

example, see above

:optional : matches when a form element is optional.

example, see above

:user-invalid : : represents an element with incorrect input, but only when the user has interacted with it.

Syntax : :user-invalid{/*...*/}

examples

code:
                    <div>
                        <form>
                            <label class="item-ZZZ" for="email">Email *: </label>
                            <input id="email-AA" name="email" type="email" required />
                            <span></span>
                        </form>
                    </div>
                    <style>
                        .item-ZZZ input#eamil-AA:user-invalid {border: 0.2vw solid blue;}
                        input#email:user-invalid + span::before {content: "✖";color: red;}
                    </style>
                

tree-structural pseudo-classes

top

These pseudo-classes relate to the location of an element within the document tree.

:root : represents an element that is the root of the document.

In HTML this is usually the <html> element.

example, see above

:empty : represents an element with no children other than white-space characters.

example, see above

:nth-child : uses An+B notation to select elements from a list of sibling elements.

example, see above

:nth-last-child : uses An+B notation to select elements from a list of sibling elements, counting backwards from the end of the list..

example, see above

:first-child : matches an element that is the first of its siblings

example, see above

:last-child : matches an element that is the last of its siblings

example, see above

:only-child : matches an element that has no siblings

For example a list item with no other list items in that list.

example, see above

:nth-of-type : uses An+B notation to select elements from a list of sibling elements that match a certain type from a list of sibling elements.

example, see above

:nth-last-of-type : uses An+B notation to select elements from a list of sibling elements that match a certain type, counting backwards from the end of the list.

example, see above

:first-of-type: matches an element that is the first of its siblings, and also matches a certain type selector.

example, see above

:last-of-type : matches an element that is the last of its siblings, and also matches a certain type selector.

example, see above

:only-of-type: matches an element that has no siblings of the chosen type selector.

example, see above


:is() pseudo-class and :where() pseudo-class

top

The :is() CSS pseudo-class function takes a selector list as its argument, and selects any element that can be selected by one of the selectors in that list. This is useful for writing large selectors in a more compact form.

/* Selects any paragraph inside a header, main or footer element that is being hovered */
        :is(header, main, footer) p:hover {
            color: red;
            cursor: pointer;
        }
        
/* The code on the left is equivalent to the following */
            header p:hover,
            main p:hover,
            footer p:hover {
                color: red;
                cursor: pointer;
            }
        

Older browsers support this functionality as ":matches()", or through an older, prefixed pseudo-class — ":any()", including older versions of Chrome, Firefox, and Safari.
":any()" works in exactly the same way as :matches()/:is(), except that it requires vendor prefixes and doesn't support complex selectors.

The ":where()" CSS pseudo-class function takes a selector list as its argument, and selects any element that can be selected by one of the selectors in that list. Anything that ":is()" can do regarding grouping, so can ":where()". This includes being used anywhere in the selector, nesting, and stacking them.

The ":where()" pseudo selector in CSS is functionally identical to the ":is()" pseudo selector in that it takes a comma-separated list of selectors to match against, except that where ":is()" takes the most specific among them as the specificity of that whole part, the specificity of ":where()" is always zero (0).

":is()" and ":where()" accept a forgiving selector list.

In CSS when using a selector list, if any of the selectors are invalid then the whole list is deemed invalid. When using ":is()" or ":where()", instead of the whole list of selectors being deemed invalid if one fails to parse, the incorrect or unsupported selector will be ignored and the others used.

examples

This is my header paragraph

  • This is my first

    list item

  • This is my second

    list item

This is my footer paragraph

code:
                    <div>
                        <header><p>This is my header paragraph</p></header>
                        <main">
                            <ul>
                            <li><p>This is my first</p><p>list item</p></li>
                            <li><p>This is my second</p><p>list item</p></li>
                            </ul>
                        </main>
                        <footer><p>This is my footer paragraph</p></footer>
                    </div>
                    <style>
                          :is(header, main, footer) p:hover {color: red; cursor: pointer;}
                    </style>
                

The :is() pseudo-class can greatly simplify your CSS selectors. It is particularly useful when dealing with HTML5 sections and headings. Since <section>, <article>, <aside>, and <nav> are commonly nested together, without :is(), styling them to match one another can be tricky.

use cases

            /* at the beginning */
            :where(h1,h2,h3,h4,h5,h6) > b {
                color: hotpink;
            }
            
            /* in the middle */
            article :where(header,footer) > p {
                color: gray;
            }
            
            /* at the end */
            .dark-theme :where(button,a) {
                color: rebeccapurple;
            }
        
            /* multiple */
            :is(.dark-theme, .dim-theme) :where(button,a) {
            color: rebeccapurple;
            }
            
            /* stacked */
            :is(h1,h2):where(.hero,.subtitle) {
            text-transform: uppercase;
            }
            
            /* nested */
            .hero:is(h1,h2,:is(.header,.boldest)) {
            font-weight: 900;
        

Each of the above selector examples demonstrates the flexibility of these two functional pseudo-classes. To find areas of your code that could benefit from :is() or :where(), look for selectors with multiple commas and selector repetition.

examples

examples

What it is used for!

CSS The :where() pseudo-class

A serious portal for web developments.


open the door to example-based learning and understanding
code:
                    <div>
                        <h4 style="color:green;">What it is used for!</h4>
                        <h5>CSS The :where() pseudo-class</h5>
                        <div class="mine">
                            <p><em>A serious portal for web developments.</em></p>
                        </br>
                            <section><em>open the door to example-based learning and understanding</em></section>
                        </div>
                    </div>
                    <style>
                        /* CSS selector with :where() psuedo-class */
                        .mine :where(section, p) em {background: green; color: white; margin-left: 2vw; }
                    </style>
                
code:
                    <div class="special" style="margin-left:5vw">
                        <header class="header"><a>Climate</a></header>
                        <main class="main"><a>change</a></header>
                        <aside id="aside"><a>needs</a></aside>
                        <footer class="footer"><a>actions</a></footer>
                    </div>
                    <style>
                        :where(.header, .footer) a {color: red;}            
                    </style>
                

":where()" pseudo-class: syntax

codes
        <style>
             .a > span, .b > span, .c > span {...}
        </style>
        
        equals
        
        <style>
            :where (.a, .b, .c) > span {...}
        </style>  
    

:is() vs. :where()

code:
                <div class="spec-1">
                    <article>
                        <h3>:is()-styled links</h3>
                        <section class="is-styling">
                            <p>Here is my main content. 
                            This <a href="https://mozilla.org">contains a link</a>.
                        </section>
                        <aside class="is-styling">
                            <p>Here is my aside content. 
                            This <a href="https://developer.mozilla.org">
                            also contains a link</a>.
                        </aside>
                        <footer class="is-styling">
                            <p>This is my footer, also containing 
                            <a href="https://github.com/mdn">
                            a link</a>.
                        </footer>
                    </article>
                    <article>
                        <h3>:where()-styled links</h3>
                        <section class="where-styling">
                            <p>Here is my main content. 
                            This <a href="https://mozilla.org">
                            contains a link</a>.
                        </section>
                        <aside class="where-styling">
                            <p>Here is my aside content. This <a 
                            href="https://developer.mozilla.org">
                            also contains a link</a>.
                        </aside>
                        <footer class="where-styling">
                            <p>This is my footer, also containing <a 
                            href="https://github.com/mdn">
                            a link</a>.
                        </footer>
                    </article>
                </div>
                <style>
                    :is(section.is-styling, aside.is-styling, footer.is-styling) a 
                    {color: red;}
                    :where(section.where-styling, aside.where-styling, 
                    footer.where-styling) a{color: orange;}
                    footer a {color: blue;}
                </style>
            

:where() and class

First list no class

  1. List Item 1
  2. List Item 2

Second list with class

  1. List Item 1
  2. List Item 2

Third list with class

  1. List Item 1
  2. List Item 2
code:
                <div>
                    <div>
                        <h3>First list no class</h3>
                        <ol>
                            <li>List Item 1</li>
                            <li>List Item 2</li>
                        </ol>
                    </div>
                    <div>
                        <h3>Second list with class</h3>
                        <ol class="second-list">
                            <li>List Item 1</li>
                            <li>List Item 2</li>
                        </ol>
                    </div>
                    <div>
                        <h3>Third list with class</h3>
                        <ol class="third-list">
                            <li>List Item 1</li>
                            <li>List Item 2</li>
                        </ol>
                    </div>
                </div>
                <style>
                    .second-list {list-style-type: disc;}
                    :where(ol[class]) {list-style-type: none;}
                    .third-list{list-style-type: square;}
                </style>
            


:not() pseudo-class

top

The :not() CSS pseudo-class represents elements that do not match a list of selectors. Since it prevents specific items from being selected, it is known as the "negation pseudo-class".

Syntax: :not(selector) {css declarations;}

example:

/* Selects any element that is NOT a paragraph */
    :not(p) {
      color: blue;
    }
    

The :not() pseudo-class requires a comma-separated list of one or more selectors as its argument. The list must not contain another negation selector or a pseudo-element.

There are several unusual effects and outcomes when using :not() that you should keep in mind when using it:

The :not() pseudo-class may not be nested, which means that :not(:not(...)) is invalid.

Useless selectors can be written using this pseudo-class. For example, :not(*) matches any element which is not an element, which is obviously nonsense, so the accompanying rule will never be applied.

This pseudo-class can increase the specificity of a rule. For example, "#foo:not(#bar)" will match the same element as the simpler "#foo", but has a higher specificity.

:not(.foo) will match anything that isn't .foo, including <html> and <body>.

This selector only applies to one element; you cannot use it to exclude all ancestors. For instance, "body :not(table) a" will still apply to links inside of a table, since <tr> will match with the :not() part of the selector.

Using two selectors at the same time is not yet supported in all browsers. Example: :not(.foo, .bar). For wider support you could use, :not(.foo):not(.bar)

Example:
                <p>I am a paragraph.</p>
                <p class="fancy">I am so very fancy!</p>
                <div>I am NOT a paragraph.</div>
                <h2>
                <span class="foo">foo inside h2</span>
                <span class="bar">bar inside h2</span>
                </h2> 
                <style>
                    .fancy {text-shadow: 2px 2px 3px gold;}
                    /* <p> elements that are not in the class `.fancy` */
                    p:not(.fancy) {color: green;}
                    /* Elements that are not <p> elements */
                    body :not(p) {text-decoration: underline;}
                    /* Elements that are not <div> and not <span> elements */
                    body :not(div):not(span) {font-weight: bold;}
                    /* Elements that are not <div>s or `.fancy` */
                    /* Note that this syntax is not well supported yet. */
                    body :not(div, .fancy) {text-decoration: overline underline;}
                    /* Elements inside an <h2> that aren't a <span> with a class of foo. */
                    /* Complex selectors such as an element with a class are not well supported yet. */
                    h2 :not(span.foo) {color: red;}
                </style>
            
Example
                    <div style="margin-left:3vw;"class="parent">
                        <div class="on">
                            <div class="two"></div>
                            <div class="two"></div>
                        </div>
                        <div class="on"></div>
                    </div>
                    <style>
                        div:not(.special) {margin: 2%; background-color: cadetblue; 
                            padding: 10%; height: 8vw;}
                        div:not(.parent, .special) { box-shadow: inset 0 0 2.4vw tomato; 
                            padding: 2%; }
                        .parent{border: 4px ridge orange;}
                        .on {background-color: lightgreen;border: 4px groove gray;}
                        .two{height: 2vw;}
                    </style>
                
Example
code: 
                <ul class="spec new">
                    <li class="new-li">
                        <span>cell</span>
                        <span>content</span>
                    </li>
                    <li class="new-li">
                        <span>cell</span>
                        <span>content</span>
                    </li>
                    <li class="new-li">
                        <span>cell</span>
                        <span>content</span>
                    </li>
                    <li class="new-li">
                        <span>cell</span>
                        <span>content</span>
                    </li>
                </ul>
                <style>
                .new{list-style: none;padding: 0 0.5vw; font-size: 1.4vw; height: 10vw; width: 40vw;
                     margin-top: 1vw; background-color: skyblue;}
                .new-li {list-style: none;padding: 0 0.5vw; font-size: 1.4vw; height: 2vw; display: 
                    flex; flex-flow: row wrap;align-items: center; justify-content: space-between;}
                .new-li:not(:last-child) {border-bottom: 0.2vw solid red;}
                .new-li span:first-child {color: brown;}
                .new-li span:last-child {color: lightgreen;}
                </style>
        

:focus() pseudo-class

top

The ":focus" CSS pseudo-class represents an element (such as a form input) that has received focus. It is generally triggered when the user clicks or taps on an element or selects it with the keyboard's Tab key.

The ":focus" selector is allowed on elements that accept keyboard events or other user inputs.

examples

code:
                    <div class="special">
                        <div class="special" style="margin-left:5vw;"><input class="red-input" value="I'll be red when focused."></div>
                        <div class="special" style="margin-left:5vw;"><input class="blue-input" value="I'll be blue when focused."></div>
                    </div>
                    <style>
                        .red-input:focus {background: yellow; color: red;}
                        .blue-input:focus {background: yellow; color: blue; }
                    </style>
                

Click inside the text fields to see a green background:

First name:
Last name:
code:
                    <div class="special">
                        <form style="margin-left:5vw;">
                                First name: <input class="name" type="text" name="firstname"><br>
                                Last name: <input class="name" type="text" name="lastname">
                        </form>
                    </div>
                    <style>
                        .name:focus {background-color: lightgreen;}
                    </style>
                

Set the width of the input field to 100 pixels. However, when the input field gets focus, make it 250 pixels wide:

Search:

code:
                    <div class="special">
                        <p class="spec-1" style="margin-left:5vw;">Set the width of the input field to 100 pixels. However, when the input 
                        field gets focus, make it 250 pixels wide:</p>
                        <p class="spec-1" style="margin-left:5vw;"> Search: <input class="one" type="text" name="search"></p>
                    </div>
                    <style>
                        input.one {width: 10vw; -webkit-transition: width .35s ease-in-out; transition: width .35s ease-in-out;}
                        input.one:focus {width: 25vw;}
                    </style>
                

:focus-visible

The ":focus-visible" pseudo-class (also known as the "Focus-Indicated" pseudo-class) is a native CSS way to style elements that are in focus and need a visible indicator to show focus.

The ":focus-visible" pseudo-class applies while an element matches the ":focus" pseudo-class and the UA (User Agent) determines via heuristics that the focus should be made evident on the element. (Many browsers show a “focus ring” by default in this case.). This selector is useful to provide a different focus indicator based on the user's input modality (mouse vs. keyboard).

In the example below, the ":focus-visible" selector uses the UA's behavior to determine when to match. Compare what happens when you click on the different controls with a mouse, versus when you tab through them using a keyboard. Note the difference in behavior from elements styled with ":focus".

examples






code:
                    <div class="special">
                        <input style="margin-left:5vw;" value="default styles"><br>
                        <button>default styles</button><br>
                        <input style="margin-left:5vw;" class="focus-only" value=":focus only"><br>
                        <button class="focus-only">:focus only</button><br>
                        <input style="margin-left:5vw;" class="focus-visible-only" value=":focus-visible only"><br>
                        <button class="focus-visible-only">:focus-visible only</button>
                    </div>
                    <style>
                        input, button {margin-left: 5vw;}
                        .focus-only:focus {outline: 0.2vw solid black;}
                        .focus-visible-only:focus-visible {outline: 0.4vw dashed darkorange;}
                    </style>
                

Click a sentence to get focus

examples

Click Me
code:
 
                    <div class="special">
                        <custom-button style="margin-left:5vw;" tabindex="0" role="button">Click Me</custom-button>
                    </div>
                    <style>
                        custom-button {display: inline-block; margin: 1vw;}
                        custom-button:focus {/* Provide a fallback style for browsers that don't support :focus-visible */ 
                        outline: 0.2vw solid red; background: lightgrey;}
                        custom-button:focus:not(:focus-visible) {/* Remove the focus indicator on mouse-focus for browsers that do support 
                            :focus-visible */background: transparent;}
                        custom-button:focus-visible {/* Draw a very noticeable focus style for keyboard-focus on browsers that do support 
                            :focus-visible */ outline: 0.4vw dashed darkorange; background: transparent;}
                    </style>
                

:focus-within

The ":focus-within" CSS pseudo-class matches an element if the element or any of its descendants are focused. In other words, it represents an element that is itself matched by the ":focus" pseudo-class or has a descendant that is matched by ":focus" (this includes descendants in shadow trees).

This selector is useful, to take a common example, for highlighting an entire <form> container when the user focuses on one of its <input> fields.

examples

Try typing into this form.


code:
                    <div class="special">
                        <p class="spec-1" style="margin-left:5vw;">Try typing into this form.</p>
                        <form class="trial" style="margin-left:5vw;">
                            <label for="given_name">Given Name:</label>
                            <input id="given_name" type="text">
                            <br>
                            <label for="family_name">Family Name:</label>
                            <input id="family_name" type="text">
                        </form>
                    </div>
                    <style>
                        .trial {border: 0.1vw solid; color: gray; padding: 0.4vw;}
                        .trial:focus-within {background: #ff8; color: black;}
                        input {margin: 0.4vw;}
                    </style>
                

code:
                    <div class="special">
                        <form style="margin-left:5vw;" class="demo-form">
                            <input type="text" placeholder="Name" /><br>
                            <input type="text" placeholder="Email" />
                        </form>
                    </div>
                    <style>
                        .demo-form {background-color: #bdc3c7; padding: 2vw;}
                        .demo-form:focus-within {background-color: #f1c40f;}
                        .demo-form input[type="text"]:focus {border: 0.1vw solid #d35400;}
                    </style>
                

:nth-of microsyntax : pre-filter a set of child elements

top

The :nth-child() and :nth-last-child() pseudo-class selectors

With the :nth-child() pseudo-class selector it is possible to select elements in the DOM by their index. Using the An+B microsyntax you get fine control over which elements you want to select.

:nth-child(2) : select the 2nd child.

:nth-child(2n) : select all even children (2nd, 4th, 6th, 8th, and so on).

:nth-child(2n+1) : select all odd children (1st, 3rd, 5th, 7th, and so on).

:nth-child(5n+1) : select the 1st (=(5x0)+1), 6th (=(5x1)+1), 11th (=(5x2)+1), … child.

:nth-child(5n+2) : Select the 2nd (=(5x0)+2), 7th (=(5x1)+2), 12th (=(5x2)+2), … child.

:nth-child(2) : select the 2nd child.

But, there are more creative selections you can do, if you omit the A parameter. For example:

:nth-child(n+3) : select every child from the 3rd one up (3rd, 4th, 5th, and so on).

:nth-child(-n+5) : select every child up to the 5th one (1st, 2nd, 3rd, 4th, 5th).

Combine a few of these :nth-child() selections and you can select ranges of elements:

:nth-child(n+3):nth-child(-n+5): select every child from the 3rd one up to the 5th one (3rd, 4th, 5th).

Using :nth-last-child() you can do similar selections, but instead of starting to count from the start, you start counting from the end.

Pre-filtering selecCSS Selectors Level 4 is the ability to optionally pass a selector list into :nth-child() and :nth-last-child().

        :nth-child(An+B [of S]?)
        :nth-last-child(An+B [of S]?)
    

The An+B notation defines an integer step (A) and offset (B), and represents the An+Bth elements in a list, for every positive integer or zero value of n, with the first element in the list having index 1 (not 0).

For values of A and B greater than 0, this effectively divides the list into groups of A elements (the last group taking the remainder), and selecting the Bth element of each group.

The An+B notation also accepts the "even" and "odd" keywords, which have the same meaning as 2n and 2n+1, respectively.

When of S is specified, the An+B logic is only applied onto elements that match the given selector list S. This essentially means that you can prefilter children before An+B does its thing.


:has() pseudo-class : a powerful new CSS capability

top

The ":has()" selector in CSS allows to style a parent element if it contains a specific child element. It allows developers to apply styles to parent elements based on specific conditions met by their descendants. In other words, it can style an element if any of the relative selectors that are passed as an argument match at least one element when anchored against this element.

With :has(), you can apply styles by checking to see if a parent element contains the presence of specific children, or if those children are in a specific state. This means, we essentially now have a parent selector, which takes on the "specificity" of the most specific selector in its arguments (the same way as :is() and :not() do).

Synthax: :has(<relative-selector-list>) {/* ... */ }

If the :has() pseudo-class itself is not supported in a browser, the entire selector block will fail unless :has() is in a forgiving selector list, such as in :is() and :where().

The :has() pseudo-class cannot be nested within another :has(). This is because many pseudo-elements exist conditionally based on the styling of their ancestors and allowing these to be queried by :has() can introduce cyclic querying.

Pseudo-elements are also not valid selectors within :has() and pseudo-elements are not valid anchors for :has().

Logical operations

The :has() relational selector can be used to check if one of the multiple features is true or if all the features are true.
By using comma-separated values inside the :has() relational selector, you are checking to see if any of the parameters exist. x:has(a, b) will style x if descendant a OR b exists.
By chaining together multiple :has() relational selectors, you are checking to see if all of the parameters exist. x:has(a):has(b) will style x if descendant a AND b exist.

Syntax examples

        /* Style div only if it contains an image */
        div:has(img) {border: 2px solid blue;}

        /* Style a table row if it contains a cell with a specific class */
        tr:has(td.highlight) {background-color: yellow;}

        .bento-card:has(button:focus-visible) { outline: 2px solid var(--color-primary);}

        /* Select section elements that have an h1 element inside */
        section:has(h1) {background-color: gold; }

        /* Select section elements that have an element with class 'funfact' inside */
        section:has(.funfact) {color: blue;}

        /* Select li elements that have checked input element inside */
        ul li:has(input:checked) { border: 2px solid maroon;}
    

Examples:

Morning Times

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Morning Times

Delivering you news every morning

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

code:
                    <div>
                        <section>
                            <article>
                            <h4>Morning Times</h4>
                            <p>
                                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                                tempor incididunt ut labore et dolore magna aliqua.
                            </p>
                            </article>
                            <article>
                            <h4>Morning Times</h4>
                            <h5>Delivering you news every morning</h5>
                            <p>
                                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                                tempor incididunt ut labore et dolore magna aliqua.
                            </p>
                            </article>
                        </section>
                    </div>
                    <style>
                        h4, h5 {margin: 0 0 1vw 0;}
                        h4:has(+ h5) {margin: 0 0 0.25vw 0;}
                    </style>
                

Morning Times

Delivering you news every morning

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Morning Times

Delivering you news every morning
8:00 am

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

code:
                    <div>
                        <section>
                            <article>
                            <h4>Morning Times</h4>
                            <h5>Delivering you news every morning</h5>
                            <p>
                                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                                tempor incididunt ut labore et dolore magna aliqua.
                            </p>
                            </article>
                            <article>
                            <h4>Morning Times</h4>
                            <h5>Delivering you news every morning</h5>
                            <h6>8:00 am</h6>
                            <p>
                                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                                tempor incididunt ut labore et dolore magna aliqua.
                            </p>
                            </article>
                        </section>
                    </div>
                    <style>
                        h4, h5, h6 {margin: 0 0 1vw 0;}
                        :is(h4, h5, h6):has(+ :is(h4, h5, h6)) { margin: 0 0 0.25vw 0;}
                    </style>
                

Lorem Ipsum is that it has a more-or-less normal distribution of letters,as opposed to using 'Content here, content here', making it look like readable English.

code:
            <div class="frame-1">
                <p>
                    Lorem Ipsum is that it has a more-or-less normal distribution of
                    letters,<a href="/">as opposed to using 'Content here, content 
                    here', making it look like readable English.</a> 
                </p>
            </div>
            <style>
                .frame-1 > p:has(a) {font-size: 20px;}
            </style>
        

            <ul>
                <li> <label> <input type="radio" name="source" />Google</label> </li>
                <li><label> <input type="radio" name="source" />Word of Mouth</label></li>
                <li><label> <input type="radio" name="source" />Other</label></li>
            </ul>
            <style>
                ul li input:checked {accent-color: #f806e4;}
                ul li:has(input:checked) { border: 2px solid #f806e4;}
            </style>
        

the basics

holiday
holiday-2
holiday souvenirs
code:
            <div class="spec-1">
                <figure class="one">
                    <img src="../../images/1.jpg" alt="holiday">
                </figure>
                <figure class="one">
                    <img src="../../images/2.jpg" alt="holiday-2">
                    <figcaption>holiday souvenirs</figcaption>
                </figure>
                
            </div>
            <style>
                figure.one img{width: 30%;}
                figure.one:has(figcaption){background-color: skyblue; padding: 0.5vw; width: 30%;}
            </style>
        
river activity
highway
highway in the city of Shanghai.
Using :has() can be easy.
                        
                            figure:has(pre) {
                                background: rgb(252, 232, 255);
                                border: 3px solid white;
                                padding: 1rem;
                            }
                        
                    
code:
                <div>
                    <article">
                        <figure class="two">
                            <img src="../../images/3a.jpg" alt="river activity">
                        </figure>
                        <figure class="two">
                            <img src="../../images/19.jpg" alt="highway">
                            <figcaption>highway in the city of Shanghai.</figcaption>
                        </figure>
                        <figure class="two">
                            <figcaption>Using <code>:has()</code> can be easy.</figcaption>
                            <pre class="pre">
                                <code>
                                    figure:has(pre) {
                                        background: rgb(252, 232, 255);
                                        border: 3px solid white;
                                        padding: 1rem;
                                    }
                                </code>
                            </pre>
                        </figure>
                    </article>
                </div>
                <style>
                    .two{ margin: 0; box-shadow: 1px 1px 4px rgba(1, 1, 1, 0.4); width: 40vw;}
                    .two:has(figcaption) {background: white; padding: 0.5vw;}
                    .two:has(.pre) { background: rgb(252, 232, 255); border: .3vw solid burlywood; padding: 1vw;}
                    .two:not(:has(:not(img))) {display: flex;}
                    .two:has(img) figcaption {font-size: 90%; font-style: italic; margin: 0.6vw 0 0.1vw;}
                    .two figcaption {font-family: georgia; color: blue;}
                    .two:has(.pre) figcaption {margin: 0.6vw 0 0.1vw;}
                    .two img {max-width: 40%; display: block; object-fit: cover;}
                    .two.pre {font-family: Courier New, monospace; margin: 0; color: rgb(159, 43, 148);}
                    .een {display: grid; grid-template-columns: repeat(1fr, minmax(10vw, 1fr));gap: 1vw;font-family: georgia;}
                </style>
            

using :has() with the child combinator

This image is wrapped in a figure element.
code:
                <main class="twee">
                    <a href="#">
                    <figure>
                    <img src='../images/9.jpg' alt=''>
                        <figcaption>This image is wrapped in a figure element.</figcaption>
                    </figure>
                    </a>
                    <a href="#">
                    <img src='../images/8.jpg' alt=''>
                    </a>
                </main>
                <style>
                    .twee a:has(img) {border: 0.5vw solid darkolivegreen; background: palegoldenrod; 
                        color: saddlebrown;}
                    .twee a:has(> img) {border: 0.5vw solid chocolate; background: pink;}
                    .twee a {display: block; text-decoration: none;}
                    .twee img { max-width: 90%; display: block;}
                    .twee p {margin: 0.5vw 0 0;}
                    .twee {margin: 1.5vw 1vw; display: grid; grid-template-columns: 15vw 15vw; gap: 1vw; 
                        font-family: Avenir, Helvetica; font-size: 0.8vw; align-items: start;}
                    .twee figure {margin: 0;}
                    .twee figcaption {padding: 0.5vw;}
                </style>
            

using :has() with the sibling combinator

Morning Times

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Morning Times

Delivering you news every morning

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

code:
            <section>
                <article class="art1">
                  <h4 class="title">Morning Times</h4>
                  <p class="para1">
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                    tempor incididunt ut labore et dolore magna aliqua.
                  </p>
                </article>
                <article class="art1">
                  <h4 class="title">Morning Times</h4>
                  <h5>Delivering you news every morning</h5>
                  <p class="para1">
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                    tempor incididunt ut labore et dolore magna aliqua.
                  </p>
                </article>
            </section>
            <style>
                .art1 {color: black; font-size: 1vw; background-color: skyblue;}
                .title{font-size: 1.5vw; }
                .para1{font-size: 1w; text-decoration-style: none;}
                h4, h5 {margin: 0 0 1rem 0;}
                h4:has(+ h4) {margin: 0 0 0.25rem 0;}
            </style>
        

styling without JS, using pseudo-classes

top

styling forms

code: 
                <article class="drie">
                    <form>
                        <div>
                            <label for="name">Name</label> 
                            <input type="text" id="name">
                        </div>
                        <div>
                            <label for="site">Website</label> 
                            <input type="url" id="site">
                        </div>
                        <div>
                            <label for="email">Email</label>
                            <input type="email" id="email">
                        </div>
                    </form>
                    <footer>
                        <input type="checkbox" id="check"> <label for="check">Dark mode</label>
                    </footer>
                </article>
                <style>
                    @layer basic, focus, darkmode, invalid;
                    /* Styling when a field has focus */
                    @layer focus {
                        form:has(:focus-visible) {background: antiquewhite; border: 0.4vw solid antiquewhite;}
                        /* -- Styling other fields when one field has focus -- */
                        form:has(:focus-visible) div:has(input:not(:focus-visible)) label {color: peru;}
                        form:has(:focus-visible) div:has(input:not(:focus-visible)) input {border: .2vw solid peru; }
                    } /* End "focus" layer */
                    /* Styling field if input is invalid */
                    @layer invalid {
                        input:invalid {outline: .4vw solid red; border: .2vw solid red;}
                        div:has(input:invalid) label {color: crimson;} 
                        label:has(+ input:invalid)::before {content: '✖ ';color: crimson;}
                    } /* End "invalid" layer */
                    /* Dark Mode when checkbox checked */
                    @layer darkmode {
                        body:has(input[type="checkbox"]:checked) {background: blue; --primary-color: white;}
                        body:has(input[type="checkbox"]:checked) form {border: .4vw solid white; }
                        body:has(input[type="checkbox"]:checked) form:has(:focus-visible) {background: navy;}
                        body:has(input[type="checkbox"]:checked) input:focus-visible {outline: .3vw solid 
                            lightsalmon; border: .2vw solid lightsalmon; /* needed for Chrome */}
                    } /* End "darkmode" layer */
                    /* Basic styling */
                    @layer basic {
                    /* ---- Layout ---- */
                    .drie {display: grid; grid-template-columns: 10vw auto ; grid-template-rows: min-content 
                        max-content 1fr; grid-column-gap: calc(2vw + 0.5rem);}
                    .drie form {grid-column: 2 / 3; height: 20vw;}
                    .drie footer {width: 100%; }
                    /* -- Basic form styling -- */
                    .drie form { border: .4vw solid white; padding: 2vw 3vw; width: min-content;}
                    .drie form label, form input {display: block;}
                    .drie label, .drie input {font-size: 1.2vw; font-family: Avenir, Helvetica, sans-serif;}
                    .drie div {margin: 1.25vw 0;}
                    .drie input {margin: 0.25vw 0; padding: 0.5vw; appearance: none; border: .2vw solid 
                        var(--primary-color);}
                    .drie :focus-visible {outline: .4vw solid sienna; border: .2v solid sienna; 
                        /* needed for Chrome */}
                    .drie input[type="checkbox"] {float: left; margin-right: 0.66vw; }
                    .drie footer:has(input[type="checkbox"]) label {display: inline-block; margin: 
                        .1vw 0 0;}
                    /* -- Custom checkbox styling -- */
                    @supports(appearance: none) {
                        input[type="checkbox"] { appearance: none; width: 1.6vw; height: 1.6vw; border: 
                            .2vw solid var(--primary-color);           background: white; margin-top: 0; }
                        input[type="checkbox"]:checked {position: relative; background: none;}
                        input[type="checkbox"]:checked::after {position: absolute; top: 0.35vw; left: 0.12vw; 
                            content: ""; width: 0.9vw;   height: 0.3vw; border: .4vw solid var(--primary-color); 
                            border-right: none; border-top: none; transform: rotate(-45deg);}}
                    } /* End "basic" layer */
                </style>
            

dark mode toggle with no JS

Choose a theme

code:
                <main class="vier">
                    <h3>Choose a theme</h3>
                    <menu>
                        <label for="color-mode">Options:</label>
                        <select name="color-mode" id="color-mode">
                            <option value="pony">Pony</option>
                            <option value="wine">Wine</option>
                            <option value="molly">Molly</option>
                            <option value="zeldman">Zeldman</option>
                        </select>
                    </menu>
                </main>
                <style>
                .vier {display: block; width: 30vw; height: 20vw; color: var(--text-color); 
                    background-color: var(--main-background);padding: 1vw;
                    font-family: var(--font-family);}
                h4 {margin: 0; font-weight: 400; font-size: 2vw;}
                .vier:has(option[value="pony"]:checked) {--font-family: cursive; --text-color: #b10267;
                     --body-background: #ee458e; --main-background: #f4b6d2;}
                .vier:has(option[value="wine"]:checked) {--font-family: avenir, helvetica; --text-color: white; 
                    --body-background: black; --main-background: brown;}
                .vier:has(option[value="molly"]:checked) {--font-family: helvetica; --text-color: skyblue; 
                    --body-background: #6c3; --main-background: #09c; }
                .vier:has(option[value="zeldman"]:checked) {--font-family: georgia; --text-color: black;
                     --body-background: #c30; --main-background: #f60; }
                </style>
            


:scope pseudo class

top

The "scope" selector is used to define the scope for styles in CSS.

If ":scope" is at the root level of a css, it is equivalent to ":root"

examples

Using ":scope" within @scope blocks allows for clear and targeted styling based on the context defined by the class names.

examples

Lorem Ipsumdev.to,

code:
            <div>
                <div class="light">
                    <p><a href="#">dev.to</a>, </p>
                </div>
                <div class="dark">
                    <p>Lorem Ipsum<a href="#">dev.to</a>,</p>
                </div>
            </div>
            <style> 
                @scope (.light) {
                    :scope {background-color: black;}
                    a {font-size : 2rem;}
                }
                @scope (.dark) {
                    :scope {background-color: yellow; color: purple;}
                    a {font-size : 4rem;}
                }
            </style>
        

:hover pseudo-class

top

matches when a user designates an item with a pointing device, for example holding the mouse pointer over it.

mouse over me



holiday
code:
                <div>
                    <div id="d">mouse over me</div>
                    <br>
                    <button class="buttonval">Button</button>
                    <br><br>
                    <img id="img1" src="../images/2.jpg" alt="holiday" width="20%" height="auto"/>
                </div>
                <style>
                    #d{background-color:blue; color: lightgrey; padding: 1vw;text-align: center;
                        margin-left: 5vw; width: 10vw; height: 4vw;}
                    #d:hover{background-color:orange; transform: translate(-2vw, -3vw); transition: all 1s;}
                    .buttonval {margin-left: 5vw; background-color: aqua;  color: black; padding:1vw 2vw;}
                    .buttonval:hover {background-color:tomato; transform: scale(2); transition: all 2s;}
                    #img1{margin-left: 5vw;}
                    #img1:hover {margin-left: 10vw; border: 1vw solid blue; transition: all 1s;}
                </style>
            

tooltip hover example

hover over me to see the p element

Here I am!


Hover over me.
code:
                <div>
                    <div>
                        <div id="e">hover over me to see the p element
                            <p id="f">Here I am!</p>
                    </div>
                    <br>
                    <div>
                        <a href="#" data-tip="this is a tooltip"> Hover over me. </a>
                    </div>
                </div>
                <style>
                    #e{margin-left: 5vw; color: black;}
                    #f{display: none; background-color: black; width: 7vw; padding: 0.2vw;}
                    #e:hover #f{display: block; background-color:white;margin-left: 2vw;}
                    a[data-tip] {display: block; position: relative; margin-left: 5vw; padding-top: 2vw; }
                    a[data-tip]:hover::after, a[data-tip]:focus::after {content: attr(data-tip); 
                        position: relative; left: 1vw; top: 0.4vw; min-width: 20vw; border-radius: 5%; 
                        background-color: rgba(0, 0, 0, 0.5); padding: 1vw; color: white; font-size: 1vw;z-index: 1;}

                </style>
            


select any child

top

* - universal selector

1
b
2
a
3
code:
                <div class="my-div">
                    <span>1</span>
                    <div>b</div>
                    <span>2</span>
                    <div>a</div>
                    <span>3</span>
                </div>
                <style>
                    .my-div > * {border:0.5vw solid green; width: 40vw;}
                </style>
            

CSS combinators - child selector

This paragraph is under the div.set element

This paragraph2 is under the div.set element.

This paragraph is not under the div.set element. As it is not the child element of div

Even This paragraph is under the div.set element.

This paragraph is not under the div.set element.

This paragraph is not under the div.set element.

code:
                <div>
                    <div class="set">
                        <p>This paragraph is under the div.set element</p>
                        <p>This paragraph2 is under the div.set element. </p>
                        <section><p>This paragraph is not under the div.set 
                        element. As it is not the child element of div</p></section>
                        <p> Even This paragraph is under the div.set element. </p>
                    </div>
                    <p>This paragraph is not under the div.set element. </p>
                    <p>This paragraph is not under the div.set element. </p>
                </div>
                <style>
                .set{width: 40vw;}
                    div .set > p {background-color: salmon; font-size : 1.2vw; padding: .3vw;}    
                </style>     
            

CSS combinators - descendant selector

Paragraph 1
Paragraph 2

Paragraph 3

Paragraph 4
Paragraph 5
code:
                <div>
                    <div class="exmple">
                        <span class="one">Paragraph 1</span><br>
                        <span class="one">Paragraph 2</span>
                        <p>
                            <span class="one">Paragraph 3</span>
                        </p>
                    </div>
                    <span class="one">Paragraph 4</span><br>
                    <span class="one">Paragraph 5</span>
                </div>
                <style>
                    span .one {display: block;} 
                    div.example, div.example > * {background-color: #9ba2a3; width: 40vw;}
                </style>
            

CSS combinators - child selector

Exam Details

Exam begins at 11AM.

Do not bring cell phone.
Candidates should reach the center till 10:45.

The candidates need to be bring the Admit Card.
code:
                <article>
                    <div class="two">Exam Details</div>
                    <div class="two"><p>Exam begins at 11AM.</p></div>
                    <span><div class="two"> Do not bring cell phone.</div>
                    </span>
                    <div class="two">
                        <div>Candidates should reach the center till 10:45.
                        </div>
                        <br/>
                        The candidates need to be bring the Admit Card.
                    </div>
                </article>
                <style>
                article > div.two { border: dashed midnightblue; width: 85%; 
                    & p {font-size: 1.5vw; font-style: italic; box-shadow: inset 0 0 .8vw orange;}
                  }
                </style>
            

:first-child pseudo-class

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a urna elit. Integer malesuada tempus enim nec rhoncus. Aenean tempus adipiscing porttitor.

Quisque aliquam nunc vel arcu varius aliquam. Vestibulum euismod nulla id nulla suscipit sollicitudin.


First Child so CSS styling applicable here.

Second Child so CSS styling NOT applicable here.


style applied here - 1st child in div - - No style here - 2nd child in div

  1. bread - 1st child
  2. butter - 2nd child
  3. Egg - 3rd child
code:
                <div>
                    <div id="g">
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a 
                        urna elit. Integer malesuada tempus enim nec rhoncus. Aenean tempus 
                        adipiscing porttitor.</p>
                        <p>Quisque aliquam nunc vel arcu varius aliquam. Vestibulum euismod 
                        nulla id nulla suscipit
                        sollicitudin.</p>
                    </div>
                    <br>
                    <div id="h">
                        <p >First Child so CSS styling applicable here.</p>
                        <p >Second Child so CSS styling NOT applicable here.</p>
                    </div>   
                    <br>
                    <div id="i">
                        <em>style applied here - 1st child in div - - </em>
                        <em>No style here - 2nd child in div</em><br>
                    </div> 
                    <br>
                    <ol id="j">
                        <li>bread - 1st child </li>
                        <li>butter - 2nd child </li>
                        <li>Egg - 3rd child </li>
                    </ol> 
                </div>
                <style>
                    #i, #j{margin-left: 1vw; } 
                    #g p:first-child{color:red;}
                    #h p:first-child {padding:0.5vw; border: 2px solid red; display: block; width: 40vw;}
                    #i em:first-child {text-align:center; padding:0.5vw; border: 
                        2px solid red;}
                    #j li:first-child{text-decoration: red wavy underline;}
                </style>
            

:last-child pseudo-class

This text isn't selected.

This text is selected!


This text isn't selected.

This text isn't selected: it's not a `p`.


main menu - layout

code:
                <div>
                    <div id="last">
                        <p>This text isn't selected.</p>
                        <p>This text is selected!</p>
                    </div>
                    <br>
                    <div id="last-2">
                        <p>This text isn't selected.</p>
                        <h3>This text isn't selected: it's not a `p`.</h3>
                    </div>
                    <br>
                    <p style="font: 1.25vw sans-serif; color:black;">main menu - layout</p>
                    <ul class="main-menu">
                        <li>Home</li>
                        <li>About</li>
                        <li>Contact</li>
                        <li>Members</li>
                    </ul>
                </div>
                <style>
                    #last .afstand{margin-left: -5vw;}
                    #last p:last-child {color: lime;background-color: black; padding: 5px; width: 40vw;}
                    #last-2 h3{margin-left: 5vw;}  
            
                    li {list-style: none; display: inline; padding: 20px;font-family: arial;}
                    .main-menu {margin-left: 5vw;background: #2d7484;color: #fff;padding:  20px;max-width: 40vw;}
                    .main-menu :not(:last-child) {border: solid 1px #ddd;color: #ea9e12;}
                </style>
            


:target pseudo-class

top

matches the element which is the target of the document URL.

topics to be explored
  1. CSS

  2. HTML

  3. JAVA

  4. PHP

  5. C++


new content

jump to new content 1

jump to new content 2

jump to new content 3

jump to new content 4

Click on the links above and the :target selector highlight the current active HTML anchor.

new content 1...

new content 2...

new content 3...

new content 4...

code:
                <div>
                    <div id="pick">topics to be explored
                        <ul>
                            <li><a href="#topic1">topic 1</a></li>
                            <li><a href="#topic2">topic 2</a></li>
                            <li><a href="#topic3">topic 3</a></li>
                            <li><a href="#topic4">topic 4</a></li>
                            <li><a href="#topic5">topic 5</a></li>
                        </ul>
                        <ol>
                            <li id="topic1"><h3>CSS</h3></li>
                            <li id="topic2"><h3>HTML</h3></li>
                            <li id="topic3"><h3>JAVA</h3></li>
                            <li id="topic4"><h3>PHP</h3></li>
                            <li id="topic5"><h3>C++</h3></li>
                        </ol>
                    </div>
                    <br>
                    <div id="new">new content
                        <p><a href="#news1">jump to new content 1</a></p>
                        <p><a href="#news2">jump to new content 2</a></p>
                        <p><a href="#news3">jump to new content 3</a></p>
                        <p><a href="#news4">jump to new content 4</a></p>
                        <p>Click on the links above and the :target selector highlight
                        the current active HTML anchor.</p>
                        <p id="news1"><b>new content 1...</b></p>
                        <p id="news2"><b>new content 2...</b></p>
                        <p id="news3"><b>new content 3...</b></p>
                        <p id="news4"><b>new content 4...</b></p>
                    </div> 
                </div>
                <style>
                    #pick, #new{margin-left: 5vw;}
                    ul li a{display: block; padding: 0.5vw; text-decoration: none; 
                        color: blue;}
                    #pick :target {background-color: Gold; width: 20vw; transform: 
                        scale(1.5) translate(4vw); 
                        transition: all 1s;}
                    #new :target {border: 2px solid #D4D4D4;background-color: #e5eecc;
                        width: 30vw;transform: scale(1.05) translate(4vw); transition: 
                        all 1s;}
                </style> 
            


link-related pseudo-classes

top

:link - matches links that have not yet been visited.

move mouse over the links to see the change in layout.

This link changes color

This link changes font-size

This link changes background-color

This link changes font-family

This link changes text-decoration

code:
                <div>
                    <p>move mouse over the links to see the change in layout.</p>
                    <p><a class="ex1" href="#">This link changes color</a></p>
                    <p><a class="ex2" href="#">This link changes font-size</a></p>
                    <p><a class="ex3" href="#">This link changes background-color</a></p>
                    <p><a class="ex4" href="#">This link changes font-family</a></p>
                    <p><a class="ex5" href="#">This link changes text-decoration</a></p>
                    <div class="pre">
                    <p class="pre" style="color: darkblue">Codes:

</div> <style> a.ex1:hover, a.ex1:active {color: red;} a.ex2:hover, a.ex2:active {font-size: 150%;} a.ex3:hover, a.ex3:active {background: red;} a.ex4:hover, a.ex4:active {font-family: monospace;} a.ex5:visited, a.ex5:link {text-decoration: none;} a.ex5:hover, a.ex5:active {text-decoration: underline;} </style>

:active - matches when an item is being activated by the user.


Click This Link

go to my website

go to wikipedia.org

Note: The :active selector styles the active link.

code:
                <div>
                    <button id="two"><a href="#">Button</a></button><br>
                    <p><a id="two_one"href = "">Click This Link</a></p>
                    <p><a id="two_two" target="_blank" href="https://www.lwitters.com">
                    go to my website</a></p>
                    <p><a id="two_three" target="_blank" href="http://www.wikipedia.org">
                    go to wikipedia.org</a></p>
                    <p><b>Note:</b> The :active selector styles the active link.</p>
                </div>
                <style>
                    button{margin-left: 5vw;}
                    #two {background-color: aqua; padding:1vw 2vw;}
                    #two:active, #two a:active {background-color:yellow; color: red;}
                    #two:hover:after{content:" - - - turns yellow and red when active - - - ";}
                    #two_one:active {color: #FF00CC}
                    #two_two:active, #two_three:active {background-color: yellow;}
                </style>
            

:visited - matches links that have been visited by the user.

This link goes to my website. After it's been visited, it will turn orange.

This link goes to Wikipedia. After it's been visited, its border will turn black.

This link goes to Google. After it's been visited, it will turn white and the background will turn orange.

code:
                <div>
                    <div class="visit">
                        <a href="#test-visited-link">Have you visited this link yet?</a><br>
                        <a href="">You've already visited this link.</a>
                    </div> 
                    <section>
                        <p><a href="https://lwitters.com" clas="lwitters">This link goes to my website. 
                        After it's been visited, it will turn orange.</a></p>
                        <p><a href="https://www.wikipedia.com" class="wikipedia">This link goes to Wikipedia. 
                        After it's been visited, its border will turn black.</a></p>
                        <p><a href="https://www.google.com" class="google">This link goes to Google. 
                        After it's been visited, it will turn white and the background will turn orange. </a></p>
                    </section>
                </div>
                <style>
                    .visit a {background-color: white;border: 1px solid white; margin-left: 5vw;}
                    .visit a:visited {background-color: yellow; border-color: hotpink; color: hotpink;}
                    a.lwitters {color: green;}
                    a.lwitters:visited {color: #E18728;}
                    a.wikipedia{border: 2px solid blue;}
                    a.wikipedia:visited {border: 2px solid black;}
                    a.google {background-color: yellow;color: black;}
                    a.google:visited {background-color: #E18728;color: white;}
                    section {max-width: 60vw; font-size: 1vw; font-family: Helvetica, sans-serif;}
                </style>
            

:any-link - matches an element if the element would match either :link or :visited.

Medium
Lorem ipsum dolor sit amet
code:
                <div id="update" style="margin-left:4vw;">
                    <a href="https://ww.lwitters.com">Medium</a><br>
                    <a>Lorem ipsum dolor sit amet</a>
                </div>
                <style>
                    #update a:any-link {color: red;}
                </style>
            


focus-related pseudo-classes

top

:focus - is triggered when the user clicks or taps on an element or selects it with the keyboard's Tab key.




code:
                <div>
                    <div class="search">
                        <p>Set the width of the input field to 100 pixels. However, when the input field 
                        gets focus, make it 250 pixels wide:</p>
                        <p>Search: <input type="text" name="search"></p>
                    </div>   
                    <br> 
                    <div>
                        <input class="red-input" value="I'll be red when focused."><br>
                        <input class="blue-input" value="I'll be blue when focused.">
                    </div>
                    <br>
                    <form class="fc" action="" method="post">
                        <div class="container">
                            <label>Username</label>
                            <input type="text" name="uname" required>
                
                            <label>Password</label>
                            <input type="password" name="pass" required>
                
                            <button type="submit">Sign in</button>
                        </div>
                    </form>
                </div>
                <style>
                    .search input[type=text] {width: 100px;-webkit-transition: 
                        width .35s ease-in-out; transition: width .35s ease-in-out;}
                    .search input[type=text]:focus {width: 30vw; background-color: burlywood;}

                    .red-input, .blue-input{margin-left: 1vw;}
                    .red-input:focus {background: yellow; color: red;}
                    .blue-input:focus {background: yellow;color: blue;}

                    .fc{margin-left: 1vw; box-shadow: 0 1px 3px rgba(50, 50, 93, .15), 0 1px 0 
                        rgba(0, 0, 0, .02); border: 1px solid #d7d7d7; background-color: #FFF; 
                        width: 30vw;}
                    .fc input[type=text], input[type=password] {width: 100%; padding: 1.2vw 2vw; 
                        margin: 1vw 0; display: inline-block; border: 1px solid #ccc; box-sizing:
                        border-box; box-shadow: 0 1px 3px rgba(50, 50, 93, .15), 0 1px 0 
                        rgba(0, 0, 0, .02);}
                    .fc input:focus {background-color: aqua;}
                    .fc button {background-color: #4CAF50; color: #FFF; margin: 8px 0; 
                        border: none; cursor: pointer;
                        box-shadow: 0 2px 5px 0 rgba(0, 0, 0, .26); min-width: 12vw; 
                        border-radius: 2px; padding: 1vw
                        1.2vw;}
                    .container {padding: 1.2vw;}
                </style>
            

:focus-within - matches an element if the element or any of its descendants are focused.

Try typing into this form.



A paragraph

My Website

code:
                <div>
                    <p>Try typing into this form.</p>
                    <form class="fcw">
                        <label for="given_name">Given Name:</label>
                        <input id="given_name" type="text">
                        <br>
                        <label for="family_name">Family Name:</label>
                        <input id="family_name" type="text">
                    </form>
                    <br>
                    <div class="my-element">
                        <p>A paragraph</p>
                        <p><a target="_blank" href="https://lwitters.com">My Website
                        </a></p>
                        <label for="lw_email">Your email:</label>
                        <input type="email" id="lw_email" />
                    </div>
                </div>
                <style>
                    .fcw {border: 1px solid; color: darkgray; padding: 1vw; margin-left: 1vw; width: 30vw;}
                    .fcw input{margin: 0.4vw;}
                    .fcw:focus-within{background:orange; color: black;}
                    .my-element *:focus {background: yellow !important; color: #000;}
                    label{padding-left: 5vw;}
                    .my-element{width: 40vw;}
                    .my-element:focus-within {outline: 3px solid #333; width: 40vw; margin-left: 5vw;}
                </style>
            

:focus-visible - matches when an element has focus and the user agent identifies that the element should be visibly focused<. (It is triggered by selecting it with the keyboard's Tab key.)

code:
                <div id="update1" style="margin-left:4vw;">
                    <button>Save</button>
                    <button>Submit</button>
                </div>
                <style>
                button:focus-visible {background-color: green; color: skyblue;}
                </style>
            


:autofill

top

matches when an <input> has been autofilled by the browser. The class stops matching if the user edits the field.

code:
                <form>
                    <label for="email">Email</label>
                    <input type="email" name="email" id="email" />
                </form>
                <style>
                    input:-webkit-autofill {  box-shadow: 0 0 0px 10vw seagreen inset;
                        -webkit-text-fill-color: white;}
                </style>
            

:lang

top

matches elements based on the language they are determined to be in.

This paragraph is English, but the language is not specifically defined.

This paragraph is defined as British English.

Ce paragraphe est défini en français.

I live in Italy.

Ciao bella!

code:
                <div>
                    <div>    
                        <p>This paragraph is English, but the language is not specifically defined.</p>
                        <p lang="en-GB">This paragraph is defined as British English.</p>
                        <p lang="fr">Ce paragraphe est défini en français.</p>
                    </div>
                    <div>
                        <p>I live in Italy.</p>
                        <p lang="it">Ciao bella!</p>
                    </div>
                </div>
                <style>
                    .output {font: 1vw 'Fira Sans', sans-serif;}
                    p::before {padding-right: 5px;}
                    [lang="en-GB"]::before {content: url('british.jpg');}
                    [lang="fr"]::before {content: url('french.png'); width: 10%;}

                    p:lang(it) {background: yellow; width: 30vw; margin-left: 5vw;}
                </style>
            


:checked

top




Male
Female
I have a bike
I have a car
code:
                <div>
                    <div class="choose">
                        <input type="radio" name="my-input" id="yes">
                        <label for="yes">Yes</label>
                    
                        <input type="radio" name="my-input" id="no">
                        <label for="no">No</label>
                    </div>
                    <br>
                    <div class="tick">
                        <input type="checkbox" name="my-checkbox" id="opt-in">
                        <label for="opt-in">Check me!</label>
                    </div>
                    <br>
                    <select name="my-select" id="fruit">
                        <option value="opt1">Apples</option>
                        <option value="opt2">Grapes</option>
                        <option value="opt3">Pears</option>
                    </select>
                    <br><br>
                    <form id="fill" action="">
                        <input type="radio" checked="checked" value="male" name="gender"> Male<br>
                        <input type="radio" value="female" name="gender"> Female<br>
                        <input type="checkbox" value="Bike"> I have a bike<br>
                        <input type="checkbox" value="Car"> I have a car 
                    </form>
                </div>
                <style>
                    div.choose, div.tick, select {margin-left: 5vw; padding: -5vw;}
                    .choose label, .tick label{margin: 0;padding: 0;}
                    /* Labels for checked inputs */
                    input:checked + label {color: red;}
                    /* Radio element, when checked */
                    input[type="radio"]:checked {box-shadow: 0 0 0 3px orange;}
                    /* Checkbox element, when checked */
                    input[type="checkbox"]:checked {box-shadow: 0 0 0 3px hotpink;}
                    /* Option elements, when selected */
                    option:checked {box-shadow: 0 0 0 3px lime; color: red;}

                    #fill{display: flex; flex-flow: column nowrap; justify-content: space-around; 
                        margin-left: 5vw;margin-top: 2vw;}
                    #fill input{margin: 0; padding:0;}
                    #fill input:checked {height: 50px; width: 50px;}
                </style>
            

:default pseudo-class

top

The :default selector selects the default form element in a group of related elements. The :default selector can only be used on <button>, <input type="checkbox">, <input type="radio">, and <option> elements.

Syntax: :default {css declarations;}

examples

The "male" radio button is checked by default:

Male
Female
Other
code:
                    <style>
                        input.again:default {box-shadow: 0 0 1px 1px red;}
                    </style>
                    <div>
                        <p class="spec-1">The "male" radio button is checked by default:</p>
                        <form style="margin-left: 2vw;" action="">
                        <input class="again" type="radio" name="gender" value="male" checked> Male<br>
                        <input class="again" type="radio" name="gender" value="female"> Female<br>
                        <input class="again" type="radio" name="gender" value="other"> Other
                        </form>
                    </div>
                

":fullscreen"

top

The :fullscreen selector is used to select the element(s) that is in full-screen mode.

Syntax: :fullscreen {CSS declarations}

examples

Click on the "Open Fullscreen" button to open this page in fullscreen mode. Close it by either clicking the "Esc" key on your keyboard, or with the "Close Fullscreen" button.

code:
                    <style>
                        /* Safari syntax */
                        :-webkit-full-screen {background-color: red;}
                        /* IE11 syntax */
                        :-ms-fullscreen {background-color: red;}
                        /* Standard syntax */
                        :fullscreen {background-color: red;}
                        /* Style the button */
                        button {padding: 1vw; font-size: 1vw;}
                    </style>  
                    <div>
                        <p class="spec-1">Click on the "Open Fullscreen" button to open this page in 
                        fullscreen mode. Close it by either clicking the "Esc" key on your keyboard, 
                        or with the "Close Fullscreen" button.</p>
                        <button style="margin-left:2vw;" onclick="openFullscreen();">Open Fullscreen</button>
                        <button onclick="closeFullscreen();">Close Fullscreen</button>
                    </div>
                

":indeterminate"

top

The :indeterminate selector selects form elements that are in an indeterminate state. The :indeterminate selector can only be used on <input type="checkbox>, <input type="radio">, and <progress> elements.

Checkboxes cannot be indeterminate with HTML - it is a property of the checkbox object, which can be set to true by JavaScript.

Radio buttons are indeterminate when all radio buttons with the same name value in the form are unchecked.

Syntax: :indeterminate {CSS declarations}

examples

The checkbox below is in an indeterminate state (by JavaScript). If you click on it, it will change its state to "checked", and lose its red shadow color, as it is no longer indeterminate.

Note that an indeterminate checkbox has a "-" icon, rather than a checkmark or an empty box.

Checkbox
code:
                    <style>
                        input:indeterminate {box-shadow: 0 0 1px 1px red;}
                    </style>  
                    <div>
                        <p class="spec-1">The checkbox below is in an indeterminate state (by JavaScript).
                        If you click on it, it will change its state to "checked", and lose its red
                        shadow color, as it is no longer indeterminate.</p>
                        <p class="spec-1">Note that an indeterminate checkbox has a "-" icon, 
                        rather than a checkmark or an empty box.</p>
                        <input style="margin-left:2vw;" type="checkbox" id="myCheckbox"> Checkbox
                    </div>
                    <script>
                        // Make the checkbox indeterminate via JavaScript
                        var checkbox = document.getElementById("myCheckbox");
                        checkbox.indeterminate = true;
                    </script>    
                

":invalid"

top

The :invalid selector selects form elements with a value that does not validate according to the element's settings.

The :invalid selector only works for form elements with limitations, such as input elements with min and max attributes, email fields without a legal email, or number fields without a numeric value, etc.

Use the :valid selector to select form elements with a value that validates according to the element's settings.

Syntax: :invalid {CSS declarations}

examples

Try typing a legal e-mail address, to see the styling disappear.

code:
                    <style>
                        input:invalid { border: 1vw solid red;}
                    </style>  
                    <div>
                        <input style="margin-left:2vw;" type="email" value="supportEmail">
                        <p class="spec-1">Try typing a legal e-mail address, to see the styling disappear.</p>
                    </div>  
                

some practical examples

top

practical example

This is an article headline

Here's the description of this article, if you decide to click through to read it.

This is a teaser for another article

This is a shorter description.

Flooding

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

more flooding

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Lorem ipsum

Duis aute irure dolor in reprehenderit in voluptate.

:has( )

Read about :has( ) at MDN.

code:
            <main>
                <article class="art">
                    <h4>This is an article headline</h4>
                    <p>Here's the description of this article, if you decide to click through 
                    to read it.</p>
                </article>
                <article class="art">
                    <h4>This is a teaser for another article</h4>
                    <p>This is a shorter description.</p>
                </article>
                <article class="art">
                    <h4>Flooding</h4>
                    <img src='../images/6.jpg' alt=''>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
                </article>  
                <article class="art">
                    <h4>more flooding</h4>
                    <img src='../images/7.jpg' alt=''>
                    <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi 
                    ut aliquip ex ea commodo consequat.</p>
                </article>
                <article class="art">
                    <h4>Lorem ipsum</h4>
                    <p>Duis aute irure dolor in reprehenderit in voluptate.</p>
                </article>
                <article class="art">
                    <h4>:has( )</h4>
                    <p>Read about <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:has"
                    >:has( ) at MDN</a>.</p>
                </article>  
            </main>
            <style>
                .vijf {display: grid; grid-template-columns: repeat(auto-fit, minmax(15vw, 1fr)); gap: 1vw; margin: 1vw;}
                .art {background: lightgreen; background: color(display-p3 0.925 0.912 0.865); border-radius: 1vw; padding: 1vw;       display: flex; flex-flow: column;}
                .art:has(img) {grid-column: span 2;grid-row: span 2;}
                .art img {order: -1;}
                h4 {margin: 0; font-weight: 600; line-height: 1.1; color:darkolivegreen;}
                h4 + p {margin-top: 0.33vw;}
                .art:has(img) h4 {margin-top: 1vw;}
                .art p {margin-bottom: 0; font-size: 0.8vw;}
                .vijf a {text-decoration-thickness: 0.4vw; text-decoration-color: rgba(9, 146, 163, 0.3); text-underline-offset: -0.2vw;
                text-decoration-skip-ink: none; color: inherit;}
                .vijf a:hover { color: inherit; color: rgba(9, 146, 163, 1); }
                .art img { max-width: 70%;}
            </style>
        

emaill form using pseudo-classes

code:

                <div class="container">
                    <div class="form">
                        <div class="form_group">
                            <label for="ONE"><i>email</i>
                                <input type="email" name="" id="ONE">
                            </label>
                        </div>
                    </div>   
                </div>
                <style>
                    .form_group label{font-size: 1.4vw;margin-left: 2vw;}
                    .form_group #ONE {border: 0.4vw solid blue;border-radius: 10vw; color: lightgrey; 
                        font-family: 'Montserrat', sans-serif; font-size: 2vw; outline: none; padding: 
                        .5vw 1.5vw; width: 20vw;}
                    .form_group #ONE:focus {border: 0.5vw inset purple;color: black;}
                    .form_group #ONE:not(:focus):invalid {background:crimson; border: 0.5vw inset purple; 
                        color: pink;}
                </style>
            

trigger transitions on other elements using pseudo-classes

Grow!
code:
                    <div class="container-2">
                        <div class="btn">Grow!</div>
                        <div class="balloon"></div>
                    </div>
                    <style>
                        .btn {margin-left: 1vw; background: black;color: white; text-align: center; 
                            font-size: 2vw; width: 8vw; height: 4vw;cursor: pointer; padding: 0.5vw 1vw;
                            border-radius: 5vw;}
                        .balloon {margin-left: 1vw; width: 30vw; height: 20vw; background: blue;  
                            padding-bottom: 1vw; border-radius:40%; transform: scale(0.2, 0.1); 
                            transition: transform 4000ms;}
                        .btn:hover + .balloon{transform: scale(2.5, 1) translateX(10vw); animation: coloring 0.5s linear infinite;}
                        @keyframes coloring{
                            0%{background-color: blue;}
                            100%{background-color: red;}
                        }
                    </style>
                

nt-child vs. nth-of-type

Here's a h3 Header

I'm a paragraph with all kinds of information.

Let's add another paragraph for fun.

Here's a another h3 Header

yadda yadda yadda

blah blah blah

yadda yadda yadda

Here's a h3 Subheader

blah blah blah

And maybe just one more.

Here's another h3 Header

code:
                <article>
                <h3>Here's a h3 Header</h3>
                <p>I'm a paragraph with all kinds of information.</p>
                <p>Let's add another paragraph for fun.</p>
                <h3>Here's a another h3 Header</h3>
                <p>yadda yadda yadda</p>
                <p>blah blah blah</p>
                <p>yadda yadda yadda</p>
                <h3>Here's a h3 Subheader</h3>
                <p>blah blah blah</p>
                <p>And maybe just one more.</p>
                <h3>Here's another h3 Header</h3>
                </article>
                <style>
                    article{margin-left: 5vw; font-size: 2vw;}
                    article p:nth-child(odd) {color: goldenrod;}
                    article h3:nth-of-type(even) {color: green;}
                    article p:nth-child(odd):before{content: "('odd' p child) - - - "; 
                    color: red; font-size:1vw; 
                    font-weight: bold;}
                    article h3:nth-of-type(even):before{content: "('even' h3 child) - - -  "; 
                    color: red; 
                    font-size:1vw;}
                </style>
            

apply alternating background colors

code:
                <ul class="old">
                    <li class="last">Item 1</li>
                    <li class="last">Item 2</li>
                    <li class="last">Item 3</li>
                    <li class="last">Item 4</li>
                    <li class="last">Item 5</li>
                    <li class="last">Item 6</li>
                    <li class="last">Item 7</li>
                    <li class="last">Item 8</li>
                    <li class="last">Item 9</li>
                </ul>
                <style>
                        li.last:nth-child(3n + 1) {background-color: black;}
                        li.last:nth-child(3n + 2) {background-color: yellow; color: rgba(0, 0, 0, 0.7);}
                        li.last:nth-child(3n + 3) {background-color: red;}
                        ul .old {list-style: none; margin: 0 7vw; padding: 0;  min-height: 100vh;display: grid;}
                        li .last{display: grid; place-content: center; padding: 1vw 0.5vw; text-align: center ;
                            color: rgba(255, 255, 255, 0.95);   font-weight: bold; font-size: 2vw;}
                </style>
            

focused-within used in a form

This is a form.

This form breaks at 600px and goes from a left-label form to a top-label form. At above 1200px, the labels align right. It also indicates focused fields with :focus-within.
Select a Choice
Check All That Apply
code:
                <form action="#">
                <header>
                    <h4>This is a form.</h4>
                    <div>This form breaks at 600px and goes from a left-label form to a
                    top-label form. 
                    At above 1200px, the labels align right. It also indicates focused 
                    fields with :focus-within.</div>
                </header>
                <div>
                    <label class="desc" id="title_1" for="Field_1">full name</label>
                    <div><input id="Field_1" name="Field_1" type="text" 
                    class="field text fn" 
                    value="" size="20" tabindex="1"></div>
                </div>
                
                <div>
                    <label class="desc" id="title_3" for="Field_3">email</label>
                    <div><input id="Field_3" name="Field_3" type="email" 
                    spellcheck="false" 
                    value="" maxlength="255" tabindex="3"></div>
                </div>
                    
                <div>
                    <label class="desc" id="title_4" for="Field_4">message</label>
                    <div><textarea id="Field_4" name="Field_4" spellcheck="true" 
                    rows="10" cols="50" tabindex="4"></textarea></div>
                </div>
                    
                <div>
                    <fieldset>
                    <legend id="title_5" class="desc">Select a Choice</legend>
                    <div><input id="radioDefault_5" name="Field_5" type="hidden" 
                    value="">
                        <div>
                            <input id="Field5_0" name="Field_5" type="radio" 
                            value="First Choice" tabindex="5" checked="checked">
                            <label class="choice" for="Field5_0">First Choice</label>
                        </div>
                        <div>
                            <input id="Field5_1" name="Field_5" type="radio" value="Second 
                            Choice" tabindex="6">
                            <label class="choice" for="Field5_1">Second Choice</label>
                        </div>
                        <div>
                            <input id="Field5_2" name="Field_5" type="radio" value="Third 
                            Choice" tabindex="7">
                            <label class="choice" for="Field5_2">Third Choice</label>
                        </div>
                    </div>
                    </fieldset>
                </div>
                
                <div>
                    <fieldset>
                    <legend id="title6" class="desc">Check All That Apply</legend>
                    <div>
                        <div>
                            <input id="Field6" name="Field6" type="checkbox" value="First 
                            Choice" tabindex="8">
                            <label class="choice" for="Field6">First Choice</label>
                        </div>
                        <div>
                            <input id="Field7" name="Field7" type="checkbox" value="Second 
                            Choice" tabindex="9">
                            <label class="choice" for="Field7">Second Choice</label>
                        </div>
                        <div>
                            <input id="Field8" name="Field8" type="checkbox" value="Third 
                            Choice" tabindex="10">
                            <label class="choice" for="Field8">Third Choice</label>
                        </span>
                        </div>
                    </fieldset>
                </div>
                
                <div>
                    <label class="desc" id="title106" for="Field106">Select a Choice</label>
                    <div>
                        <select id="Field106" name="Field106" class="field select medium"
                        tabindex="11"> 
                        <option value="First Choice">First Choice</option>
                        <option value="Second Choice">Second Choice</option>
                        <option value="Third Choice">Third Choice</option>
                        </select>
                    </div>
                </div>
                
                <div>
                    <div><input id="saveForm" name="saveForm" type="submit" value="Submit"></div>
                </div>
                </form>
                <style>
                form:focus-within {background-image: linear-gradient(15deg, burlywood, pink,
                    magenta);}
                form header {padding-left: 3vw; font-size: 1.15vw;}
                form header div {font-size: 100%; color: #000;}
                form header h3 {margin: 0 0 1vw 0;}
                form > div {clear: both; overflow: hidden; padding: 0.5vw 2vw;}
                form > div:last-child {padding-bottom: 2vw;}
                form > div:focus-within {background: #a1c084;}
                form > div > fieldset > div > div {margin: 0 0 1vw 0;}
                form > div > label, legend {width: 25%; float: left; padding-right: 2vw;}
                form > div > div, form > div > fieldset > div {width: 75%;float: right;}
                form > div > fieldset label {font-size: 100%;}
                fieldset {border: 0; padding: 0;}
                input[type=text], input[type=email], input[type=url], input[type=password], textarea {width: 100%;
                    border-top: 0.3vw solid #aaa; border-left: 0.3vw solid #aaa; border-right: 0.3vw solid #bbb; 
                    border-bottom: 0.3vw solid #bbb; }
                input[type=text]:focus, input[type=email]:focus, input[type=url]:focus, input[type=password]:focus, 
                textarea:focus { outline: 0; border-color: cyan;}

                @media (max-width: 600px) {
                form > div > label, legend {width: 100%; float: none; margin: 0 0 0.5vw 0;}
                form > div > div, form > div > fieldset > div {width: 100%; float: none;}
                input[type=text], input[type=email], input[type=url], input[type=password],  textarea,
                select { width: 100%; }
                }
                @media (min-width: 1200px) {
                form > div > label, legend {text-align: right;}
                }
                </style>

            

example 1

This is a link

code:
                    <style>
                        /* unvisited link */
                        a:link {color: red;}
                        /* visited link */
                        a:visited {color: green;}
                        /* mouse over link */
                        a:hover {color: hotpink;}
                        /* selected link */
                        a:active {color: blue;}
                    </style>
                    <p class="ex"><b><a href="https://www.lwitters.com" target="_blank">This is a link</a></b></p>
                

example 2

Mouse Over Me

Tada! Here I am!

code:
                    <style>
                        .one {background-color: green; color: white; padding: 2vw; text-align: center;}
                        .one:hover {background-color: blue;}
                        .two {display: none; background-color: red; padding: 2vw;}
                        .one:hover .two {display: block;}
                    </style>
                    <div class="one">Mouse Over Me
                        <p class="two spec">Tada! Here I am!</p>
                    </div>