为链接添加 .stretched-link,即可通过 ::after 伪元素使其包含块变得可点击。在大多数情况下,这意味着包含带有 .stretched-link 类链接的 position: relative; 元素是可点击的。请注意,鉴于 CSS position 的工作方式,.stretched-link 不能与大多数表格元素混用。
在 Bootstrap 中,卡片默认具有 position: relative,因此在这种情况下,你可以安全地为卡片中的链接添加 .stretched-link 类,而无需其他 HTML 更改。
不建议在拉伸链接中使用多个链接和点击目标。不过,如果确实需要,一些 position 和 z-index 样式可以提供帮助。
Card with stretched link
Some quick example text to build on the card title and make up the bulk of the card’s content.
Go somewhere<div class="card" style="width: 18rem;">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Card with stretched link</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card’s content.</p>
<a href="#" class="btn-solid theme-primary stretched-link">Go somewhere</a>
</div>
</div>大多数自定义组件默认没有 position: relative,因此我们需要在此处添加 .position-relative,以防止链接拉伸到父元素之外。
Custom component with stretched link
This is some placeholder content for the custom component. It is intended to mimic what some real-world content would look like, and we’re using it here to give the component a bit of body and size.
Go somewhere<div class="d-flex position-relative">
<img src="..." class="flex-shrink-0 me-3" alt="...">
<div>
<h5 class="mt-0">Custom component with stretched link</h5>
<p>This is some placeholder content for the custom component. It is intended to mimic what some real-world content would look like, and we’re using it here to give the component a bit of body and size.</p>
<a href="#" class="stretched-link">Go somewhere</a>
</div>
</div>Columns with stretched link
Another instance of placeholder content for this other custom component. It is intended to mimic what some real-world content would look like, and we’re using it here to give the component a bit of body and size.
Go somewhere<div class="row g-0 bg-1 position-relative">
<div class="md:col-6 md:mb-0 md:p-4">
<img src="..." class="w-100" alt="...">
</div>
<div class="md:col-6 p-4 md:ps-0">
<h5 class="mt-0">Columns with stretched link</h5>
<p>Another instance of placeholder content for this other custom component. It is intended to mimic what some real-world content would look like, and we’re using it here to give the component a bit of body and size.</p>
<a href="#" class="stretched-link">Go somewhere</a>
</div>
</div>识别包含块
如果拉伸链接似乎不起作用,那么包含块很可能是原因所在。以下 CSS 属性会使元素成为包含块:
position值不是statictransform或perspective值不是nonewill-change值为transform或perspectivefilter值不是none,或will-change值为filter(仅在 Firefox 中有效)
Card with stretched links
Some quick example text to build on the card title and make up the bulk of the card’s content.
Stretched link will not work here, because position: relative is added to the link
这个拉伸链接只会覆盖在 p 标签上,因为它应用了 transform。
<div class="card" style="width: 18rem;">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Card with stretched links</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card’s content.</p>
<p class="card-text">
<a href="#" class="stretched-link fg-danger" style="position: relative;">Stretched link will not work here, because <code>position: relative</code> is added to the link</a>
</p>
<p class="card-text bg-1" style="transform: rotate(0);">
这个<a href="#" class="fg-warning stretched-link">拉伸链接</a>只会覆盖在 <code>p</code> 标签上,因为它应用了 transform。
</p>
</div>
</div>CSS
变量
Stretched links use local CSS variables on .stretched-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
$stretched-link-tokens: defaults(
(
--stretched-link-z-index: 1,
),
$stretched-link-tokens
);
Sass 变量
用于拉伸链接的伪元素默认为 after,可以通过 $stretched-link-pseudo-element Sass 变量进行更改。这不能是 CSS 变量,因为它会直接编译到生成的选择器中。
$stretched-link-pseudo-element: after;