按钮

通过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;
}
Code language: CSS (css)
<button>默认按钮</button>
<a href="#" class="button">链接按钮</a>
<button class="button">按钮</button>
<input type="button" class="button" value="输入框按钮">
Code language: HTML, XML (xml)

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

.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 */
Code language: CSS (css)

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

.button1 {font-size: 10px;}
.button2 {font-size: 12px;}
.button3 {font-size: 16px;}
.button4 {font-size: 20px;}
.button5 {font-size: 24px;}
Code language: CSS (css)

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

.button1 {border-radius: 2px;}
.button2 {border-radius: 4px;}
.button3 {border-radius: 8px;}
.button4 {border-radius: 12px;}
.button5 {border-radius: 50%;}
Code language: CSS (css)

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

.button1 {
    background-color: white;
    color: black;
    border: 2px solid #4CAF50; /* Green */
}
Code language: CSS (css)

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

.button {
    -webkit-transition-duration: 0.4s; /* Safari */
    transition-duration: 0.4s;
}
.button:hover {
    background-color: #4CAF50; /* Green */
    color: white;
}
Code language: CSS (css)

我们可以使用 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);
}
Code language: CSS (css)

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

.disabled {
    opacity: 0.6;
    cursor: not-allowed;
}
Code language: CSS (css)

按钮宽度

.button1 {width: 250px;}
.button2 {width: 50%;}
.button3 {width: 100%;}
Code language: CSS (css)

Previous:
Next:

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注