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

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

为按钮设置样式

按钮使用 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 创建图标按钮的代码 −

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 { 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+ 浏览

开启您的职业生涯

完成课程以获得认证

开始吧
广告