伸長されたリンク
CSSを介してネストされたリンクを「伸張」することで、任意のHTML要素またはBootstrapコンポーネントをクリック可能にします。
.stretched-link
をリンクに追加して、::after
擬似要素を介してcontaining block
をクリック可能にします。ほとんどの場合、これはposition: relative;
を持つ要素が.stretched-link
クラスを持つリンクを含んでいることを意味します。クリック可能です。CSSposition
の仕組みを考慮すると、.stretched-link
はほとんどのテーブル要素と混在できないことに注意してください。
カードはデフォルトでBootstrapでposition: relative
を備えているので、この場合は他のHTML変更を行わずに.stretched-link
クラスをカード内のリンクに安全に追加できます。
伸張されたリンクでは、複数のリンクとタップターゲットはお勧めしません。ただし、これが必要な場合は、いくつかのposition
とz-index
スタイルが役立ちます。
<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 btn-primary stretched-link">Go somewhere</a>
</div>
</div>
メディアオブジェクトはデフォルトでposition: relative
を持っていないので、リンクがメディアオブジェクトの外側に広がるのを防ぐには、ここに.position-relative
を追加する必要があります。
伸張リンク付きメディア
これはメディアオブジェクトのプレースホルダーコンテンツです。実際のコンテンツがどのように見えるかを模倣することを目的としており、ここではコンポーネントに少し本体とサイズを与えるために使用しています。
どこかへ<div class="media position-relative">
<img src="..." class="mr-3" alt="...">
<div class="media-body">
<h5 class="mt-0">Media with stretched link</h5>
<p>This is some placeholder content for the media object. 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>
列はデフォルトでposition: relative
なので、クリック可能な列にはリンクの.stretched-link
クラスのみが必要です。ただし、.row
全体にリンクを伸張するには、列に.position-static
、行に.position-relative
が必要です。
伸張リンク付き列
この他のカスタムコンポーネントのプレースホルダーコンテンツの別のインスタンス。実際のコンテンツがどのように見えるかを模倣することを目的としており、ここではコンポーネントに少し本体とサイズを与えるために使用しています。
どこかへ<div class="row no-gutters bg-light position-relative">
<div class="col-md-6 mb-md-0 p-md-4">
<img src="..." class="w-100" alt="...">
</div>
<div class="col-md-6 position-static p-4 pl-md-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プロパティは、要素をコンテナーブロックにします。
static
以外のposition
値none
以外のtransform
またはperspective
値transform
またはperspective
のwill-change
値none
以外のfilter
値またはfilter
のwill-change
値(Firefoxでのみ動作します)
伸張リンク付きカード
カードのタイトルに基づいて短く例示するテキストを作成し、カードの大部分のコンテンツを作成します。
position: relative
がリンクに追加されているため、この場合はストレッチされたリンクは機能しません。
このストレッチされたリンクは、変換が適用されているため、p
タグにのみ広げられます。
<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 text-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-light" style="transform: rotate(0);">
This <a href="#" class="text-warning stretched-link">stretched link</a> will only be spread over the <code>p</code>-tag, because a transform is applied to it.
</p>
</div>
</div>