css - 为什么 flexbox 会影响其包含列表项的标记?

标签 css flexbox listitem

知道之后a list-item element cannot be display: flex at the same time ,我用容器包裹我的内容,然后将容器放入 <li /> 中.

对于有内容的元素来说一切顺利。 但是,如果容器中的第一个元素没有子节点,<li /> 的标记意外地位于底部。

虽然在我的例子中我可以简单地添加一个 display: none解决方法,我仍然感到困惑: flexbox 中的内容如何影响其祖父节点?

示例:(标记 1. 位于 <li /> 的右下角)

.flex { display: flex; }
<ol style="width: 15em">
    <li>
        <div class="flex">
            <div></div>
          <div>When an element without content is preceding, the ::marker of the list-item is located at the bottom.</div>
        </div>
    </li>
    <li>
        <div class="flex">
            <div>TITLE</div>
            <div>If there's content in the first element, then it goes as expected.</div>
        </div>
    </li>
    <li>
        <div class="flex">
            <div>It's also fine if there's only one child here.</div>
        </div>
    </li>
</ol>

相关问题: CSS伪元素::marker在流行的浏览器中没有实现,只有 Firefox 支持 @counter-style .目前,建议在 <ol /> 中实现用户定义的标记。 ? (我看到有些人使用 ::before 来做到这一点,目的是让用户不要复制标记的文本。但我不知道它是否好。)

最佳答案

我不确定您要在元素内使用 flexbox 来完成什么,但是 display flex 内的空元素会塌陷,因此它会产生奇怪的影响。您可以通过使用 flex: numberflex-basis: 50px

给元素一个“大小”来解决这个问题

我会避免使用 ::marker 属性,因为它有 horrific support并使用 css 计数器简单地创建您自己的计数器。

这是一个例子:

ol {
  display:block;
  width: 30em;
  list-style: none;
  counter-reset: counterName;
}
li {
 padding-left: 2em;
 padding-bottom: 1em;
 border-bottom: 1px solid #ccc;
 margin-bottom: 1em;
 position: relative;
 counter-increment: counterName;
}
li:before {
  content:'#' counter(counterName);
  position: absolute;
  left:0;
  top:0;
}

.flex {
  display:flex;
  justify-content: space-between;
  flex-wrap:no-wrap;
}

.flex > * {
  flex: 1;
}
<ol>
    <li>
        <div class="flex">
            <div></div>
          <div>When an element without content is preceding, the ::marker of the list-item is located at the bottom.</div>
        </div>
    </li>
    <li>
        <div class="flex">
            <div>TITLE</div>
            <div>If there's content in the first element, then it goes as expected.</div>
        </div>
    </li>
    <li>
        <div class="flex">
            <div>It's also fine if there's only one child here.</div>
        </div>
    </li>
</ol>

关于css - 为什么 flexbox 会影响其包含列表项的标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54466230/

相关文章:

html - Flexbox 不垂直居中内容

html - flex-direction 和 flex 不适用于所有浏览器

list - 来自 JSON 数据的列表中的material-ui 动态嵌套项目

css - 列表项之间的间距不规则

html - Flexbox 中的 CSS Grid 仅在 Chrome 上无法按预期工作

Delphi ListView拖放整行

jquery - 如何拖动一个元素,但它仍应保留在原来的位置?

html - CSS `height: calc(100vh);` 与 `height: 100vh;`

html - 使 CSS 选项卡具有特定的宽度和高度

html - HTML 表格 <td> 宽度扩展困难