如何在文本的左,中,右位置中间放置一行?

时间:2019-03-14 22:34:03

标签: html css html5 css3

我想创建以下内容:

Left Text - - - - - - Center Text - - - - - - Right Text

其中-----是1px的虚线,位于文本高度的中间。

3 个答案:

答案 0 :(得分:2)

带有space-between的Flexbox容器。虚线是1像素高度跨度上的边框。确保文本范围不会随着flex: 0 0 auto;

的增长

.container {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.container .text {
  flex: 0 0 auto;
}
.container .dashed {
  display: block;
  width: 50%;
  height: 1px;
  border-top: 1px dashed #000;
  margin: 0 5px;
  box-sizing: border-box;
}
<div class="container">
    <span class="text">Left Text</span>
    <span class="dashed"></span>
    <span class="text">Center Text</span>
    <span class="dashed"></span>
    <span class="text">Right Text</span>
</div>

答案 1 :(得分:0)

我会做这样的事情:https://jsfiddle.net/s59rzh4b/

<div class="outer">
  <div class="center">
    <span class="middle">More text</span>
  </div>
  <span class="left">Some text</span>
  <span class="right">Even more...</span>
</div>
.outer {
  position: relative;
  width: 100%;
}

.outer > * {
  position: absolute;
  background: white;
  padding: 0 5px;
  top: 0;
}

.left {
  left: 0;
}

.center {
  text-align: center;
  width: 100%;
  height: 8px;
  border-style: solid;
  border-color: black;
  border-width: 0 0 2px 0;
}

.middle {
  background: white;
  padding: 0 5px;
}

.right {
  right: 0;
}

答案 2 :(得分:-1)

html, body, .grid-container { height: 100%; margin: 0; }

.grid-container *:after { 
 position: absolute;
 top: 0;
 left: 0;
}

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  grid-template-areas: "t1 t2 t3" ". . ." ". . .";
 txt-align: center;
  
}

.t1 { grid-area: t1; }

.t2 { grid-area: t2; }

.t3 { grid-area: t3; }
<div class="grid-container">
  <div class="t1">sfsfds -----------------------</div>
  <div class="t2">dsfsdf -------------------</div>
  <div class="t3">dsfdsf -----------------------</div>
</div>

这个?