- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 包含
- CSS - 测量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 内联边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 内联块级元素
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - 清除浮动
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 位置
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 转换
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 导航栏
- CSS - 覆盖层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载动画
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪裁
- CSS - 书写模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - 全部
- CSS - 内嵌
- CSS - 隔离
- CSS - 滚动溢出
- CSS - Justify Items
- CSS - Justify Self
- CSS - 制表符大小
- CSS - 指针事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大块级尺寸
- CSS - 最小块级尺寸
- CSS - 混合模式
- CSS - 最大内联尺寸
- CSS - 最小内联尺寸
- CSS - 偏移量
- CSS - 口音色
- CSS - 用户选择
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 图层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文本效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - Important
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - 盒装饰中断
- CSS - 光标颜色
- CSS - 文本阴影
- CSS - 文本
- CSS - 2d 变换
- CSS - 3d 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特异性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS - 按钮
按钮是交互式元素,允许用户在您的网站或应用程序上执行操作。按钮通常呈矩形或圆形,并带有文本标签,说明单击时会发生什么。
在浏览网页时,您可能遇到过各种交互式元素,例如提交按钮。在本文中,我们将介绍如何使用 CSS 样式化按钮,探索不同类型的按钮,并讨论相关属性。
目录
如何在 CSS 中设置按钮样式?
- 设置尺寸:在 CSS 中,height 和 width 属性可用于调整网页中按钮的大小。
- 定义颜色和边框:与 UI 兼容的颜色和边框以及合适的厚度使按钮脱颖而出。background-color 和 border 属性可用于设置 css 中任何按钮的颜色和边框。
- 阴影效果:使用 box-shadow 属性在按钮周围添加阴影效果可以增强按钮样式。
- 悬停效果:交互式样式(如悬停效果)会在用户将鼠标悬停在按钮上时更改按钮的样式。这可能包括更改不透明度、大小(使用转换)等。
- 动画按钮:CSS animation 可用于创建动态交互式按钮。
设置按钮颜色
如上所述,我们可以在 CSS 中使用 background-color 属性为按钮提供正确的颜色。查看示例
示例
在此示例中,我们使用与背景颜色 UI 匹配的不同 颜色 样式化按钮。
<!DOCTYPE html>
<html>
<head>
<style>
body{
background: black;
padding: 40px;
}
button {
padding: 20px;
margin-bottom: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<button style="background: #FFD700">
Yellow Button
</button>
<button style="background: #00FFFF">
Blue Button
</button>
<button style="background: #FFFFFF">
White Button
</button>
<button style="background: #FF4500">
Orange Button
</button>
<button style="background: #32CD32">
Lime Button
</button>
</body>
</html>
设置按钮边框
边框是按钮边缘周围的空间。我们可以使用 border 属性为按钮边框设置样式。
border 属性采用三个值:边框厚度、类型和颜色。
示例
以下是如何使用以下方法创建具有不同边框颜色的按钮的示例:
<!DOCTYPE html>
<html>
<head>
<style>
button {
font-size: 12px;
padding: 10px;
margin: 5px;
background-color: #f0f0f0;
}
.style1 {
border: 3px solid darkred;
}
.style2 {
border: 3px solid gold;
}
.style3 {
border: 3px solid black;
}
</style>
</head>
<body>
<button class="style1">
darkred border
</button>
<button class="style2">
gold border
</button>
<button class="style3">
black border
</button>
</body>
</html>
创建圆角按钮
我们可以使用 border-radius 属性创建圆角按钮或完全圆形的按钮。
/* Round cornered button */ border-radius: 10px; /* Circular button */ border-radius: 50%;
示例
以下是如何创建圆角按钮的示例。
<!DOCTYPE html>
<html>
<head>
<style>
button {
font-size: 12px;
padding: 10px;
margin: 5px;
}
.style1 {
border-radius: 10px;
background-color: violet;
}
.style2 {
border-radius: 20px;
background-color: pink;
}
.style3 {
border-radius: 50%;
background-color: violet;
}
</style>
</head>
<body>
<button class="style1">
Border-radius: 10px;
</button>
<button class="style2">
Border-radius: 20px;
</button>
<button class="style3">
Circle
</button>
</body>
</html>
按钮悬停效果
在不点击按钮的情况下将鼠标指针移到按钮上方称为悬停。我们可以使用 :hover 伪类 为按钮的悬停状态设置样式。
button:hover{
property: value;
}
示例
以下是如何创建按钮悬停效果的示例。
<!DOCTYPE html>
<html>
<head>
<style>
button {
font-size: 12px;
padding: 10px;
margin: 5px;
background-color: #1156b3; /* Darker blue */
}
.style1:hover {
background-color: #0069d9; /* Slightly darker blue */
}
.style2:hover {
transform: scale(1.2); /* Size increase effect */
}
.style3:hover {
background-color: lightblue;
}
</style>
</head>
<body>
<button class="style1">
Button 1
</button>
<button class="style2">
Button 2
</button>
<button class="style3">
Button 3
</button>
</body>
</html>
按钮焦点和激活状态样式
按钮的焦点状态是在按钮获得焦点时的状态,通常是在单击或使用 Tab 键切换到它之后。按钮的激活状态是在单击按钮时的状态。我们可以使用 伪类 :focus. 和 :active 为这些状态设置样式。
button:focus {
property: value;
}
button:active {
property: value;
}
示例
以下是如何设置按钮的焦点状态和激活状态的示例。
<!DOCTYPE html>
<html>
<head>
<style>
.style-button {
display: inline-block;
padding: 15px;
background-color: pink;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.1s ease;
}
.style-button:hover {
background-color: violet;
}
.style-button:focus {
outline: none;
box-shadow: 0 0 5px 2px blue;
}
.style-button:active {
transform: translateY(2px);
background-color: yellow;
}
</style>
</head>
<body>
<button class="style-button">Press Me</button>
</body>
</html>
在按钮上设置阴影效果
在 CSS 中,box-shadow 属性用于在按钮周围添加阴影效果。
盒阴影由相对于元素的水平和垂直偏移量、模糊和扩展半径以及颜色描述。以下是 box-shadow 的语法
button {
box-shadow: inset horizontal vertical
blur-radius spread-color;
}
示例
以下是如何创建带有投影的按钮的示例。
<!DOCTYPE html>
<html>
<head>
<style>
button {
font-size: 12px;
padding: 10px;
margin: 5px;
}
.style1 {
background-color: pink;
box-shadow: 0 5px 10px 0 red;
}
.style2 {
background-color: yellow;
}
.style3:hover {
background-color: yellow;
box-shadow: 0 5px 10px 0 orange;
}
</style>
</head>
<body>
<button class="style1">
Button 1
</button>
<button class="style2">
Button 2
</button>
<button class="style3">
Button 3
</button>
</body>
</html>
禁用按钮
禁用按钮是指不可点击的按钮。满足某些条件后,可以使用 JavaScript 启用此按钮。
我们可以通过将 cursor 属性设置为 not-allowed 来创建禁用按钮。
示例
以下是一个示例。
<!DOCTYPE html>
<html>
<head>
<style>
button {
font-size: 12px;
padding: 10px;
margin: 5px;
}
.style1 {
background-color: pink;
cursor: pointer;
}
.style2 {
background-color: yellow;
opacity: 0.5;
cursor: not-allowed;
}
</style>
</head>
<body>
<button class="style1">
Normal Button
</button>
<button class="style2">
Disabled Button
</button>
</body>
</html>
设置按钮宽度
我们可以使用 width 属性定义按钮的水平宽度。
示例
以下是如何创建具有不同宽度的按钮的示例。
<!DOCTYPE html>
<html>
<head>
<style>
button {
font-size: 12px;
padding: 10px;
margin: 5px;
}
.style1 {
background-color: pink;
width: 200px;
}
.style2 {
background-color: violet;
width: 50%;
}
.style3 {
background-color: yellow;
width: 100%;
}
</style>
</head>
<body>
<button class="style1">
width 100px
</button><br>
<button class="style2">
width 50%
</button><br>
<button class="style3">
width 100%
</button><br>
</body>
</html>
CSS 按钮组
以下是如何通过删除边距并为每个按钮添加 float: left 属性来创建水平按钮组的示例。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.button-group {
display: flex;
justify-content: center;
float: left;
}
.style-button {
background-color: yellow;
border: none;
padding: 10px 20px;
float: left;
text-align: center;
text-decoration: none;
font-size: 16px;
}
.style-button:hover {
background-color: orange;
}
</style>
</head>
<body>
<div class="button-group">
<button class="style-button">Submit</button>
<button class="style-button">Cancel</button>
<button class="style-button">Reset</button>
</div>
</body>
</html>
CSS 垂直按钮组
以下是如何通过设置 display: block 和 float: left 属性来创建垂直按钮组的示例
示例
<!DOCTYPE html>
<html>
<head>
<style>
.button-group {
justify-content: center;
float: left;
}
.style-button {
background-color: yellow;
border: 2px solid orange;
padding: 10px 20px;
text-align: center;
display: block;
text-decoration: none;
font-size: 16px;
width: 100px;
}
.style-button:hover {
background-color: violet;
}
</style>
</head>
<body>
<div class="button-group">
<button class="style-button">Home</button>
<button class="style-button">Blog</button>
<button class="style-button">Setting</button>
</div>
</body>
</html>
CSS 图片上的按钮
以下是如何使用相对定位将按钮覆盖在图像上的示例
示例
<!DOCTYPE html>
<html>
<head>
<style>
.image-container {
position: relative;
display: inline-block;
}
img {
width: 300px;
height: 200px;
}
button {
position: absolute;
top: 40%;
left: 30%;
background-color: yellow;
border: none;
padding: 15px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 15px;
}
button:hover {
background-color: orange;
}
</style>
</head>
<body>
<div class="image-container">
<img src="/css/images/tree.jpg" alt="Your Image">
<button>Click Me</button>
</div>
</body>
</html>
CSS 动画按钮
在 CSS 中,我们可以使用 伪元素 来为按钮单击效果制作动画。
示例
以下是如何在按钮上创建动画效果的示例
<!DOCTYPE html>
<html>
<head>
<style>
button {
display: inline-block;
padding: 15px;
background-color: violet;
border: none;
text-align: center;
text-decoration: none;
font-size: 20px;
cursor: pointer;
position: relative;
overflow: hidden;
}
button::before {
content:"";
position: absolute;
width: 0;
height: 100%;
top: 0;
left: 0;
background-color: pink;
transition: width 0.3s ease-in-out;
}
button:hover::before {
width: 100%;
}
.icon::after {
content: '\2192';
margin-left: 10px;
}
</style>
</head>
<body>
<button>
Hover Me <span class="icon"></span>
</button>
</body>
</html>