search

Home  >  Q&A  >  body text

Safari does not support the :focus-within pseudo-class

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #my-button:focus-within {
      color: red;
    }
  </style>
</head>
<body>
  <button id="my-button" tabindex="0">item2</button>
</body>
</html>

When pressed, the color should switch to red. Works in Chrome and Firefox, always has issues with Safari (tested on version 15.6.1).

But what is the problem?

P粉561438407P粉561438407438 days ago606

reply all(1)I'll reply

  • P粉141911244

    P粉1419112442023-09-15 13:05:45

    It seems :focus-within cannot be applied to buttons in Safari. But this works:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <style>
        #my-li:focus-within > #my-button {
          color: red;
        }
      </style>
    </head>
    <body>
      <ul>
        <li id="my-li" tabindex="0">
          <button id="my-button">item2</button>
        </li>
      </ul>
    </body>
    </html>

    reply
    0
  • Cancelreply