この質問は古いかもしれませんが、私は今日この効果が必要であり、絶対に配置されたCSS疑似要素を使用してそれを達成しました。まず、HTMLの要素のテキストの周りにラッパーを配置します。
<h3>
<span>Title</span>
</h3>
このスパンを使用して、テキストと重なる線の部分(疑似要素)を覆います。これにより、テキストの終わりから行の終わりまでの行の効果が得られますが、実際には、行は常にコンテナの100%の幅になります。
h3 {
/* we will absolutely position the line relative to the parent element (h3) */
position: relative;
z-index: 1;
}
h3 span {
background: #dedede; /* make this the same color as the page background! */
padding-right: 0.5em; /* this will be the 'margin' from the text to the line */
}
/* Add Line After Text*/
h3 span::after {
content: '';
width: 100%;
height: 2px;
background: #919098;
/* position the line within the parent element */
position: absolute;
bottom: 0.5em; /* half the height of the text from the bottom */
left: 50%; transform: translateX(-50%); /* horizontally center it */
z-index: -1; /* display it behind the parent */
}
この方法の結果
これは見出し要素を使用して行いましたが、タグを入れ替えることで、段落やその他の要素にこの効果を簡単に使用できます。頑張ってください!