How to underline part of a heading (first 40px for instance)?

大城市里の小女人 提交于 2021-01-27 07:02:20

问题


Is it possible in simple CSS to have a heading underlined partly only, specifically first 50px on the left?


回答1:


You can use the :before pseudo element and add a border to it.

h1 {
  position: relative;
  line-height: 1.2em;
}
h1:before {
  position: absolute;
  left: 0;
  top: 1.2em;
  height: 0;
  width: 50px;
  content: '';
  border-top: 1px solid red;
}
<h1>This is a header, partly underlined</h1>



回答2:


Use a pseudo-element:

h1 {
  position: relative;
}
h1::before {
  content: '';
  position: absolute;
  bottom: 0; left: 0;
  width: 50px;
  height: 2px;
  background-color: green;
}
<h1>foobar</h1>



回答3:


You can add a pseudo element containing a couple of non-breaking spaces that are underlined. This way you get a real underlining instead of a border. However it will still cross over letters like "g".

h1::before {
  content:'\a0\a0\a0\a0\a0\a0\a0\a0';
  display:block;
  position:absolute;
  text-decoration:underline;
  width:50px;
  overflow:hidden;
}
<h1>Headline</h1>
<h1>Another Headline</h1>


来源:https://stackoverflow.com/questions/36860457/how-to-underline-part-of-a-heading-first-40px-for-instance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!