flexbox 中元素之间的行分隔符

问题描述 投票:0回答:3

我正在尝试在 CSS 中使用

display:flex;
制作一行具有相同文本间距的元素。

这就是我得到的,我使用

display: inline-block;
和文本之间的间距差异来实现它 - 我希望对每个文本都进行相同的处理。

element {
  border-width: 1px;
  border-style: solid;
  color: rgb(0, 146, 247);
  display: inline-block;
  height: 18px;
  border-radius: 30px;
  vertical-align: middle;
}

.footertext {
  font-family: 'Open Sans', sans-serif;
  color: rgb(124, 134, 205);
  margin-left: 20px;
  margin-right: 20px;
  display: inline-block;
  vertical-align: middle;
}
<div>
  <p class="footertext">
    First Line
  </p>
  <element></element>
  <p class="footertext">
    ABC
  </p>
  <element></element>
  <p class="footertext">
    Third Line
  </p>
  <element></element>
  <p class="footertext">
    DEFG
  </p>
</div>

我需要文本之间那些有趣的元素,当我尝试使用

display:flex;
时,这些元素就会超出范围。

css flexbox
3个回答
17
投票

flex怎么样,除了第一个元素之外的所有元素都有左边框:

div {
  display: flex;
}

.footertext {
  padding-left: 20px;
  padding-right: 20px;
}

.footertext + .footertext {
  border-left: 3px solid rgb(0, 146, 247);
}

* { box-sizing: border-box; }

/* non-essential decorative styles */
.footertext {
  font-family: 'Open Sans', sans-serif;
  color: rgb(124, 134, 205);
}
<div>
  <p class="footertext">First Line</p>
  <p class="footertext">ABC</p>
  <p class="footertext">Third Line</p>
  <p class="footertext">DEFG</p>
</div>


11
投票

这是一种简化的方法。

.footer-texts {
  display: flex;
  color:rgb(124,134,205);
  font-family: sans-serif;
}
.footer-texts > span {
  position: relative;
  padding: .5rem 1rem;
  display: flex;
  text-align: center;
  justify-content: center;
  align-items: center;
  flex: 0 1 25%;
}
.footer-texts > span:not(:last-child)::after {
  content: '';
  position: absolute;
  right: -2px;
  top: 25%;
  width: 2px;
  height: 50%;
  background-color:rgb(0, 146,247);
}
<div class="footer-texts">
  <span>First Line</span>
  <span>ABC</span>
  <span>Third Line<br />two lines</span>
  <span>DEFG</span>
</div>

一些注意事项:

  • 无需向所有子级添加相同的类,只需向父级添加一个并使用
    .someClassName > span
    设置样式(其中
    someClassName
    是类名,
    span
    是子级选择器。
  • 尽可能使用伪元素而不是 DOM 元素向标记添加分隔符或任何其他类型的装饰器。在这种特殊情况下,
    border-right
    也是一个不错的候选者。

0
投票

有一篇关于该主题的好文章 https://ishadeed.com/article/flexbox-separator/

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