按钮是交互式元素,允许用户在您的网站或应用程序上执行操作。按钮通常为矩形或圆形,并有一个文本标签,说明单击按钮时会发生什么。
要创建 CSS 按钮,您需要使用 HTML 中的 <button> 元素。然后,您可以使用 CSS 来设置按钮的样式。
带有按钮的基本 HTML 结构如以下代码所示:
<html>
<head>
</head>
<body>
<button class="style-button">点击</button>
</body>
</html>
使用 CSS 将样式添加到按钮:
.style-button {
background-color: pink;
border: none;
border-radius: 10px;
padding: 15px;
font-size: 16px;
text-decoration: none;
cursor: pointer;
}
CSS 按钮: 基本示例
这里是如何使用 CSS 设置按钮和链接样式的示例 -
<html>
<head>
<style>
.style-button {
background-color: pink;
border: none;
padding: 10px;
margin: 5px;
font-size: 12px;
text-decoration: none;
}
</style>
</head>
<body>
<button>基本CSS按钮</button>
<button class="style-button">CSS 样式按钮</button>
<a href="#" class="style-button">CSS 链接按钮</a>
<input type="button" class="style-button" value="CSS Input 按钮">
</body>
</html>
CSS 按钮颜色
这里是如何使用 CSS 为具有不同 color -
<html>
<head>
<style>
.style-button {
display: inline-block;
border: none;
padding: 10px;
margin: 5px;
font-size: 12px;
text-decoration: none;
}
.color-pink {
background-color:pink;
}
.color-green {
background-color: greenyellow;
}
.color-violet {
background-color: violet;
}
.color-blue {
background-color: skyblue;
}
.color-yellow {
background-color: yellow;
}
</style>
</head>
<body>
<button class="style-button color-pink">粉色按钮</button>
<button class="style-button color-green">绿色按钮</button>
<button class="style-button color-violet">紫色按钮</button>
<button class="style-button color-blue">蓝色按钮</button>
<button class="style-button color-yellow">黄色按钮</button>
</body>
</html>
CSS 按钮尺寸
这里是如何使用 CSS 为具有不同font-size -
<html>
<head>
<style>
.style-button {
display: inline-block;
border: none;
padding: 10px;
margin: 5px;
text-decoration: none;
background-color: violet;
}
.size1 {
font-size: 10px;
}
.size2 {
font-size: 15px;
}
.size3 {
font-size: 20px;
}
.size4 {
font-size: 25px;
}
.size5 {
font-size: 30px;
}
</style>
</head>
<body>
<button class="style-button size1">Size 10px</button>
<button class="style-button size2">Size 15px</button>
<button class="style-button size3">Size 20px</button>
<button class="style-button size4">Size 25px</button>
<button class="style-button size5">Size 30px</button>
</body>
</html>
CSS 按钮填充
以下是如何使用 padding 属性向按钮添加填充的示例 -
<html>
<head>
<style>
.style-button {
display: inline-block;
border: none;
font-size: 12px;
margin: 5px;
text-decoration: none;
}
.padding-style1 {
padding: 5px 10px;
background-color: violet;
}
.padding-style2 {
padding: 10px;
background-color: pink;
}
.padding-style3 {
padding: 10px 20px 10px 20px;
background-color: violet;
}
.padding-style4 {
padding: 15px 30px;
background-color: pink;
}
.padding-style5 {
padding: 30px 15px;
background-color: violet;
}
</style>
</head>
<body>
<button class="style-button padding-style1">Padding 5px 10px</button>
<button class="style-button padding-style2">Padding 10px</button>
<button class="style-button padding-style3">Padding 10px 20px 10px 20px</button>
<button class="style-button padding-style4">Padding 15px 30px</button>
<button class="style-button padding-style5">Padding 30px 15px</button>
</body>
</html>
CSS 圆角按钮
以下是如何使用 border-radius CSS 属性创建圆角按钮的示例 -
<html>
<head>
<style>
.style-button {
display: inline-block;
border: none;
font-size: 12px;
text-decoration: none;
padding: 10px;
margin: 5px;
}
.border-style1 {
border-radius: 10px;
background-color: violet;
}
.border-style2 {
border-radius: 20px;
background-color: pink;
}
.border-style3 {
border-radius: 50%;
background-color: violet;
}
</style>
</head>
<body>
<button class="style-button border-style1">border-radius: 10px</button>
<button class="style-button border-style2">border-radius: 20px</button>
<button class="style-button border-style3">circle</button>
</body>
</html>
CSS 彩色按钮边框
这里是如何使用 border 属性创建具有不同边框颜色的按钮的示例 -
<html>
<head>
<style>
.style-button {
display: inline-block;
font-size: 12px;
text-decoration: none;
padding: 10px;
margin: 5px;
background-color: white;
}
.border-style1 {
border: 3px solid red;
}
.border-style2 {
border: 3px solid yellow;
}
.border-style3 {
border: 3px solid green;
}
.border-style4 {
border: 3px solid pink;
}
</style>
</head>
<body>
<button class="style-button border-style1">Red Border</button>
<button class="style-button border-style2">Yellow Border</button>
<button class="style-button border-style3">Green Border</button>
<button class="style-button border-style4">Pink Border</button>
</body>
</html>
CSS 悬停按钮
这里是如何使用 :hover 伪类创建悬停按钮的示例 -
<html>
<head>
<style>
.style-button {
display: inline-block;
font-size: 12px;
text-decoration: none;
padding: 10px;
margin: 5px;
}
.hover-style1 {
background-color: pink;
border: none;
}
.hover-style2 {
border: 3px solid yellow;
background-color: white;
}
.hover-style1:hover {
border: 3px solid pink;
background-color: white;
}
.hover-style2:hover {
background-color: yellow;
}
</style>
</head>
<body>
<button class="style-button hover-style1">悬停按钮</button>
<button class="style-button hover-style2">悬停按钮</button>
</body>
</html>
CSS 阴影按钮
这里是如何创建按钮的示例使用 box-shadow 属性添加阴影 -
<html>
<head>
<style>
.style-button {
display: inline-block;
font-size: 12px;
text-decoration: none;
padding: 10px;
margin: 10px;
border: none;
}
.shadow-style1 {
background-color: pink;
box-shadow: 0 5px 10px 0 red;
}
.shadow-style2 {
background-color: yellow;
}
.shadow-style2:hover {
background-color: yellow;
box-shadow: 0 5px 10px 0 orange;
}
</style>
</head>
<body>
<button class="style-button shadow-style1">阴影按钮</button>
<button class="style-button shadow-style2">悬停阴影按钮</button>
</body>
</html>
CSS 禁用按钮
这里是如何通过将光标设置为不允许来创建禁用按钮的示例 -
<html>
<head>
<style>
.style-button {
display: inline-block;
font-size: 12px;
text-decoration: none;
padding: 10px;
margin: 5px;
border: none;
background-color: pink;
}
.normal-button {
cursor: pointer;
}
.disable-button {
opacity: 0.5;
cursor: not-allowed;
}
</style>
</head>
<body>
<button class="style-button normal-button">点击</button>
<button class="style-button disable-button disabled">禁用按钮</button>
</body>
</html>
CSS 按钮宽度
这里是如何创建具有不同属性的按钮的示例宽度 -
<html>
<head>
<style>
.style-button {
display: inline-block;
font-size: 12px;
text-decoration: none;
padding: 10px;
border: none;
margin: 5px;
}
.button-width-px {
background-color: pink;
width: 200px;
}
.half-width-button {
background-color: violet;
width: 50%;
}
.full-width-button {
background-color: yellow;
width: 100%;
}
</style>
</head>
<body>
<button class="style-button button-width-px">Width: 200px</button><br>
<button class="style-button half-width-button">Half Width Button</button>
<button class="style-button full-width-button">Full Width Button</button>
</body>
</html>
CSS 按钮组
以下是如何创建的示例通过删除边距并向每个按钮添加 float: left 属性来创建水平按钮组 -
<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">提交</button>
<button class="style-button">取消</button>
<button class="style-button">重置</button>
</div>
</body>
</html>
CSS 边框按钮组
这里是如何创建边框按钮组的示例通过使用 border 属性 -
<html>
<head>
<style>
.button-group {
display: flex;
justify-content: center;
float: left;
}
.style-button {
background-color: yellow;
border: 2px solid orange;
padding: 10px 20px;
float: left;
text-align: center;
text-decoration: none;
font-size: 16px;
}
.style-button:hover {
background-color: violet;
}
</style>
</head>
<body>
<div class="button-group">
<button class="style-button">提交</button>
<button class="style-button">取消</button>
<button class="style-button">重置</button>
</div>
</body>
</html>
CSS 垂直按钮组
这里是一个示例如何通过查看 display: block 和 float: left 属性来创建垂直按钮组 -
<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">首页</button>
<button class="style-button">博客</button>
<button class="style-button">配置</button>
</div>
</body>
</html>
图像上的 CSS 按钮
这里是如何在顶部覆盖按钮的示例使用相对定位的图像 -
<html>
<head>
<style>
.image-container {
position: relative;
display: inline-block;
}
.image {
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 class="image" src="images/tree.jpg" alt="Your Image">
<button class="button" href="#">点击</button>
</div>
</body>
</html>
CSS 动画按钮
这里是如何在按钮上创建动画效果的示例 -
<html>
<head>
<style>
.arrow-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;
}
.arrow-button::before {
content:"";
position: absolute;
width: 0;
height: 100%;
top: 0;
left: 0;
background-color: pink;
transition: width 0.3s ease-in-out;
}
.arrow-button:hover::before {
width: 100%;
}
.arrow-icon::after {
content: '\2192';
margin-left: 10px;
}
</style>
</head>
<body>
<button class="arrow-button" href="#">鼠标悬停看效果<span class="arrow-icon"></span></button>
</body>
</html>
CSS 按钮按下效果
这是一个如何在单击按钮时向按钮添加按下效果的示例 -
<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:active {
transform: translateY(2px);
background-color: yellow;
}
</style>
</head>
<body>
<button class="style-button">点击</button>
</body>
</html>
CSS 按钮淡入效果
这里是一个悬停时如何向按钮添加淡入淡出效果的示例 -
<html>
<head>
<style>
.style-button {
display: inline-block;
padding: 15px;
background-color: violet;
border: none;
border-radius: 5px;
cursor: pointer;
}
.style-button:hover {
opacity: 0.5;
}
</style>
</head>
<body>
<button class="style-button">鼠标悬停看效果</button>
</body>
</html>
CSS 按钮波纹效果
这里是如何向按钮添加波纹效果的示例单击时 -
<html>
<head>
<style>
.style-button {
display: inline-block;
padding: 15px;
position: relative;
background-color: violet;
border: none;
border-radius: 5px;
overflow: hidden;
cursor: pointer;
}
.style-button:after {
content: "";
background: pink;
display: block;
position: absolute;
padding-top: 200%;
padding-left: 250%;
margin-left: -15px !important;
margin-top: -100%;
opacity: 0;
transition: transform 0.3s, opacity 0.3s;
}
.style-button:active:after {
padding: 0;
margin: 0;
opacity: 1;
transition: 0s;
}
</style>
</head>
<body>
<button class="style-button">点击</button>
</body>
</html>