div中间的横线

新手上路,请多包涵

我想在 div 中间画一条线。在下图中,该线应位于红色框的中间。

在此处输入图像描述

我正在尝试使用行高来做到这一点,但无法做到。

这是代码:

HTML/CSS:

 .wrap {
  text-align: center;
  margin: 20px;
}
.links {
  padding: 0 10px;
  border-top: 1px solid #000;
  height: 1px;
  line-height: 0.1em;
}
.dot {
  width: 20px;
  height: 20px;
  background: red;
  float: left;
  margin-right: 150px;
  position: relative;
  top: -10px;
}
 <div class="wrap">
  <div class="links">
    <div class="dot"></div>
    <div class="dot"></div>
    <div class="dot"></div>
  </div>
</div>

演示: https ://jsfiddle.net/nkq468xg/

原文由 sam 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.9k
2 个回答

您可以在 --- Flexbox 上使用 --- links 并且对于行,您可以在 wrap 元素上使用 :before 伪元素。

 .wrap {
  text-align: center;
  margin: 20px;
  position: relative;
}
.links {
  padding: 0 10px;
  display: flex;
  justify-content: space-between;
  position: relative;
}
.wrap:before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  border-top: 1px solid black;
  background: black;
  width: 100%;
  transform: translateY(-50%);
}
.dot {
  width: 20px;
  height: 20px;
  background: red;
}
 <div class="wrap">
  <div class="links">
    <div class="dot"></div>
    <div class="dot"></div>
    <div class="dot"></div>
  </div>
</div>

原文由 Nenad Vracar 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以使用伪元素,例如 ::after

 .links {
    padding: 0 10px;
    overflow: auto; // Your div will have the height of the overflowing elements
}

.links::after {
    content: '';
    width: 100%;
    height: 1px;
    background: black;
    display: block;
    position: relative;
    top: 10px;
}

原文由 Amaury Hanser 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏