如何为标题的一部分加下划线(例如前40px)?

dra*_*035 4 css underline

是否有可能在简单的CSS中只有一个标题下划线,特别是左边的前50px?

Pau*_*aul 11

您可以使用:before伪元素并为其添加边框.

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;
}
Run Code Online (Sandbox Code Playgroud)
<h1>This is a header, partly underlined</h1>
Run Code Online (Sandbox Code Playgroud)


Vuc*_*cko 7

使用pseudo-element:

h1 {
  position: relative;
}
h1::before {
  content: '';
  position: absolute;
  bottom: 0; left: 0;
  width: 50px;
  height: 2px;
  background-color: green;
}
Run Code Online (Sandbox Code Playgroud)
<h1>foobar</h1>
Run Code Online (Sandbox Code Playgroud)