小码哥的IT人生

CSS 按钮 详解

css3基础 2022-05-23 12:08:06小码哥的IT人生shichen

CSS 按钮

学习如何使用 CSS 设置按钮样式。

基本按钮样式

示例代码:

.button {
  background-color: #4CAF50; /* Green */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #4CAF50;
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}
</style>
</head>
<body>
<h1>CSS 按钮</h1>
<button>默认按钮</button>
<a href="#" class="button">链接按钮</a>
<button class="button">按钮</button>
<input type="button" class="button" value="输入按钮">
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

按钮颜色

请使用 background-color 属性更改按钮的背景色:

示例代码:

.button1 {background-color: #4CAF50;} /* 绿色 */
.button2 {background-color: #008CBA;} /* 蓝色 */
.button3 {background-color: #f44336;} /* 红色 */
.button4 {background-color: #e7e7e7; color: black;} /* 灰色 */
.button5 {background-color: #555555;} /* 黑色 */

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #4CAF50; /* 绿色 */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}
.button2 {background-color: #008CBA;} /* 蓝色 */
.button3 {background-color: #f44336;} /* 黑色 */
.button4 {background-color: #e7e7e7; color: black;} /* 灰色 */
.button5 {background-color: #555555;} /* 黑色 */
</style>
</head>
<body>
<h1>按钮颜色</h1>
<p>通过 background-color 属性改变按钮的背景色:</p>
<button class="button">绿色</button>
<button class="button button2">蓝色</button>
<button class="button button3">红色</button>
<button class="button button4">灰色</button>
<button class="button button5">黑色</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

按钮尺寸

请使用 font-size 属性更改按钮的字体大小:

示例代码:

.button1 {font-size: 10px;}
.button2 {font-size: 12px;}
.button3 {font-size: 16px;}
.button4 {font-size: 20px;}
.button5 {font-size: 24px;}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #4CAF50; /* 绿色 */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  margin: 4px 2px;
  cursor: pointer;
}
.button1 {font-size: 10px;}
.button2 {font-size: 12px;}
.button3 {font-size: 16px;}
.button4 {font-size: 20px;}
.button5 {font-size: 24px;}
</style>
</head>
<body>
<h1>按钮大小</h1>
<p>通过 font-size 属性改变按钮的字体大小:</p>
<button class="button button1">10px</button>
<button class="button button2">12px</button>
<button class="button button3">16px</button>
<button class="button button4">20px</button>
<button class="button button5">24px</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

请使用 padding 属性更改按钮的内边距:

示例代码:

.button1 {padding: 10px 24px;}
.button2 {padding: 12px 28px;}
.button3 {padding: 14px 40px;}
.button4 {padding: 32px 16px;}
.button5 {padding: 16px;}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #4CAF50; /* 绿色 */
  border: none;
  color: white;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}
.button1 {padding: 10px 24px;}
.button2 {padding: 12px 28px;}
.button3 {padding: 14px 40px;}
.button4 {padding: 32px 16px;}
.button5 {padding: 16px;}
</style>
</head>
<body>
<h1>按钮大小</h1>
<p>通过 padding 属性改变按钮的内边距:</p>
<button class="button button1">10px 24px</button>
<button class="button button2">12px 28px</button>
<button class="button button3">14px 40px</button>
<button class="button button4">32px 16px</button>
<button class="button button5">16px</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

圆角按钮

请使用 border-radius 属性为按钮添加圆角:

示例代码:

.button1 {border-radius: 2px;}
.button2 {border-radius: 4px;}
.button3 {border-radius: 8px;}
.button4 {border-radius: 12px;}
.button5 {border-radius: 50%;}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #4CAF50; /* 绿色 */
  border: none;
  color: white;
  padding: 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}
.button1 {border-radius: 2px;}
.button2 {border-radius: 4px;}
.button3 {border-radius: 8px;}
.button4 {border-radius: 12px;}
.button5 {border-radius: 50%;}
</style>
</head>
<body>
<h1>圆角按钮</h1>
<p>通过 border-radius 属性为按钮添加圆角:</p>
<button class="button button1">2px</button>
<button class="button button2">4px</button>
<button class="button button3">8px</button>
<button class="button button4">12px</button>
<button class="button button5">50%</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

彩色的按钮边框

请使用 border 属性为按钮添加彩色边框:

示例代码:

.button1 {
  background-color: white;
  color: black;
  border: 2px solid #4CAF50; /* 绿色 */
}
...

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #4CAF50; /* 绿色 */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}
.button1 {
  background-color: white;
  color: black;
  border: 2px solid #4CAF50;
}
.button2 {
  background-color: white;
  color: black;
  border: 2px solid #008CBA;
}
.button3 {
  background-color: white;
  color: black;
  border: 2px solid #f44336;
}
.button4 {
  background-color: white;
  color: black;
  border: 2px solid #e7e7e7;
}
.button5 {
  background-color: white;
  color: black;
  border: 2px solid #555555;
}
</style>
</head>
<body>
<h1>有颜色的按钮边框</h1>
<p>请使用 border 属性为按钮添加边框:</p>
<button class="button button1">绿色</button>
<button class="button button2">蓝色</button>
<button class="button button3">黑色</button>
<button class="button button4">灰色</button>
<button class="button button5">黑色</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

可悬停按钮


当鼠标移动到按钮上方时,使用 :hover 选择器可更改按钮的样式。

提示:请使用 transition-duration 属性来确定“悬停”效果的速度:

示例代码:

.button {
  transition-duration: 0.4s;
}
.button:hover {
  background-color: #4CAF50; /* Green */
  color: white;
}
...

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #4CAF50; /* Green */
  border: none;
  color: white;
  padding: 16px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  transition-duration: 0.4s;
  cursor: pointer;
}
.button1 {
  background-color: white;
  color: black;
  border: 2px solid #4CAF50;
}
.button1:hover {
  background-color: #4CAF50;
  color: white;
}
.button2 {
  background-color: white;
  color: black;
  border: 2px solid #008CBA;
}
.button2:hover {
  background-color: #008CBA;
  color: white;
}
.button3 {
  background-color: white;
  color: black;
  border: 2px solid #f44336;
}
.button3:hover {
  background-color: #f44336;
  color: white;
}
.button4 {
  background-color: white;
  color: black;
  border: 2px solid #e7e7e7;
}
.button4:hover {background-color: #e7e7e7;}
.button5 {
  background-color: white;
  color: black;
  border: 2px solid #555555;
}
.button5:hover {
  background-color: #555555;
  color: white;
}
</style>
</head>
<body>
<h1>可悬停的按钮</h1>
<p>使用 :hover 选择器在鼠标移动到按钮上时改变其样式。</p>
<p><b>提示:</b>请使用 transition-duration 属性来确定悬停效果的速度:</p>
<button class="button button1">绿色</button>
<button class="button button2">蓝色</button>
<button class="button button3">红色</button>
<button class="button button4">灰色</button>
<button class="button button5">黑色</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

阴影按钮

请使用 box-shadow 属性为按钮添加阴影:

示例代码:

.button1 {
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
.button2:hover {
  box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #4CAF50; /* Green */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  -webkit-transition-duration: 0.4s; /* Safari */
  transition-duration: 0.4s;
}
.button1 {
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
.button2:hover {
  box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24),0 17px 50px 0 rgba(0,0,0,0.19);
}
</style>
</head>
<body>
<h1>阴影按钮</h1>
<p>使用 box-shadow 属性为按钮添加阴影:</p>
<button class="button button1">阴影按钮</button>
<button class="button button2">悬停时的阴影</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

禁用的按钮

请使用 opacity 属性为按钮添加透明度(创建“禁用”外观)。

提示:您还可以添加带有 "not-allowed" 值的 cursor 属性,当您将鼠标悬停在按钮上时,该属性会显示 "no parking sign"(禁停标志):

示例代码:

.disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #4CAF50; /* 绿色 */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}
.disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>
</head>
<body>
<h1>被禁用的按钮</h1>
<p>使用 opacity 属性向按钮添加一定的透明度(使它看上去已被禁用)</p>
<button class="button">正常按钮</button>
<button class="button disabled">被禁用的按钮</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

按钮宽度


默认情况下,按钮的大小取决于其文本内容(与内容的宽度一样)。请使用 width 属性来更改按钮的宽度:

示例代码:

.button1 {width: 250px;}
.button2 {width: 50%;}
.button3 {width: 100%;}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #4CAF50; /* Green */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}
.button1 {width: 250px;}
.button2 {width: 50%;}
.button3 {width: 100%;}
</style>
</head>
<body>
<h1>按钮宽度</h1>
<p>使用 width 属性来改变按钮的宽度:</p>
<p><b>提示:</b>请使用像素设置固定宽度,并为响应式按钮使用百分百(例如其父元素的 50%)。请调整窗口大小来查看效果。</p>
<button class="button button1">250px</button><br>
<button class="button button2">50%</button><br>
<button class="button button3">100%</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

按钮分组

 

删除外边距并向每个按钮添加 float:left,来创建按钮组:

示例代码:

.button {
  float: left;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.btn-group .button {
  background-color: #4CAF50; /* 绿色 */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  cursor: pointer;
  float: left;
}
.btn-group .button:hover {
  background-color: #3e8e41;
}
</style>
</head>
<body>
<h1>按钮组</h1>
<p>删除外边距并浮动按钮,来创建一个按钮组:</p>
<div class="btn-group">
  <button class="button">Button</button>
  <button class="button">Button</button>
  <button class="button">Button</button>
  <button class="button">Button</button>
</div>
<p style="clear:both"><br>请记得之后清除浮动,否则这个 p 元素会向按钮浮动。</p>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

带边框的按钮组

 

使用 border 属性来创建带边框的按钮组:

示例代码:

.button {
  float: left;
  border: 1px solid green;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.btn-group .button {
  background-color: #4CAF50; /* 绿色 */
  border: 1px solid green;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  cursor: pointer;
  float: left;
}
.btn-group .button:not(:last-child) {
  border-right: none; /* 阻止双边框 */
}
.btn-group .button:hover {
  background-color: #3e8e41;
}
</style>
</head>
<body>
<h1>带边框的按钮组</h1>
<p>添加边框,来创建带按钮的按钮组:</p>
<div class="btn-group">
  <button class="button">Button</button>
  <button class="button">Button</button>
  <button class="button">Button</button>
  <button class="button">Button</button>
</div>
<p style="clear:both"><br>请记得之后清除浮动,否则这个 p 元素会向按钮浮动。</p>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

垂直按钮组

使用 display:block 取代 float:left 将按钮上下分组,而不是并排:

示例代码:

.button {
  display: block;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.btn-group .button {
  background-color: #4CAF50; /* 绿色 */
  border: 1px solid green;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  font-size: 16px;
  cursor: pointer;
  width: 150px;
  display: block;
}
.btn-group .button:not(:last-child) {
  border-bottom: none; /* 阻止双边框 */
}
.btn-group .button:hover {
  background-color: #3e8e41;
}
</style>
</head>
<body>
<h1>垂直按钮分组</h1>
<div class="btn-group">
  <button class="button">Button</button>
  <button class="button">Button</button>
  <button class="button">Button</button>
  <button class="button">Button</button>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

图像上的按钮

Coffee

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.container {
  position: relative;
  width: 100%;
  max-width: 400px;
}
.container img {
  width: 100%;
  height: auto;
}
.container .btn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  background-color: #f1f1f1;
  color: black;
  font-size: 16px;
  padding: 16px 30px;
  border: none;
  cursor: pointer;
  border-radius: 5px;
  text-align: center;
}
.container .btn:hover {
  background-color: black;
  color: white;
}
</style>
</head>
<body>
<h1>图像上的按钮</h1>
<p>为图像添加按钮:</p>
<div class="container">
  <img src="img_lights.jpg" alt="Snow" style="width:100%">
  <button class="btn">按钮</button>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

动画按钮

实例 1

在鼠标悬停时添加箭头:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  display: inline-block;
  border-radius: 4px;
  background-color: #f4511e;
  border: none;
  color: #FFFFFF;
  text-align: center;
  font-size: 28px;
  padding: 20px;
  width: 200px;
  transition: all 0.5s;
  cursor: pointer;
  margin: 5px;
}
.button span {
  cursor: pointer;
  display: inline-block;
  position: relative;
  transition: 0.5s;
}
.button span:after {
  content: '\00bb';
  position: absolute;
  opacity: 0;
  top: 0;
  right: -20px;
  transition: 0.5s;
}
.button:hover span {
  padding-right: 25px;
}
.button:hover span:after {
  opacity: 1;
  right: 0;
}
</style>
</head>
<body>
<h1>带动画效果的按钮</h1>
<button class="button" style="vertical-align:middle"><span>请悬停在我上方</span></button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

实例 2

添加点击时的“按键按下”效果:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  display: inline-block;
  padding: 15px 25px;
  font-size: 24px;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  outline: none;
  color: #fff;
  background-color: #4CAF50;
  border: none;
  border-radius: 15px;
  box-shadow: 0 9px #999;
}
.button:hover {background-color: #3e8e41}
.button:active {
  background-color: #3e8e41;
  box-shadow: 0 5px #666;
  transform: translateY(4px);
}
</style>
</head>
<body>
<h1>带动画效果的按钮 - 按键效果</h1>
<button class="button">请点击我</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

实例 3

鼠标悬停时淡入:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.button {
  background-color: #f4511e;
  border: none;
  color: white;
  padding: 16px 32px;
  text-align: center;
  font-size: 16px;
  margin: 4px 2px;
  opacity: 0.6;
  transition: 0.3s;
  display: inline-block;
  text-decoration: none;
  cursor: pointer;
}
.button:hover {opacity: 1}
</style>
</head>
<body>
<h1>淡入按钮 - 淡入效果</h1>
<button class="button">请悬停在我上方</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

实例 4

添加点击时的“涟漪”效果:

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.button {
  position: relative;
  background-color: #4CAF50;
  border: none;
  font-size: 28px;
  color: #FFFFFF;
  padding: 20px;
  width: 200px;
  text-align: center;
  transition-duration: 0.4s;
  text-decoration: none;
  overflow: hidden;
  cursor: pointer;
}
.button:after {
  content: "";
  background: #f1f1f1;
  display: block;
  position: absolute;
  padding-top: 300%;
  padding-left: 350%;
  margin-left: -20px !important;
  margin-top: -120%;
  opacity: 0;
  transition: all 0.8s
}
.button:active:after {
  padding: 0;
  margin: 0;
  opacity: 1;
  transition: 0s
}
</style>
</head>
<body>
<h1>带动画效果的按钮 - 涟漪效果</h1>
<button class="button">请点击我</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024