<hr>区切り線スタイルガイド
HTML <hr>タグを、色や太さなどお好みのデザインにスタイル設定する方法について解説します。
CSSを適用すると、<hr>で表示される区切り線を、お好みのスタイルでデザインできます。
ブラウザで提供されるデフォルトスタイル
デザインの観点から見ると、ほとんどのWebサイトではデフォルトスタイルをそのまま使用せず、CSSで上書きしたりリセットしたりすることが一般的です。これは、ブラウザのデフォルトスタイルがWebサイトのデザイン要件と一致しない場合があるためです。
<hr>
カスタムスタイリングする
よく使われるさまざまなスタイルガイドを参考にしてみましょう。
実線パターン
最も直感的で広く使われている方法は、border: noneを指定し、heightで区切り線の太さを調整し、backgroundで色を設定する方法です。幅はwidthで調整できます。
<hr class="solid-hr">
hr.solid-hr {
border: none; /* デフォルトの3D立体ボーダーを削除 */
height: 2px; /* 線の太さ */
background: gray; /* 線の色 */
width: 50%; /* 線の幅(省略時は100%):自動で中央揃えになります */
}
活用例1
<hr class="solid-hr">
hr.solid-hr {
border: none;
height: 5px;
background: orangered;
width: 50%;
}
活用例2
<hr class="solid-hr">
hr.solid-hr {
border: none;
height: 5px;
background: linear-gradient(to right, orange, red);
}
点線と破線のパターン
この場合は、ボーダーが重ならないように片側のborder-topプロパティだけを指定し、点線(dotted)や破線(dashed)を作成できます。この方法では、heightは指定しません。
点線パターン
<hr class="dotted-hr">
hr.dotted-hr {
border: none; /* デフォルトの3D立体ボーダーを削除 */
border-top-style: dotted;
border-top-width: 2px; /* 線の太さ */
border-top-color: gray; /* 線の色 */
width: 50%; /* 線の幅(省略時は100%):自動で中央揃えになります */
}
活用例1
<hr class="dotted-hr">
hr.dotted-hr {
border: none;
border-top-style: dotted;
border-top-width: 5px;
border-top-color: orangered;
width: 50%;
}
活用例2
<hr class="dotted-hr">
hr.dotted-hr {
border: none;
border-top-style: dotted;
border-top-width: 5px;
border-top-color: orangered;
}
破線パターン
<hr class="dashed-hr">
hr.dashed-hr {
border: none; /* デフォルトの3D立体ボーダーを削除 */
border-top-style: dashed;
border-top-width: 2px; /* 線の太さ */
border-top-color: gray; /* 線の色 */
width: 50%; /* 線の幅(省略時は100%):自動で中央揃えになります */
}
活用例1
<hr class="dashed-hr">
hr.dashed-hr {
border: none;
border-top-style: dashed;
border-top-width: 5px;
border-top-color: orangered;
width: 50%;
}
活用例2
<hr class="dashed-hr">
hr.dashed-hr {
border: none;
border-top-style: dashed;
border-top-width: 5px;
border-top-color: orangered;
}
シンメトリカル区切り線(Symmetrical Divider)
<hr class="symmetrical-hr">
hr.symmetrical-hr {
position: relative;
overflow: visible;
border: none;
width: 300px; /* 中央の太い線の幅 */
max-width: calc(33.333% - 10px); /* 中央の太い線の幅に関係なく、レスポンシブWebデザインに対応 */
height: 10px; /* 中央の太い線の太さ */
background: gray; /* 中央の太い線の色 */
}
hr.symmetrical-hr:before,
hr.symmetrical-hr:after {
content: "";
position: absolute;
top: 50%;
width: 100%;
height: 1px; /* 両側の細い線の太さ */
background: gray; /* 両側の細い線の色 */
}
hr.symmetrical-hr:before {
transform: translateX(calc(-100% - 5px)) translateY(-50%);
}
hr.symmetrical-hr:after {
transform: translateX(calc(100% + 5px)) translateY(-50%);
}
活用例1
<hr class="symmetrical-hr">
hr.symmetrical-hr {
position: relative;
overflow: visible;
border: none;
width: 300px;
max-width: calc(33.333% - 10px);
height: 8px;
background: orangered;
}
hr.symmetrical-hr:before,
hr.symmetrical-hr:after {
content: "";
position: absolute;
top: 50%;
width: 100%;
height: 1px;
background: orangered;
}
hr.symmetrical-hr:before {
transform: translateX(calc(-100% - 5px)) translateY(-50%);
}
hr.symmetrical-hr:after {
transform: translateX(calc(100% + 5px)) translateY(-50%);
}
活用例2
<hr class="symmetrical-hr">
hr.symmetrical-hr {
position: relative;
overflow: visible;
border: none;
width: 300px;
max-width: calc(33.333% - 10px);
height: 10px;
background: linear-gradient(to right, orange, red);
border-radius: 5px;
}
hr.symmetrical-hr:before,
hr.symmetrical-hr:after {
content: "";
position: absolute;
top: 50%;
width: 100%;
height: 1px;
background: orangered;
}
hr.symmetrical-hr:before {
transform: translateX(calc(-100% - 5px)) translateY(-50%);
}
hr.symmetrical-hr:after {
transform: translateX(calc(100% + 5px)) translateY(-50%);
}