图标链接辅助类组件会修改我们默认的链接样式,以增强其外观,并快速对齐任意图标与文本的组合。对齐通过内联 Flex 布局和默认的 gap 值实现。我们使用自定义偏移和颜色来设计下划线。图标会自动调整为 1em 大小,以最佳匹配其关联文本的 font-size。
图标链接假定使用 Bootstrap Icons,但你也可以使用任何喜欢的图标或图像。
当图标纯粹是装饰性的时候,应使用 aria-hidden="true" 将其从辅助技术中隐藏,正如我们在示例中所做的那样。对于传达含义的图标,请通过为 SVG 添加 role="img" 和适当的 aria-label="..." 来提供合适的文本替代。
示例
取一个普通的 <a> 元素,添加 .icon-link,然后在链接文本的左侧或右侧插入图标。图标会自动调整大小、放置位置和着色。
<a class="icon-link" href="#">
<svg xmlns="http://www.w3.org/2000/svg" class="bi" viewBox="0 0 16 16" aria-hidden="true">
<path d="M8.186 1.113a.5.5 0 0 0-.372 0L1.846 3.5l2.404.961L10.404 2l-2.218-.887zm3.564 1.426L5.596 5 8 5.961 14.154 3.5l-2.404-.961zm3.25 1.7-6.5 2.6v7.922l6.5-2.6V4.24zM7.5 14.762V6.838L1 4.239v7.923l6.5 2.6zM7.443.184a1.5 1.5 0 0 1 1.114 0l7.129 2.852A.5.5 0 0 1 16 3.5v8.662a1 1 0 0 1-.629.928l-7.185 2.874a.5.5 0 0 1-.372 0L.63 13.09a1 1 0 0 1-.63-.928V3.5a.5.5 0 0 1 .314-.464L7.443.184z"/>
</svg>
Icon link
</a><a class="icon-link" href="#">
Icon link
<svg xmlns="http://www.w3.org/2000/svg" class="bi" viewBox="0 0 16 16" aria-hidden="true">
<path d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8z"/>
</svg>
</a>悬停样式
添加 .icon-link-hover 可在悬停时将图标向右移动。
<a class="icon-link icon-link-hover" href="#">
Icon link
<svg xmlns="http://www.w3.org/2000/svg" class="bi" viewBox="0 0 16 16" aria-hidden="true">
<path d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8z"/>
</svg>
</a>CSS
变量
Icon links use local CSS variables on .icon-link for real-time customization. Values for the CSS variables are generated from Sass maps unique to each component and applied to the aforementioned class.
// stylelint-disable-next-line scss/dollar-variable-default
$icon-link-tokens: defaults(
(
--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),
),
$icon-link-tokens
);
通过覆盖 --bs-icon-link-transform CSS 变量来自定义悬停时的 transform:
<a class="icon-link icon-link-hover" style="--bs-icon-link-transform: translate3d(0, -.125rem, 0);" href="#">
<svg xmlns="http://www.w3.org/2000/svg" class="bi" viewBox="0 0 16 16" aria-hidden="true">
<path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"/>
<path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"/>
</svg>
Icon link
</a>图标链接的下划线颜色源自 --bs-link-color,因此覆盖它会同时更新文本和下划线:
<a class="icon-link icon-link-hover" style="--bs-link-color: #198754;" href="#">
Icon link
<svg xmlns="http://www.w3.org/2000/svg" class="bi" viewBox="0 0 16 16" aria-hidden="true">
<path d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8z"/>
</svg>
</a>Sass 工具类 API
使用我们的链接工具类中的任意一个来修改图标链接,以调整下划线颜色和偏移。
<a class="icon-link icon-link-hover theme-success underline-success underline-opacity-30 hover:underline-opacity-80 underline-offset-3" href="#">
Icon link
<svg xmlns="http://www.w3.org/2000/svg" class="bi" viewBox="0 0 16 16" aria-hidden="true">
<path d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8z"/>
</svg>
</a>