【问题标题】:Stop :after element from jumping to the next row on wrap停止 :after 元素在换行时跳转到下一行
【发布时间】:2017-03-17 09:55:09
【问题描述】:

我有一些漂亮的 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>

这是它的作用:

但在一定大小时,:after 元素可以向下跳,如下所示:

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

【问题讨论】:

    标签: css flexbox pseudo-element


    【解决方案1】:

    弹性容器上的 ::before::after 伪元素被视为弹性项目。

    ::before 伪是第一项。 ::after 伪是最后一个。因此,您必须将它们视为其他弹性项目的兄弟姐妹。

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

    【讨论】:

      【解决方案2】:

      这是什么 CSS 预处理器?我在 CodePen 上使用了每个预编译器,并从 $variables 中得到了错误,所以我删除了它们并用任何东西填充它们。这是手写笔吗?无论如何,除了变量被移除和一些颜色变化之外,唯一重要的变化是我将flex-wrap:wrap 更改为flex-wrap:nowrap

      片段

      .processBar {
        display: flex;
        flex-wrap: nowrap;
      }
      
      .processBar_item {
        display: inline-block;
        background: #c8c8c8;
        height: 40px;
        position: relative;
        flex-grow: 1;
        text-align: center;
        line-height: 40px;
        text-decoration: none;
        color: #000;
        padding-left: 10px;
        padding-right: 10px;
      }
      
      .processBar_item:hover {
        text-decoration: none;
        color: red;
      }
      
      .processBar_item-default:hover {
        background: #808080;
      }
      
      .processBar_item-selected {
        background: cyan;
      }
      
      .processBar_item:before,
      .processBar_item:after {
        display: inline-block;
        content: "";
        border-style: solid;
        position: absolute;
      }
      
      .processBar_item:first-child:before {
        left: 0;
        border: none;
      }
      
      .processBar_item:not(:first-child):before {
        left: 0;
        border-color: red;
        border-width: 20px 0 20px 5px;
      }
      
      .processBar_item:last-child:after {
        right: 0;
        border: none;
      }
      
      .processBar_item:not(:last-child):after {
        right: 0;
        border-color: tomato;
        border-width: 20px 0 20px 5px;
      }
      <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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-22
        • 2019-03-06
        • 2021-05-17
        • 1970-01-01
        • 2011-07-31
        • 2019-11-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多