如何将文本左,中,右定位在中间的一条线?

问题描述 投票:-4回答:3

我想创建以下内容:

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

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

html css html5 css3
3个回答
2
投票

带有space-between的Flexbox容器。虚线是1px高度跨度的边界。确保文本跨度不随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>

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;
}

-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>

这个??

© www.soinside.com 2019 - 2024. All rights reserved.