如何使用 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>

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

放置图标

通过将 <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; }

示例

让我们看看示例 -

Open Compiler
<!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; }

示例

让我们看看一个示例 -

Open Compiler
<!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 次浏览

开启您的 职业生涯

通过完成课程获得认证

立即开始
广告