【问题标题】:Using ::before & ::after pseduo elements to move element from left to right使用 ::before & ::after 伪元素将元素从左向右移动
【发布时间】:2021-12-27 11:49:38
【问题描述】:

在 CSS 中使用 ::before::after 相当新。到目前为止,这是我所拥有的:

我有StyledLabel 上的之前和之后创建的复选框 - 我基本上只是想交换复选框和标签的顺序,以便复选框位于标签之后(右侧) ?

.wrapper {
  display: inline-flex;
  min-height: 1.5rem;
  padding-left: 1.5rem;
  margin-right: 1 rem;
}

.label {
  position: relative;
  margin-bottom: 0;
 }


 .label:before {
    position: absolute;
    top: 4px;
    left: -24px;
    display: block;
    width: 20px;
    height: 20px;
    pointer-events: none;
    content: "";
    background-color: red;
  }
  
 .label:after {
    position: absolute;
    top: 4px;
    left: -24px;
    display: block;
    width: 40px;
    height: 40px;
    content: "";
    background-repeat: no-repeat;
    background-position: center center;
    background-size: 50% 50%;
  }
}
<div class="wrapper">
  <label class="label">
    <div class="label-wrapper">In progress</div>
  </label>
</div>

【问题讨论】:

    标签: css css-selectors styled-components pseudo-element


    【解决方案1】:

    我在 css 中添加了 cmets 以显示您需要更改的内容。

    .wrapper {
        display: inline-flex;
        min-height: 1.5rem;
        /*padding-left: 1.5rem;*/ /* change this */
        padding-right: 1.5rem; /* to this */
        
        /*margin-right: 1rem;*/ /* change this */
        margin-left: 1rem; /* to this or remove of you will */
    }
    
    .label {
        position: relative;
        margin-bottom: 0;
    }
    
    .label:before {
        content: '';
        position: absolute;
        top: 4px;
        /*left: -24px;*/ /* change this */
        right: -24px; /* to this */
        display: block;
        width: 20px;
        height: 20px;
        pointer-events: none;
        background-color: #fff;
        border: 1px solid black; /* added just to show where this element is */
    }
    
    .label:after {
        content: '';
        position: absolute;
        top: 4px;
        /*left: -24px;*/ /* change this */
        right: -24px; /* to this */
        display: block;
        width: 40px;
        height: 40px;
        background-repeat: no-repeat;
        background-position: center center;
        background-size: 50% 50%;
    }
    <div class="wrapper">
        <label class="label">
            <span class="label-wrapper">In progress</span>
        </label>
    </div>

    【讨论】:

      【解决方案2】:

      这里不需要使用定位,只需要flexbox和row-reverse

      .wrapper {
        display: inline-flex;
        align-items: center;
        min-height: 1.5rem;
        padding-left: 4px;
        margin-right: 1rem;
        border:1px solid grey;
      }
      
      .label {
        display: flex;
        flex-direction:row-reverse;
        position: relative;
        margin-bottom: 0;
      }
      .label:before {
          display: block;
          width: 20px;
          height: 20px;
          pointer-events: none;
          content: "";
          background-color: red;
          margin:0  4px;
      
        }
      <div class="wrapper">
        <label class="label">
          <div class="label-wrapper">In progress</div>
        </label>
      </div>  

      【讨论】:

        猜你喜欢
        • 2015-05-06
        • 2011-04-02
        • 1970-01-01
        • 1970-01-01
        • 2020-09-20
        • 2016-05-24
        • 2018-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多