【问题标题】:How to make first items wrap on top of other items using flexbox?如何使用 flexbox 使第一个项目包裹在其他项目之上?
【发布时间】:2018-03-22 00:09:38
【问题描述】:

我正在制作一些单选按钮。这几乎就是我想要的。

input[type=radio] {
  position: absolute;
  visibility: hidden;
  display: none;
}

label {
  flex: 1 0 auto;
  background-color: white;
  text-align: center;
  color: dimgray;
  cursor: pointer;
  font-weight: bold;
  user-select: none;
  padding: 5px;
  width: 60px;
  transition: background 0.2s, color 0.2s;
}

input[type=radio]:checked + label{
  color: White;
  background: dimgrey;
  
  &:hover {
    color: White;
    background: dimgrey;
  }
}

input[type=radio] + label:hover {
  color: white;
  background-color: lightgrey;
}

label + input[type=radio] + label {
  border-left: 0.5px solid lightgrey;
}

.radio-group {
  display: flex;
  flex-wrap: wrap;
  max-width: max-content;
  align-items: stretch;
  border: 0.5px solid dimgrey;
}
<form>
  <div class="radio-group flexbox">
    <input type="radio" id="alle" value="alle" name="selector" checked><label for="alle">Alle</label
    ><input type="radio" id="aktiv" value="aktiv" name="selector" ><label for="aktiv">Aktive</label
    ><input type="radio" id="inaktiv" value="inaktiv" name="selector"><label for="inaktiv">Inaktive</label>
  </div>
</form>

现在包装时的样子:

我希望它在包装时的外观:

我怎样才能做到这一点?我尝试调整 flex-wrap、方向、流等。无法让它按我想要的方式工作。

编辑:我希望它看起来像默认情况下在 sn-p 中的样子(标签水平排列)。我只是希望它在窗口缩小时用第一个标签包裹它自己的行和下面的另外两个标签。

【问题讨论】:

  • width:100% 到该项目并启用 flex 容器上的换行
  • 旁注,您不需要在 flexbox 中使用 &lt; ...wrap &gt; 技术。

标签: html css flexbox


【解决方案1】:

请检查以下代码。谢谢你

input[type=radio] {
  position: absolute;
  visibility: hidden;
  display: none;
}

label {
  flex: 1 0 auto;
  background-color: white;
  text-align: center;
  color: dimgray;
  cursor: pointer;
  font-weight: bold;
  user-select: none;
  padding: 5px;
  width: 60px;
  transition: background 0.2s, color 0.2s;
}

input[type=radio]:checked + label{
  color: White;
  background: dimgrey;
  
  &:hover {
    color: White;
    background: dimgrey;
  }
}

input[type=radio] + label:hover {
  color: white;
  background-color: lightgrey;
}

label + input[type=radio] + label {
  border-left: 0.5px solid lightgrey;
}

.radio-group {
  display: flex;
  flex-wrap: wrap;
  max-width: max-content;
  align-items: stretch;
  border: 0.5px solid dimgrey;
}

.radio-group.flexbox > label:nth-child(2) {
  width: calc( 100% - 12px ) !IMPORTANT;
}
<form>
  <div class="radio-group flexbox">
    <input type="radio" id="alle" value="alle" name="selector" checked><label for="alle">Alle</label
    ><input type="radio" id="aktiv" value="aktiv" name="selector" ><label for="aktiv">Aktive</label
    ><input type="radio" id="inaktiv" value="inaktiv" name="selector"><label for="inaktiv">Inaktive</label>
  </div>
</form>

【讨论】:

    猜你喜欢
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2012-09-14
    • 2022-08-23
    • 2021-06-14
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多