CSS 按鈕
CSS 按鈕
本章節我們為大家介紹使用 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: #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 */
.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;}
.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%;}
.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 */
}
...
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;
}
...
-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);
}
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" 來設置一個禁用的圖片:
按鈕寬度
默認情況下,按鈕的大小有按鈕上的文本內容決定( 根據文本內容匹配長度 )。 我們可以使用 width
屬性來設置按鈕的寬度:
提示: 如果要設置固定寬度可以使用像素 (px) 為單位,如果要設置響應式的按鈕可以設置為百分比。
按鈕組
移除外邊距併添加 float:left
來設置按鈕組:
帶邊框按鈕組
我們可以使用 border
屬性來設置帶邊框的按鈕組: