CSS - 伪类 :focus 属性



CSS 中的:focus伪类表示已获得焦点的元素。此类通常在用户单击、点击元素或使用键盘的 Tab 键选择元素时触发。

:focus伪类应用于聚焦的元素本身。为了选择包含聚焦元素的元素,请使用:focus-within伪类。

语法

selector:focus{ properties }

CSS :focus 示例

以下示例演示了在链接上使用:focus伪类。单击链接或按 Tab 键查看链接上的样式更改。

<html>
<head>
<style>
    a {
        color: darkviolet;
        transition: all 0.3s ease;
    }

    a:focus {
        outline: none;
        color: red;
        background-color: yellow;
    }

    body {
        margin: 5px;
        padding: 2rem;
        display: grid;
        font: 20px/1.4 system-ui, -apple-system, sans-serif;
    }
</style>
</head>
<body>
    <p>The link here has a <a href="#0">change in background and foreground color</a> as it is focused.</p>
</body>
</html>

以下示例演示了在按钮上使用:focus伪类。单击按钮以查看按钮上的样式更改。

<html>
<head>
<style>
   button {
      display: block;
      margin: 2em;
      padding: 2px;
      font-weight: bold;
      font-size: 18px;
      text-align: center;
      color: black;
      background-color: #fff;
      border: 2px solid black;
      border-radius: 24px;
      cursor: pointer;
      transition: all 0.3s ease;
      width: 150px;
      height: 50px;
   }
   button:hover {
      color: blue;
      border-color: black;
      background-color: lightpink;
   }
   button:focus {
      background-color: darkred;
      color: white;
      border-radius: 2px;
   }
</style>
</head>
<body>
    <button>Click Me!!</button>
</body>
</html>

以下示例演示了在输入框上使用:focus伪类。单击输入框以查看输入框中的样式更改。

<html>
<head>
<style>
   label {
      display: block;
      font-size: 20px;
      color: black;
      width: 500px;
   }

   input[type="text"] {
      padding: 10px 16px;
      font-size: 16px;
      color: black;
      background-color: #fff;
      border: 1px solid #597183;
      border-radius: 8px;
      margin-top: 5px;
      width: 500px;
      transition: all 0.3s ease;
   }

   input[type="text"]:focus {
      background-color: lightyellow;
   }
</style>
</head>
<body>
   <form>
      <label>
         Full Name
      <input type="text">
      </label>
   </form>
</body>
</html>

以下示例演示了在下拉菜单上使用:focus伪类。单击下拉菜单以查看下拉菜单的样式更改。

<html>
<head>
<style>
   label {
      display: block;
      font-size: 18px;
      color: black;
      width: 500px;
   }

   select {
      padding: 10px 16px;
      font-size: 16px;
      color: black;
      background-color: #fff;
      border: 1px solid #597183;
      border-radius: 8px;
      margin-top: 25px;
      width: 300px;
      transition: all 0.3s ease;
   }

   select:focus {
      background-color: rgb(173, 233, 209);
      box-shadow: 20px 15px 30px yellow, -20px 15px 30px lime, -20px -15px 30px blue, 20px -15px 30px red;
   }
</style>
</head>
<body>
   <form>
      <label>
         Ice cream Flavors:
         <select name="flavor">
            <option>Cookie dough</option>
            <option>Pistachio</option>
            <option>Cookies & Cream</option>
            <option>Cotton Candy</option>
            <option>Lemon & Raspberry Sorbet</option>
          </select>
      </label>
   </form>
</body>
</html>

以下示例演示了在切换按钮上使用:focus伪类。选中复选框以查看切换按钮的样式更改。

<html>
<head>
<style>
.toggle {
  position: relative;
  display: block;
  width: 100%;
  margin: 2em;
}

.toggle label {
  padding-right: 8px;
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  font-size: 16px;
  line-height: 28px;
  color: black;
}

.toggle span {
  position: relative;
  display: inline-block;
  vertical-align: middle;
  cursor: pointer;
}

.toggle span:before {
  display: block;
  width: 50px;
  height: 30px;
  content: "";
  background-color: #a9adaf;
  border-radius: 28px;
  transition: background-color 0.3s ease;
}

.toggle span:after {
  position: absolute;
  top: 1px;
  left: 1px;
  display: block;
  width: 30px;
  height: 28px;
  visibility: visible;
  content: "";
  background-color: #fff;
  border-radius: 28px;
  transition: left 0.3s ease;
}

input[type="checkbox"]:checked ~ span:before {
  background-color: orange;
}

input[type="checkbox"]:checked ~ span:after {
  top: 1px;
  left: 21px;
}

input[type="checkbox"]:hover ~ span:before {
  background-color: #1194d1;
}

input[type="checkbox"]:not(:checked):hover ~ span:before {
  background-color: #afbec9;
}

input[type="checkbox"]:focus ~ span:before {
  outline: none;
  box-shadow: 0 0 0 4px red;
}
</style>
</head>
<body>
    <div class="toggle">
      <label for="toggle1">Check the box to toggle me</label>
      <input type="checkbox" id="toggle1" />
      <span></span>
    </div>
</body>
</html>

除了:focus之外,还有另外两个与:focus伪类相关的伪类。它们列在下面给出的表格中

序号 伪类 描述
1 :focus-visible 在元素上使焦点变得明显,并显示给用户。
2 :focus-within 将焦点设置在包含聚焦元素的元素上。
广告