如何使用 CSS 為塊按鈕(全寬)設定樣式?
要在網頁上設定全寬按鈕,請將按鈕的寬度設定為 100%。使用 text-align 屬性進一步設定按鈕樣式,以使按鈕上的文字居中。使用 padding 屬性將文字正確放置在按鈕中。padding 屬性可同時在元素的四邊設定填充區域。讓我們看看如何使用 CSS 設定塊按鈕樣式,即全寬。
設定按鈕
使用 <button> 元素並在網頁上設定按鈕 −
<button class="blockBtn">Block Button</button>
按鈕寬度
使用寬度屬性並將其值設定為 100%,設定按鈕樣式並設定按鈕寬度。cursor 屬性設定為值指標,以使按鈕看起來可點選。由於按鈕擴充套件到全寬,因此應使用 text-align 屬性將其值設定為 center,將文字置於中央。display 屬性設定為 block 值 −
.blockBtn {
display: block;
width: 100%;
border: none;
background-color: rgb(19, 0, 105);
color: white;
padding: 14px 28px;
font-size: 36px;
cursor: pointer;
text-align: center;
font-weight: bold;
}
按鈕懸停
當滑鼠游標懸停在按鈕上時,背景和文字顏色將會更改。:hover 選擇器用於實現此目的 −
.blockBtn:hover {
background-color: rgb(132, 161, 255);
color: black;
}
網頁上的全寬按鈕
以下是使用 CSS 建立塊按鈕的程式碼 −
示例
<!DOCTYPE html>
<html>
<head>
<style>
.blockBtn {
display: block;
width: 100%;
border: none;
background-color: rgb(19, 0, 105);
color: white;
padding: 14px 28px;
font-size: 36px;
cursor: pointer;
text-align: center;
font-weight: bold;
}
.blockBtn:hover {
background-color: rgb(132, 161, 255);
color: black;
}
</style>
</head>
<body>
<h1 style="text-align: center;">Block Button Example</h1>
<button class="blockBtn">Block Button</button>
</body>
</html>
卡片上的全寬按鈕
此處建立產品卡片並在其上設定按鈕。由於寬度設定為 100% −,因此按鈕覆蓋了整個卡片
button {
width: 100%;
border: none;
outline: 0;
display: inline-block;
padding: 8px;
color: white;
background-color: rgb(23, 31, 102);
text-align: center;
cursor: pointer;
font-size: 18px;
}
示例
讓我們看下示例 −
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.productCard {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
max-width: 300px;
margin: auto;
text-align: center;
font-family: arial;
background-color: rgb(190, 224, 224);
}
.productDetails {
color: rgb(26, 0, 51);
font-weight: bold;
font-size: 18px;
}
button {
border: none;
outline: 0;
display: inline-block;
padding: 8px;
color: white;
background-color: rgb(23, 31, 102);
text-align: center;
cursor: pointer;
width: 100%;
font-size: 18px;
}
button:hover, a:hover {
opacity: 0.7;
}
</style>
</head>
<body>
<h2 style="text-align:center">Product Card Example</h2>
<div class="productCard">
<img src="https://images.pexels.com/photos/1152077/pexels-photo-1152077.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" style="width:100%"/>
<h1>Leather Bag</h1>
<p class="productDetails">Exotic Quality</p>
<p>Price 50$</p>
<p><button>Buy Now</button></p>
</div>
</body>
</html>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP