Skip to main content Skip to docs navigation
Added in v5.3

图标链接

使用 Bootstrap 图标或其他图标快速创建风格化的超链接。

图标链接辅助程序组件修改了我们的默认链接样式,以增强其外观并快速对齐图标和文本的任何配对。对齐方式是通过内联 Flexbox 样式和默认 gap 值设置的。我们使用自定义偏移量和颜色对下划线进行样式化。图标的大小会自动调整为 1em,以最佳匹配其关联文本的 font-size

¥The icon link helper component modifies our default link styles to enhance their appearance and quickly align any pairing of icon and text. Alignment is set via inline flexbox styling and a default gap value. We stylize the underline with a custom offset and color. Icons are automatically sized to 1em to best match their associated text’s font-size.

图标链接假设正在使用 Bootstrap 图标,但你可以使用你喜欢的任何图标或图片。

¥Icon links assume Bootstrap Icons are being used, but you can use any icon or image you like.

When icons are purely decorative, they should be hidden from assistive technologies using aria-hidden="true", as we’ve done in our examples. For icons that convey meaning, provide an appropriate text alternative by adding role="img" and an appropriate aria-label="..." to the SVGs.

示例

¥Example

采用常规 <a> 元素,添加 .icon-link,然后在链接文本的左侧或右侧插入一个图标。图标会自动调整大小、位置和颜色。

¥Take a regular <a> element, add .icon-link, and insert an icon on either the left or right of your link text. The icon is automatically sized, placed, and colored.

html
<a class="icon-link" href="#">
  <svg class="bi" aria-hidden="true"><use xlink:href="#box-seam"></use></svg>
  Icon link
</a>
html
<a class="icon-link" href="#">
  Icon link
  <svg class="bi" aria-hidden="true"><use xlink:href="#arrow-right"></use></svg>
</a>

悬停时的样式

¥Style on hover

添加 .icon-link-hover 以在悬停时将图标移至右侧。

¥Add .icon-link-hover to move the icon to the right on hover.

html
<a class="icon-link icon-link-hover" href="#">
  Icon link
  <svg class="bi" aria-hidden="true"><use xlink:href="#arrow-right"></use></svg>
</a>

定制

¥Customize

使用我们的链接 CSS 变量、Sass 变量、工具或自定义样式修改图标链接的样式。

¥Modify the styling of an icon link with our link CSS variables, Sass variables, utilities, or custom styles.

CSS 变量

¥CSS variables

根据需要修改 --bs-link-*--bs-icon-link-* CSS 变量以更改默认外观。

¥Modify the --bs-link-* and --bs-icon-link-* CSS variables as needed to change the default appearance.

通过覆盖 --bs-icon-link-transform CSS 变量来自定义悬停 transform

¥Customize the hover transform by overriding the --bs-icon-link-transform CSS variable:

html
<a class="icon-link icon-link-hover" style="--bs-icon-link-transform: translate3d(0, -.125rem, 0);" href="#">
  <svg class="bi" aria-hidden="true"><use xlink:href="#clipboard"></use></svg>
  Icon link
</a>

通过覆盖 --bs-link-* CSS 变量来自定义颜色:

¥Customize the color by overriding the --bs-link-* CSS variable:

html
<a class="icon-link icon-link-hover" style="--bs-link-hover-color-rgb: 25, 135, 84;" href="#">
  Icon link
  <svg class="bi" aria-hidden="true"><use xlink:href="#arrow-right"></use></svg>
</a>

Sass 变量

¥Sass variables

自定义图标链接 Sass 变量以修改 Bootstrap 支持的项目中的所有图标链接样式。

¥Customize the icon link Sass variables to modify all icon link styles across your Bootstrap-powered project.

$icon-link-gap:               .375rem;
$icon-link-underline-offset:  .25em;
$icon-link-icon-size:         1em;
$icon-link-icon-transition:   .2s ease-in-out transform;
$icon-link-icon-transform:    translate3d(.25em, 0, 0);

Sass 工具 API

¥Sass utilities API

使用任何我们的链接实用程序 修改图标链接,以修改下划线颜色和偏移量。

¥Modify icon links with any of our link utilities for modifying underline color and offset.

html
<a class="icon-link icon-link-hover link-success link-underline-success link-underline-opacity-25" href="#">
  Icon link
  <svg class="bi" aria-hidden="true"><use xlink:href="#arrow-right"></use></svg>
</a>