如何使用 CSS 建立圖示按鈕?


要使用 CSS 建立圖示按鈕,你需要在網頁上設定圖示。此處,我們將考慮 Font Awesome 圖示。要在按鈕上設定此類圖示,請在 <button> 元素下設定圖示的 CDN。

設定圖示 CDN

為了在我們的網頁上新增圖示,我們使用了 Font Awesome 圖示。使用 <link> 元素在網頁上包含它 −

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

建立圖示按鈕

在 <button> 元素下,設定 <i>。Font Awesome 圖示在 <i> 中設定 −

<button><i class="fa fa-home"></i>Home</button>
<button><i class="fa fa-phone-square" aria-hidden="true"></i>Call Us</button>
<button><i class="fa fa-map-marker" aria-hidden="true"></i>Visit Us</button>
<button><i class="fa fa-cog" aria-hidden="true"></i>Settings</button>
<button><i class="fa fa-user-o" aria-hidden="true"></i>Login</button>

為按鈕設定樣式

按鈕使用 cursor 屬性和值 pointer 設定,使其看起來像一個可點選按鈕 −

button {
   font-family: "Lucida Sans", "Lucida Sans Regular", "Lucida Grande",
   "Lucida Sans Unicode", Geneva, Verdana, sans-serif;
   background-color: rgb(30, 173, 255);
   border: none;
   color: white;
   padding: 12px 16px;
   font-size: 32px;
   cursor: pointer;
}

為 <i> 設定樣式

圖示在 <i> 中設定。因此,要正確地將其與文字對齊,使用填充屬性填充 −

i {
   padding: 15px;
   color: rgb(33, 0, 109);
}

示例

以下是使用 CSS 建立圖示按鈕的程式碼 −

<!DOCTYPE html>
<html>
<head>
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
   <style>
      button {
         font-family: "Lucida Sans", "Lucida Sans Regular", "Lucida Grande",
         "Lucida Sans Unicode", Geneva, Verdana, sans-serif;
         background-color: rgb(30, 173, 255);
         border: none;
         color: white;
         padding: 12px 16px;
         font-size: 32px;
         cursor: pointer;
      }
      i {
         padding: 15px;
         color: rgb(33, 0, 109);
      }
      button:hover {
         background-color: rgb(81, 44, 148);
      }
      button:hover i {
         color: white;
      }
   </style>
</head>
<body>
   <h1 style="font-size: 60px; font-family: Arial, Helvetica, sans-serif;">Icon Buttons Example</h1>
   <button><i class="fa fa-home"></i>Home</button>
   <button> <i class="fa fa-phone-square" aria-hidden="true"></i>Call Us </button>
   <button><i class="fa fa-map-marker" aria-hidden="true"></i>Visit Us</button>
   <button><i class="fa fa-cog" aria-hidden="true"></i>Settings</button>
   <button><i class="fa fa-user-o" aria-hidden="true"></i>Login</button>
</body>
</html>

更新日期: 14-12-2023

1K+ 瀏覽

開啟您的職業生涯

完成課程以獲得認證

開始吧
廣告