【问题标题】:How to get even lines and spacing betweeen text using css如何使用css在文本之间获得均匀的行和间距
【发布时间】:2022-02-03 18:26:53
【问题描述】:

寻找一种在一组单词之间创建线条的方法。我可以让它发挥作用,但我并不总是知道单词的长度,而且我无法在文本和行之间保持均匀的间距。

我想我也许可以添加一些空的 div 来让它工作,但我试图避免将无用的标记添加到 DOM 中

.progress-bar {
  display: flex;
  width: 100%;
  justify-content: space-between;
  margin-top: 50px;
  }

  .progress-bar p {
    display: block;
    position: relative;
    width: 100%;
    text-align: center;
    line-height: 0.1em;
    
  }

  .progress-bar p::after {
    content: "";
    z-index: -1;
    position: absolute;
    bottom: 0;
    top: 0;
    right: 0;
    width: 25%;
    height: 2px;
    background: black;
  }

  .progress-bar p:last-of-type::after {
    background: none;
    content: "";
  }
<div class="progress-bar">
  <p>text one</p>
  <p> text two</p>
  <p>text three</p>
  <p>text four</p>
</div>

【问题讨论】:

  • 您的标题听起来好像您希望文本之间的间距均匀,但是您的代码在文本之间给出了不相等的间距(因为它们的长度不同并且您使用了 flex)。您能否确认您想要哪个,因为它显然会影响文本间行的绘制方式。

标签: html css sass


【解决方案1】:

最简单的解决方案是使用 display:contents 与您的文本,该行也将成为 flexbox 容器的一部分

.progress-bar {
  display: flex;
  justify-content: space-around;
  align-items:center;
  margin-top: 50px;
}

.progress-bar p {
  display: contents;
  text-align: center;
  line-height: 0.1em;
}

.progress-bar p:not(:last-of-type)::after {
  content: "";
  width: 8%;
  height: 2px;
  background: black;
}
<div class="progress-bar">
  <p>text one</p>
  <p> text two</p>
  <p>text three</p>
  <p>text four</p>
</div>

也像下面这样:

.progress-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 50px;
}

.progress-bar p {
  display: contents;
  text-align:center;
  line-height: 0.1em;
}

.progress-bar p:not(:last-of-type)::after {
  content: "";
  width: 8%;
  margin:0 3%;
  height: 2px;
  background: black;
}
<div class="progress-bar">
  <p>text one</p>
  <p> text two</p>
  <p>text three</p>
  <p>text four</p>
</div>

【讨论】:

【解决方案2】:

我没有找到使用 flex 和 space-between 的合适方法,因为我们以某种方式“丢失”了信息。每个文本的宽度和位置。

所以这是一个使用内联块、填充和边距的方法。

这里的技巧是将文本间行绘制为一条连续的线,然后让每个文本在左右两侧空白(使用填充和背景颜色)。文本的间距是均匀的,无论它们有多宽。

这个 sn-p 允许您设置 3 个变量来适应您想要的线宽、文本和线之间的间距以及进度条中的项目数。您可以使用它们来获得所需的间距。

结果是响应式的,如果需要,较长的文本将跨越几行,但文本间行停留在文本的中间。

.progress-bar {
  width: 100%;
  position: relative;
  margin-top: 50px;
  background-image: linear-gradient(black, black);
  background-size: 100% 2px;
  background-position: 0 calc(50% - 1px);
  background-repeat: no-repeat no-repeat;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  /* alter these to suit your use-case */
  --lineW: 6%;
  /* width of the lines between texts */
  --gapW: 3%;
  /* width of the gap between text and line */
  --n: 4;
  /* number of items in the progress bar */
}

p {
  display: inline-block;
  position: relative;
  background: white;
  text-align: center;
  padding: 0 var(--gapW);
  margin: 0 calc(var(--lineW) / 2);
  max-width: calc((100% - (var(--n) * (var(--gapW) + var(--lineW)))) / var(--n));
}

p:first-child::before,
p:last-child::after {
  content: '';
  display: inline-block;
  margin-right: 0;
  position: absolute;
  width: 100vw;
  background: white;
  z-index: 1;
  height: 100%;
}

p:first-child::before {
  right: 100%;
}

p:last-child::after {
  left: 100%;
}
<div class="progress-bar">
  <p>text one</p>
  <p>text two</p>
  <p>text three is noticeably wider</p>
  <p>text four</p>
</div>

【讨论】:

    【解决方案3】:

    您可以使用 gap sintaxe 来制作这些空格:

    在你的 css 中,你可以这样做:

    .progress-bar{
      display: flex;
      flex-orientation: column;
      gap: 2rem;
    }

    您可以在这里了解 flex: https://www.w3schools.com/css/css3_flexbox.asp

    【讨论】:

    • 感谢您的建议,但这不是我要找的。我试图让每个文本之间的线条均匀。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 2019-12-14
    • 2023-03-14
    相关资源
    最近更新 更多