css - 停止 :after element from jumping to the next row on wrap

标签 css flexbox pseudo-element

我有一些漂亮的 CSS:

$itemHeight: 40px;
$arrowModifier: 8;
$pageBackgroundColor: $color-gray-50;

.processBar {
    display: flex;
    flex-wrap: wrap;

    &_item {
        display: inline-block;
        background: $color-gray-200;
        height: $itemHeight;
        position: relative;
        flex-grow: 1;

        text-align: center;
        line-height: $itemHeight;

        text-decoration: none;
        color: $color-text;
        padding-left: 10px;
        padding-right: 10px;

        &:hover {
            text-decoration: none;
            color: $color-text;
        }

        &-default {
            &:hover {
                background: $color-light-gray;
            }
        }

        &-selected {
            background: $color-white;
        }

        &:before,
        &:after {
            display: inline-block;
            content: "";
            border-style: solid;
            position: absolute;
        }

        &:first-child {
            &:before {
                left:0;
                border: none;
            }
        }

        &:not(:first-child) {
            &:before {
                left:0;
                border-color: transparent transparent transparent $pageBackgroundColor;
                border-width: $itemHeight/2 0 $itemHeight/2 $itemHeight/$arrowModifier;
            }
        }

        &:last-child {
            &:after {
                right: 0;
                border: none;
            }
        }

        &:not(:last-child) {
            &:after {
                right:0;
                border-color: $pageBackgroundColor transparent;
                border-width: $itemHeight/2 0 $itemHeight/2 $itemHeight/$arrowModifier;
            }
        }
    }
}

这是使用它的 HTML:

<div class="processBar">
    <a href="javascript:" class="processBar_item">Label</a>     
    <a href="javascript:" class="processBar_item">Label</a>     
    <a href="javascript:" class="processBar_item">Label</a>     
    <a href="javascript:" class="processBar_item">Label</a>     
    <a href="javascript:" class="processBar_item">Label</a>     
    <a href="javascript:" class="processBar_item">Label</a>
</div>

这是它的作用:

enter image description here

但是在一定尺寸下 :after 元素可以跳下来,如下所示:

enter image description here

我不明白为什么会这样。我怎样才能阻止它发生?

最佳答案

flex 容器上的 ::before::after 伪元素被认为是 flex 元素。

::before 伪是第一项。 ::after 伪是最后一个。因此,您必须将它们视为其他 flex 元素的 sibling 。

您可以尝试使用 order 属性重新排列元素以达到您想要的效果。

关于css - 停止 :after element from jumping to the next row on wrap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42854308/

相关文章:

CSS - 让 div 首先出现在标记中,但显示在 float div 下方

html - 某些样式不适用于 :after

CSS 伪元素

css - 无法更改访问的内容 :before pseudo-elements

javascript - 使用jquery优化在html中追加元素的方式

html - 创建等宽 flex 元素

reactjs - 如何使用 Reactjs 在 NavBar 的有序列表中使用 flex 方向?

flexbox - flutter - 如何获得像 css flex display none 这样的效果

css - 带有 Font Awesome 5 图标的复选框

html - 跨所有浏览器的 CSS 对齐