如何使用 CSS 樣式化下載按鈕?


下載按鈕上的下載圖示在按鈕樣式化中起著關鍵作用。這使使用者在瀏覽網站時能夠理解這是一個下載按鈕,並且點選後文件將被下載。可以使用 Font Awesome 在網頁上包含這些圖示。為此,首先需要在 <link> 元素中設定 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> 元素在網頁上建立按鈕 -

<button><i class="fa fa-download"></i> DOWNLOAD NOW</button>

放置圖示

透過將 <i> 放置在 <button> 元素內,將圖示放置在按鈕上 -

<i class="fa fa-download">

樣式化下載按鈕

以下是使用 CSS 樣式化下載按鈕的程式碼。將游標屬性設定為指標屬性,使其看起來像連結 -

button{
   background-color: rgb(78, 15, 129);
   border: none;
   color: white;
   margin-left:33%;
   padding: 12px 30px;
   cursor: pointer;
   font-size: 30px;
   font-weight: bolder;
   font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

示例

讓我們看看示例 -

<!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{
         background-color: rgb(78, 15, 129);
         border: none;
         color: white;
         margin-left:33%;
         padding: 12px 30px;
         cursor: pointer;
         font-size: 30px;
         font-weight: bolder;
         font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      }
      button:hover{
         background-color: rgb(42, 7, 82);
      }
   </style>
</head>
<body>
   <h1>Download Button Styling Example</h1>
   <button><i class="fa fa-download"></i> DOWNLOAD NOW</button>
</body>
</html>

樣式化全寬下載按鈕

在這個示例中,下載按鈕使用 width 屬性設定為 100% 的全寬。使用 text-align 屬性設定為 center 將按鈕上的文字設定在中心 -

button{
   background-color: rgb(78, 15, 129);
   border: none;
   color: white;
   text-align: center; 
   width: 100%;
   padding: 12px 30px;
   cursor: pointer;
   font-size: 30px;
   font-weight: bolder;
   font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

示例

讓我們看看一個示例 -

<!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{
         background-color: rgb(78, 15, 129);
         border: none;
         color: white;
         text-align: center; 
         width: 100%;
         padding: 12px 30px;
         cursor: pointer;
         font-size: 30px;
         font-weight: bolder;
         font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      }
      button:hover{
         background-color: rgb(42, 7, 82);
      }
   </style>
</head>
<body>
   <h1>Download Button Styling Example</h1>
   <button><i class="fa fa-download"></i> DOWNLOAD NOW</button>
</body>
</html>

更新於: 2023-12-21

603 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.