css3按钮聚焦状态处理

817
2024/10/23 12:31:07
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在CSS3中,可以通过:focus伪类选择器来处理按钮的聚焦状态。以下是一个简单的示例:

HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Button Focus Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <button class="my-button">Click me</button>
</body>
</html>

CSS代码 (styles.css):

.my-button {
  background-color: blue;
  color: white;
  padding: 10px 20px;
  border: none;
  cursor: pointer;
  outline: none;
}

.my-button:focus {
  background-color: red;
}

在这个示例中,我们创建了一个名为.my-button的按钮。当按钮聚焦时(例如,通过点击或使用Tab键导航到按钮),:focus伪类选择器会将其背景颜色更改为红色。同时,我们还移除了按钮的默认轮廓线,以使其看起来更美观。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: css3如何实现动画