CSS focus

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

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

适用范围

所有 HTML 元素,例如按钮、下拉列表、输入字段、

语法

selector:focus{ properties } 

CSS focus: 链接

以下是在链接上设置焦点的示例:

<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>单击链接或按 Tab 键。</p>
    <p><p>此处的链接在聚焦时<a href="#0">背景和前景色发生了变化</a>。</p></p>
</body>
</html> 

CSS 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>
     <p>单击按钮或按 Tab 键。</p>
   <button>点击看看</button>
</body>
</html> 

CSS 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>
    <p>单击文本框或按 Tab 键。</p>
   <form>
      <label>
         Full Name
      <input type="text">
      </label>
   </form>
</body>
</html> 

CSS 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>
   <p>从下拉框中选择一个项目。</p>
   <form>
      <label>
         您喜欢的水果:
         <select name="flavor">
            <option>哈密瓜</option>
            <option>樱桃</option>
            <option>苹果 </option>
            <option>石榴</option>
            <option>香蕉</option>
          </select>
      </label>
   </form>
</body>
</html> 

CSS 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">选中复选框以切换</label>
      <input type="checkbox" id="toggle1" />
      <span></span>
    </div>
</body>
</html> 

CSS focus: 关联属性

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

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