【问题标题】:Pseudo element reduces in size when elements around grow当周围的元素增长时,伪元素的大小会减小
【发布时间】:2022-01-02 19:07:08
【问题描述】:

我有以下标签和复选框的代码,当标签内容很短时,它看起来很完美:

但是,当标签变长时,复选框被“压扁”并且勾号不再在其中对齐,我如何使用 flex 来创建更通用的解决方案?

.outter-wrapper {
  width: 200px;
  border: 1px solid black;
}

.wrapper {
  display: inline-flex;
  min-height: 1.5rem;
  padding: 8px 24px 8px 0px;
  width: 100%;
}

.input {
  position: absolute;
  z-index: -1;
  visibility: hidden;
}

.label-wrapper {
 line-height: 1.5rem;
}


.label {
  position: relative;
  margin-bottom: 0;
  display: flex;
  margin-left: 0;
  margin-right: 24px;
  flex-direction: row-reverse;
  justify-content: space-between;
  flex: 1;
  align-items: center;

}

.label:before {
  display: block;
  width: 20px;
  height: 20px;
  content: "";
  background-color: #fff;
  border: 1px solid black;
  border-radius: 4px;
  flex: none;
}

.label:after {
  position: absolute;
  display: block;
  height: 20px;
  content: "";
  background-repeat: no-repeat;
  background-position: center center;
  background-size: 80%;
}

input[type="checkbox"]:checked~.label::after {
  background-image: url("https://cdn-icons-png.flaticon.com/512/447/447147.png");
  width: 20px;
  height: 20px;
  top: 0;
}
<div class="outter-wrapper">  
  <div class="wrapper">
    <input class="input" name="checkbox2" id="checkbox2"        type="checkbox">
    <label class="label" for="checkbox2">
      <div class="label-wrapper">
        Option 1Option 1Option 1Option 1Option 1
      </div>  
    </label>
  </div>
</div>

  
 


  

我知道flex-wrap 可能是正确的方法,但不确定是否可以考虑使用:before 创建“复选框”?

【问题讨论】:

  • 这是因为您将flex-shrink 保留为默认值,因此允许 将伪元素的宽度缩小到您指定的20px 以下。将width: 20px 替换为flex: 0 0 20px

标签: html css flexbox pseudo-element


【解决方案1】:

试试这个代码:

.outter-wrapper {
  width: 200px;
  border: 1px solid black;
}

.wrapper {
  display: inline-flex;
  min-height: 1.5rem;
  padding: 8px 24px 8px 0px;
  width: 100%;
}

.input {
  position: absolute;
  z-index: -1;
  visibility: hidden;
}

.label-wrapper {
 line-height: 1.5rem;
  position: absolute;
  left: 10px;
}


.label {
  position: relative;
  margin-bottom: 0;
  display: flex;
  margin-left: 0;
  margin-right: 24px;
  flex-direction: row-reverse;
  justify-content: space-between;
  flex: 1;
  align-items: center;
}

.label:before {
  display: block;
  min-width: 20px;
  height: 20px;
  content: "";
  background-color: #fff;
  border: 1px solid black;
  border-radius: 4px;
  position:absolute;
  top:0;
}

.label:after {
  position: absolute;
  display: block;
  height: 20px;
  content: "";
  background-repeat: no-repeat;
  background-position: center center;
  background-size: 80%;
}

input[type="checkbox"]:checked~.label::after {
  background-image: url("https://cdn-icons-png.flaticon.com/512/447/447147.png");
  width: 20px;
  height: 20px;
  top: 0;
}
<div class="outter-wrapper">  
  <div class="wrapper">
    <input class="input" name="checkbox2" id="checkbox2"        type="checkbox">
    <label class="label" for="checkbox2">
      <div class="label-wrapper">
        THis world is full of creatures
      </div>  
    </label>
  </div>
</div>

Codepen 链接:https://codepen.io/emmeiWhite/pen/LYjKWEw

注意:可能需要在任何父级上使用min-height(如果需要),以便文本保留在输入框中

【讨论】:

    【解决方案2】:

    如果你想为一个弹性容器内的元素固定宽度,你需要一个flex : none。希望它能解决您的问题。

    .outter-wrapper {
      width: 200px;
      border: 1px solid black;
    }
    
    .wrapper {
      display: inline-flex;
      min-height: 1.5rem;
      padding: 8px 24px 8px 0px;
      width: 100%;
    }
    
    .input {
      position: absolute;
      z-index: -1;
      visibility: hidden;
    }
    
    .label-wrapper {
     line-height: 1.5rem;
    }
    
    
    .label {
      position: relative;
      margin-bottom: 0;
      display: flex;
      margin-left: 0;
      margin-right: 24px;
      flex-direction: row-reverse;
      justify-content: space-between;
      flex: 1;
      align-items: center;
    
    }
    
    .label:before {
      display: block;
      width: 20px;
      height: 20px;
      content: "";
      background-color: #fff;
      border: 1px solid black;
      border-radius: 4px;
      /* a line I added*/
      flex:none;
    }
    
    .label:after {
      position: absolute;
      display: block;
      height: 20px;
      content: "";
      background-repeat: no-repeat;
      background-position: center center;
      background-size: 80%;
    }
    
    input[type="checkbox"]:checked~.label::after {
      background-image: url("https://cdn-icons-png.flaticon.com/512/447/447147.png");
      width: 20px;
      height: 20px;
      top: 0;
    }
    <div class="outter-wrapper">  
      <div class="wrapper">
        <input class="input" name="checkbox2" id="checkbox2"        type="checkbox">
        <label class="label" for="checkbox2">
          <div class="label-wrapper">
            Option 1Option 1Option 1Option 1Option 1
          </div>  
        </label>
      </div>
    </div>
    
      
     
    
    
      

    【讨论】:

    • 感谢您 - 尽管“勾号”的问题仍然远远超出复选框,但 flex: none 对于防止复选框缩小非常有帮助!
    • 太好了,我看到有人帮你解决了第二个问题。
    • 感谢您的帮助,祝您有美好的一天! :)
    猜你喜欢
    • 2017-07-28
    • 2017-08-05
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 2017-08-08
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多