GitHubで表示

伸長されたリンク

CSSを介してネストされたリンクを「伸張」することで、任意のHTML要素またはBootstrapコンポーネントをクリック可能にします。

.stretched-linkをリンクに追加して、::after擬似要素を介してcontaining blockをクリック可能にします。ほとんどの場合、これはposition: relative;を持つ要素が.stretched-linkクラスを持つリンクを含んでいることを意味します。クリック可能です。CSSpositionの仕組みを考慮すると、.stretched-linkはほとんどのテーブル要素と混在できないことに注意してください。

カードはデフォルトでBootstrapでposition: relativeを備えているので、この場合は他のHTML変更を行わずに.stretched-linkクラスをカード内のリンクに安全に追加できます。

伸張されたリンクでは、複数のリンクとタップターゲットはお勧めしません。ただし、これが必要な場合は、いくつかのpositionz-indexスタイルが役立ちます。

Card image cap
伸張リンク付きカード

カードのタイトルに基づいて短く例示するテキストを作成し、カードの大部分のコンテンツを作成します。

どこかへ
<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を追加する必要があります。

Generic placeholder image
伸張リンク付きメディア

これはメディアオブジェクトのプレースホルダーコンテンツです。実際のコンテンツがどのように見えるかを模倣することを目的としており、ここではコンポーネントに少し本体とサイズを与えるために使用しています。

どこかへ
<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が必要です。

Generic placeholder image
伸張リンク付き列

この他のカスタムコンポーネントのプレースホルダーコンテンツの別のインスタンス。実際のコンテンツがどのように見えるかを模倣することを目的としており、ここではコンポーネントに少し本体とサイズを与えるために使用しています。

どこかへ
<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またはperspectivewill-change
  • none以外のfilter値またはfilterwill-change値(Firefoxでのみ動作します)
Card image cap
伸張リンク付きカード

カードのタイトルに基づいて短く例示するテキストを作成し、カードの大部分のコンテンツを作成します。

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>