对齐单选按钮CSS问题

用户名

我试图将我的自定义单选按钮排列成一行,但是出现问题。如果文本确实很长,但单选按钮彼此重叠。我不想为其中任何一个分配固定宽度。另外,.radio .custom如果要更改单选按钮的边框以进行错误处理,则需要在其中进行样式设置。我愿意更改html结构,但我需要Text在customspan内。如果需要,我可以摆脱内部跨度。

我试过使用:

display:flex;

label

但这最终给了我彼此之间的每个选择权。

* {
  box-sizing: border-box;
}
input[type="radio"] {
  display: none;
  cursor: pointer;
}
label {
  cursor: pointer;
  min-width: 50px;
  display: inline-block;
  position: relative;
}
.radio span.custom > span {
  margin-left: 24px;
  margin-right: 10px;
}
.radio .custom {
  content: '';
  height: 20px;
  left: 0;
  top: 0;
  width: 20px;
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 100%;
  position: absolute;
}
.radio input:checked + .custom:before {
  background-color: blue;
  border-radius: 100%;
  border: 3px solid #fff;
  content: "";
  display: block;
  height: 12px;
  position: absolute;
  top: 0;
  width: 12px;
  z-index: 1;
  left: 0;
}
.radio input + .custom:after {
  border-radius: 100%;
}
.checkbox input:checked .custom {
  background-color: blue;
  border-color: blue;
}
<label class="radio">
  <input type="radio">
  <span class="custom"><span>Onasasasase</span></span>
</label>
<label class="radio">
  <input type="radio">
  <span class="custom"><span>Two</span></span>
</label>

JSFiddle演示

马克·奥德(Marc Audet)

这是一种方法。我稍微简化了HTML(删除了多余的nested span),并使用了以下CSS。

您的自定义按钮为18px正方形,因此我在child元素上留出了25px的边距span.custom

这为:before出现在左侧伪元素留出了空间.custom

我为上的背景加上了颜色,label以便您可以看到未选中的按钮,但是否则,您可以通过标记来对边框,间距等进行很多控制。

input[type="radio"] {
  cursor: pointer;
  display: none;
}

label {
  cursor: pointer;
  min-width: 50px;
  display:inline-block;
  position:relative;
  background-color: #F0F0F0; // for demo only
}

.radio .custom {
  margin-left: 25px;
  margin-right: 25px;
}

.radio input + .custom:before {
    content: "";
    background-color: #fff;
    border-radius: 100%;
    border: 3px solid #fff;
    display: block;
    height: 12px;
    width: 12px;
    position: absolute;
    top: 0;
    left: 0;
}

.radio input:checked + .custom:before {
    background-color: blue;
    border: 3px solid #fff;
}
<label class="radio">
  <input type="radio">
  <span class="custom">One is very long</span>
</label>

<label class="radio">
  <input type="radio">
  <span class="custom">Two</span>
</label>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章