代码示例:(标识:css_form_focus_2)
<!DOCTYPE html>
<html>
<head>
<style> 
input[type=text] {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  box-sizing: border-box;
  border: 3px solid #ccc;
  -webkit-transition: 0.5s;
  transition: 0.5s;
  outline: none;
}

input[type=text]:focus {
  border: 3px solid #555;
}
</style>
</head>
<body>

<p>在本例中,我们使用了 :focus 选择器,在文本字段获得焦点时(被点击)为其添加黑色边框:</p>
<p>请注意,我们已添加 CSS 过渡属性以设置边框颜色的动画(改变颜色需 0.5 秒)。</p>

<form>
  <label for="fname">First Name</label>
  <input type="text" id="fname" name="fname" value="Bill">
  <label for="lname">Last Name</label>
  <input type="text" id="lname" name="lname" value="Gates">
</form>

</body>
</html>
运行结果: