CSS 按钮

CSS3 教程 · 2019-03-01 08:28:20

本章节我们为大家介绍使用 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;
}

按钮颜色

我们可以使用 background-color 属性来设置按钮颜色:

CSS 实例

.button1 {background-color: #4CAF50;} /* Green */
.button2 {background-color: #008CBA;} /* Blue */
.button3 {background-color: #f44336;} /* Red */
.button4 {background-color: #e7e7e7; color: black;} /* Gray */
.button5 {background-color: #555555;} /* Black */

按钮大小

我们可以使用 font-size 属性来设置按钮大小:

CSS 实例

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

圆角按钮

我们可以使用 border-radius 属性来设置圆角按钮:

CSS 实例

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

按钮边框颜色

我们可以使用 border 属性设置按钮边框颜色:

CSS 实例

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

鼠标悬停按钮


我们可以使用 :hover 选择器来修改鼠标悬停在按钮上的样式。

提示: 我们可以使用 transition-duration 属性来设置 "hover" 效果的速度:

CSS 实例

.button {
    -webkit-transition-duration: 0.4s; /* Safari */
    transition-duration: 0.4s;
}

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

按钮阴影

我们可以使用 box-shadow 属性来为按钮添加阴影:

CSS 实例

.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);
}

禁用按钮

我们可以使用 opacity 属性为按钮添加透明度 (看起来类似 "disabled" 属性效果)。

提示: 我们可以添加 cursor 属性并设置为 "not-allowed" 来设置一个禁用的图片:

CSS 实例

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

按钮宽度


默认情况下,按钮的大小有按钮上的文本内容决定( 根据文本内容匹配长度 )。 我们可以使用 width 属性来设置按钮的宽度:

提示: 如果要设置固定宽度可以使用像素 (px) 为单位,如果要设置响应式的按钮可以设置为百分比。

CSS 实例

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

按钮组


移除外边距并添加 float:left 来设置按钮组:

CSS 实例

.button {
    float: left;
}

带边框按钮组


我们可以使用 border 属性来设置带边框的按钮组:

CSS 实例

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

按钮动画

CSS 实例

鼠标移动到按钮上后添加箭头标记:

CSS 实例

点击时添加 "波纹" 效果:

CSS 实例

点击时添加 "压下" 效果:

点击查看所有 CSS3 教程 文章: https://www.codercto.com/courses/l/24.html

查看所有标签

Flexible Rails

Flexible Rails

Peter Armstrong / Manning Publications / 2008-01-23 / USD 44.99

Rails is a fantastic tool for web application development, but its Ajax-driven interfaces stop short of the richness you gain with a tool like Adobe Flex. Simply put, Flex is the most productive way t......一起来看看 《Flexible Rails》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具