【问题标题】:CSS border-bottom problems [duplicate]CSS边框底部问题[重复]
【发布时间】:2019-05-23 10:00:17
【问题描述】:

我想要一个分隔标题和文本的边框,我有它,但它是 h2 的 100%,我希望它像 80% 一样位于中心。

我尝试将 h2 分开,但如果我添加 80% 的宽度,它会使 h2 成为整个 div 的 80% 宽度。 PS 我希望这段代码能显示我想要的方式。

.about-section .title {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100px; 
  font-size: 20px;
}
.title h2 {
  border-bottom: 3px solid #fe9004;
  padding-bottom: 3px;
}

【问题讨论】:

标签: css


【解决方案1】:

您可以使用伪元素:after 来实现:

.about-section .title {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100px; 
  font-size: 20px;
}
.title h2 {
  position: relative;
}
.title h2:after {
  content: '';
  display: block;
  height: 3px;
  width: 80%;
  position: absolute;
  bottom: -10px;
  left: 10%;
  background-color: #fe9004;
}
<div class="about-section">
  <div class="title">
    <h2>About</h2>
  </div>
  <div class="content">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  </div>
</div>

【讨论】:

  • 有趣,我之前试过这个,但你的代码不起作用,因为它将行推到底部,正如我所说的 width: 80% 使它成为整个 div 的 80%。这是对我有用的代码,但我无法将其居中,也无法添加 margin-bottom 以将文本向下推。这里有代码笔codepen.io/raul-rogojan/pen/arqmoe?editors=1100
  • 修复了!立场:绝对是问题所在。我仍然无法添加底部边距,但我会弄清楚的
  • 要使 line 居中,您必须从左侧移动 10%。 left: 10%;。要将 content-div(文本)向下推,您可以将边距添加到 .title-style。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
  • 2021-11-13
  • 2012-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多