html - 如何使用 flexbox 使第一个元素包裹在其他元素之上?

标签 html css flexbox

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

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>

现在包装时的样子: enter image description here

我希望它在包装时看起来如何: enter image description here

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

编辑:我希望它看起来像默认情况下在片段中所做的那样(标签水平排列)。我只希望它在窗口缩小时用第一个标签包裹自己的行,并在下面包裹其他两个标签。

最佳答案

请检查以下代码。谢谢

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>

关于html - 如何使用 flexbox 使第一个元素包裹在其他元素之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49410477/

相关文章:

html - CSS Flexbox 组 2 flex 元素

css - 输入看起来比选择小但具有相同的填充

html - 如何在 flexbox 中将文本环绕在对象周围(就像 float 一样)?

html - 水平滚动 CSS 缩略图

javascript - 有没有办法通过 Jquery 访问 Css 样式

html - "normal"CSS 到 Bootstrap 3

javascript - 将 HTML <select> 元素显示到特定的 div

javascript - $interval 不允许我更新我的前端

html - Bootstrap 中有多少数据属性?他们做什么以及他们的可能值(value)是什么?

jquery - Firefox:将文件上传对话框限制为仅 mp3