div中间的水平线

sam*_*sam 11 html css

我想在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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="wrap">
  <div class="links">
    <div class="dot"></div>
    <div class="dot"></div>
    <div class="dot"></div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

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

Nen*_*car 15

你可以使用Flexboxon links和for line,你可以:before在wrap元素上使用伪元素.

.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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="wrap">
  <div class="links">
    <div class="dot"></div>
    <div class="dot"></div>
    <div class="dot"></div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


jun*_*kie 5

下面是该行实际上位于顶部的情况,但它确实向 HTML 添加了另一个元素:

https://jsfiddle.net/nkq468xg/2/

.wrap {
    text-align: center; 
    margin: 20px; 
}
.links { 
    height: 20px;
    position: relative;
}
hr {
    border: 0;
    height: 1px;
    background: black;
    position: absolute;
    top: 1px;
    width: 100%;
}
.dot {
    width: 20px;
    height: 20px;
    background: red;
    float: left;
    margin-right: 150px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="wrap">
  <div class="links">
    <hr>
    <div class="dot"></div>
    <div class="dot"></div>
    <div class="dot"></div>
  </div>   
</div>  
Run Code Online (Sandbox Code Playgroud)

  • 添加 HTML 元素应该***始终***是最后的手段。在这里,这是完全没有必要的。 (2认同)

Ama*_*ser 5

您可以使用伪元素,例如::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;
}
Run Code Online (Sandbox Code Playgroud)