hrの上にカスタムデータ属性で設定したテキストを表示する

Ads

Result


hrに疑似要素を加えてテキストを表示させるtipsです
ただ、hrタグは終了タグがないためimgタグ同様、空要素扱いのハズなので仕様上どうなんだという疑問もあります
また、IEやEdgeでは表示に問題が生じる可能性があるようです
上記の件はこちらをご参照ください
(:before and :after to HR in Microsoft Edge and IE)

css

.hr-text {/*hr基本設定*/
  line-height: 1em;
  position: relative;
  outline: 0;
  border: 0;
  color: black;
  height: 1.5em;
  opacity: .5;
}
.hr-text:before {/*疑似要素でグラデーション設定*/
  content: '';
  background: linear-gradient(to right, transparent, #818078, transparent);
  position: absolute;
  left: 0;
  top: 50%;
  width: 100%;
  height: 1px;
}
.hr-text:after {/*疑似要素でHTMLで書いたカスタムデータ属性の値を表示*/
  content: attr(data-content);
  position: relative;
  display: inline-block;
  color: black;
  padding: 0 .5em;
  line-height: 1.5em;
  color: #818078;
  background-color: #fcfcfa;
}

html

<hr class="hr-text" data-content="東京">

via

HR with centered text

タイトルとURLをコピーしました