CSS :focus-within Selector

The :focus-within pseudo-class is a selects an element that consists of a focused element as a child. The CSS rules are applied when any child element gets focus. Example includes clicking a link, selecting an input, etc.

Syntax:

:focus {
    /* CSS Properties */
}

Example: Below is the example which illustrates the use of :focus-within pseudo-class Selector.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <style>
        form {
            border: 2px solid red;
            padding: 15px;
            width: 30%;
        }
  
        form:focus-within {
            background: #ff8;
            color: black;
        }
  
        input {
            margin: 5px;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <form>
  
        <label>Geek 1</label>
        <input type="text">
        <br>
        <label>Geek 2</label>
        <input type="text">
    </form>
</body>
  
</html>

chevron_right


Output:

Supported Browsers:

  • Chrome
  • Firefox
  • Edge
  • Opera
  • Safari
  • Internet Explorer (Not Supported).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.