内容左侧和右侧的水平线特定宽度

Horizontal line left and right sided from content for a specific width

我有一个 header,我想在 header 的左侧和右侧放置一条水平虚线,如您在 jsfiddle

中所见

https://jsfiddle.net/uLake5g3/9/

这是我的HTML:

<div class = "spotlight">
  <div class = "container spotlight__main">
    <h2><span class  ="background__red">In the spotlight</span></h2>
  </div>
</div>

这是我的CSS:

h2 > span {
  position: relative;
  display: inline-block;
}

h2 > span:before,
h2 > span:after {
  content: "";
  position: absolute;
  top: 50%;
  border-bottom: 1px dashed;
  width: 96rem;
  margin: 0 auto;
  vertical-align: center;
}

h2 > span:before {
  right: 100%;
}

h2 > span:after {
  left: 100%;
}

我想在 spotlight_main his 边框内添加虚线,但它们在外面。

你可以试试:

h2:before{
    border-bottom: 1px dashed;
    color: #fff;
    content: "";
    left:0;
    position: absolute;
    right:0;
    top: 50%;
}

您可以将 overflow: hidden; 添加到容器中。

我在这里添加了一些额外的规则,以明确破折号不会延伸到容器之外。

.container {
  overflow: hidden;
  width: 400px;
  margin: auto;
  text-align: center;
}

h2>span {
  position: relative;
  display: inline-block;
}

h2>span:before,
h2>span:after {
  content: "";
  position: absolute;
  top: 50%;
  border-bottom: 1px dashed;
  width: 96rem;
  margin: 0 auto;
  vertical-align: center;
}

h2>span:before {
  right: 100%;
}

h2>span:after {
  left: 100%;
}
<div class="spotlight">
  <div class="container spotlight__main">
    <h2><span class="background__red">In the spotlight</span></h2>
  </div>
</div>