2025.05.08

HTML・CSS

text-decorationを利用した文字の下線

text-decorationプロパティを利用して複数行の文字に下線を引きます。複数行に対応させるため、inline要素に適用します。
デモページはこちら

以下のhtml構造で確認します。参考までにbackground-imageプロパティのlinear-gradientを利用した下線も表示します。

<p>linear-gradient</p>
<div class="div-parent">
  <span class="span-child-01">linear-gradientを利用した文字の下線<br>inline要素に適用して複数行に対応</span>
</div>
<p>linear-gradient ※下線位置を2px上に調整</p>
<div class="div-parent">
  <span class="span-child-02">linear-gradientを利用した文字の下線<br>inline要素に適用して複数行に対応</span>
</div>
<p>text-decoration</p>
<div class="div-parent">
  <span class="span-child-03">text-decorationを利用した文字の下線<br>inline要素に適用して複数行に対応</span>
</div>
<p>text-decoration ※下線位置を0.2em上に調整</p>
<div class="div-parent">
  <span class="span-child-04">text-decorationを利用した文字の下線<br>inline要素に適用して複数行に対応</span>
</div>

CSSは以下になります。text-decoration-thicknessは下線の太さ、text-decoration-colorは下線の色、text-underline-offsetは下線の位置、text-decoration-skip-inkは下線と文字の重なり方です。

.div-parent {
  line-height: 1.5;
  min-height: 100px;
  margin: 0 0 1em;
  background-color: #fff;
}

.span-child-01 {
  background: linear-gradient(transparent 60%, #eae5d6 60%);
}
.span-child-02 {
  background: linear-gradient(transparent 60%, #eae5d6 60%) left bottom 2px;
}
.span-child-03 {
  text-decoration: underline;
  text-decoration-thickness: 0.5em;
  text-decoration-color: #eae5d6;
  text-decoration-skip-ink: none;
}
.span-child-04 {
  text-decoration: underline;
  text-decoration-thickness: 0.5em;
  text-decoration-color: #eae5d6;
  text-underline-offset: -0.2em;
  text-decoration-skip-ink: none;
}

text-decoration-colorは透過度を設定しなくても下線は文字の背面に重なります。text-underline-offsetを設定しないと下線と文字が重なりません。text-decoration-skip-inkの初期値はautoで、下線と文字が重なると下線が途切れてしまいます。noneを指定すれば下線が省略されません。

関連記事

2024.09.02

HTML・CSS

box-shadowとfilter: drop-shadowの違い

box-shadowプロパティとfilterプロパティのdrop-shadowを比較して違いを確認します。デモページはこちら filter: drop-shad…

2020.07.18

HTML・CSS

CSSアニメーションで画像を横スクロール無限ループ

CSSアニメーションで画像を横スクロール無限ループさせます。まずhtmlのコードです。同じ画像を2つ配置して、クラス名loop_wrapのdivタグで囲います。…

2024.02.19

HTML・CSS

Photoshopのグラデーションオーバーレイをlinear-gradientで適用

Photoshopのグラデーションオーバーレイを、background-imageプロパティのlinear-gradientで適用します。グラデーションの角度は…

2022.05.14

HTML・CSS

positionプロパティのstickyの使い方

positionプロパティのstickyの使い方と注意点です。divタグに以下のCSSを適用して動作を確認します。 デモページはこちら positionプロパテ…

2021.07.06

HTML・CSS

Photoshopのドロップシャドウをbox-shadowプロパティで適用

Photoshopのレイヤースタイルで作成したドロップシャドウを、box-shadowプロパティで適用します。デモページはこちら box-shadowプロパティ…

上に戻る