5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CSSで複数行下線が正しく表示されないとき

Posted at

CSSで複数行の下線を引く場合、以下のサイトを参考に実装してみました。

やり方としては下線を引きたい箇所に以下のクラスを付与したspanタグなどで囲うことで実現できます。

.under_line {
  background: linear-gradient(transparent 70%, #eb4034 0%);
  display: inline;
}

しかし、どうしたことかうまく適応されませんでした。

親要素のdisplayに注目

親要素のdisplayがflexだった場合、このCSSがうまく適用されないことがわかりました。
ですので、例えば以下のようなHTMLの場合

<h1>
  <span class="under_line">
    複数行になる文章
  </span>
</h1>

h1display: flex;が適用されていると複数行に渡った下線になりません。

<h1>
  <div class="headline-inner">
    <span class="under_line">
      複数行になる文章
    </span>
  </div>
</h1>

この様にheadline-innerというクラスを付与した要素をはさみ、これをdisplay: block;にすることで正しく下線が引かれます。
セマンティックかどうか微妙なので、h1pなどの要素にdisplay: flex;を適用せずに、親要素を作ってそちらにflexをつけたほうが良いかなと思います。

DEMO
https://jsfiddle.net/kubotak/42L1y9mr/1/

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?