在具有相等空间分布的弹性项之间添加分界线

Nic*_*las 18 html css css3 flexbox

我有一个列表,其中包含具有自动宽度的不同项目(在我的情况下,不能给出固定的宽度).我使用justify-content: space-between是因为我的第一个项目必须从容器的开头开始,最后一个项目开始.

所有上述工作都很好,但每当我尝试在这些列表项之间添加一行时,问题就会出现.我无法确定需要多少px或%来定位这些线.有没有办法"动态"定位不同列表项之间的行?

我们使用的html是不可编辑的,因为它是由我们正在使用的CMS呈现的.

这就是我所拥有的:

这就是我所拥有的

这是我试图实现的目标

这就是我想要实现的目标

这是我目前的代码

html {
    box-sizing: border-box;
}

.Container {
    max-width: 70%;
    margin-right: auto;
    margin-left: auto;
    background: blue;
    padding-top: 20px;
    padding-bottom: 20px;
}

.Flex {
    display: flex;
    flex-flow: row wrap;
    justify-content: space-between;
    list-style: none;
    margin: 0;
    padding: 0;
}

.Flex-item {
    background: red;
    position: relative;
}

.Flex-item:after {
    content: "";
    position: absolute;
    background: white;
    width: 1px;
    height: 40px;
    top: 50%;
    transform: translateY(-50%);
}
Run Code Online (Sandbox Code Playgroud)
<div class="Container">
    <ul class="Flex">
         <li class="Flex-item">Lorem</li>
         <li class="Flex-item">consectetur</li>
         <li class="Flex-item">vestibulum</li>
         <li class="Flex-item">nec</li>
         <li class="Flex-item">condimentum</li>
    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

Ste*_*erg 17

我正在使用这个解决方案来处理我正在进行的项目.

它设置justify-content: space-between;在弹性容器和flex: 1 1 auto;所有儿童的左边框上,除了第一个.

我修改了你的示例CSS,你可以看看.我不确定你是否会对孩子们有背景颜色,所以我只是用线高来获得更大的边框.

html {
    box-sizing: border-box;
}

.Container {
    max-width: 70%;
    margin-right: auto;
    margin-left: auto;
    background: blue;
    padding-top: 20px;
    padding-bottom: 20px;
}

.Flex {
    display: flex;
    flex-flow: row wrap;
    justify-content: space-between;
    list-style: none;
    margin: 0;
    padding: 0;
}

.Flex-item {
    flex: 1 1 auto;
    background: red;
    position: relative;
    text-align: center;
    line-height: 40px;
}

.Flex-item + .Flex-item {
    border-left: solid 1px white;
}

/** Optional for OPs exact layout */

.Flex-item:first-child {
    text-align: left;
}

.Flex-item:last-child {
    text-align: right;
}
Run Code Online (Sandbox Code Playgroud)
<div class="Container">
    <ul class="Flex">
        <li class="Flex-item">Lorem</li>
        <li class="Flex-item">consectetur</li>
        <li class="Flex-item">vestibulum</li>
        <li class="Flex-item">nec</li>
        <li class="Flex-item">condimentum</li>
    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

无需修改HTML.

  • 它是相邻同级的 CSS 选择器。因此,类为“Flex-item”的元素旁边的类为“Flex-item”的元素将获得左侧边框。在本例中,它是“跳过”第一个具有左边框的元素。这样你就不必编写 `.Flex-item:first-child { border-left: 0; }` https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator (4认同)

Rok*_*jan 7

:after使用风格化的 ARIA 代表性 LI

<li class="separator" aria-hidden="true" role="presentation"></li>
Run Code Online (Sandbox Code Playgroud)

喜欢:

<li class="separator" aria-hidden="true" role="presentation"></li>
Run Code Online (Sandbox Code Playgroud)
.Container {
  max-width: 70%;
  margin: 0 auto;
  background: blue;
}

.Flex {
  display: flex;
  justify-content: space-between;
  list-style: none; padding: 20px 0; margin:0;
}

.Flex-item {
  background: red;
}

.separator {
  background: white; width: 1px;
}
Run Code Online (Sandbox Code Playgroud)

display: list-itemPS:是的,我最初尝试在:after伪上使用,但不。

  • 此外,辅助技术现在将列出 10 项。 (3认同)

kuk*_*kuz 6

您可以使用嵌套es使其工作- 我知道您无法更改标记,但至少您必须将 es 的内容包装成我在这里的样子: flexboxlispan

  1. .flex-itemflexbox有一个文本span(这将有现在的红色背景)和分离器作为一个:after元素

  2. 应用flex-growflex-shrink为1,flex-basisautoFlex-item

  3. flex: 0最后Flex-item,并margin-auto:after也有助于效果。

演示可能会更好地解释它 - 见下文:

html {
  box-sizing: border-box;
}
.Container {
  max-width: 70%;
  margin-right: auto;
  margin-left: auto;
  background: blue;
  padding-top: 20px;
  padding-bottom: 20px;
}
.Flex {
  display: flex;
  justify-content: space-between;
  list-style: none;
  margin: 0;
  padding: 0;
}
.Flex-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex: 1 1 auto;
}
.Flex-item span {
  background: red;
}
.Flex-item:not(:last-child):after {
  content: "";
  border: 1px solid white;
  height: 40px;
  margin: auto;
}
.Flex-item:last-child {
  flex: 0;
}
Run Code Online (Sandbox Code Playgroud)
<div class="Container">
  <ul class="Flex">
    <li class="Flex-item">
      <span>Lorem</span>
    </li>
    <li class="Flex-item">
      <span>consectetur</span>
    </li>
    <li class="Flex-item">
      <span>vestibulum</span>
    </li>
    <li class="Flex-item">
      <span>nec</span>
    </li>
    <li class="Flex-item">
      <span>condimentum</span>
    </li>
  </ul>
</div>
Run Code Online (Sandbox Code Playgroud)