对齐单选按钮 css 问题

Align radio buttons css issue

我正在尝试将我的自定义单选按钮排成一行,但我遇到了问题。如果文本真的很长但是单选按钮相互重叠。我不想为它们中的任何一个分配固定宽度。此外,如果我想更改单选按钮的边框以进行错误处理,我需要在那里设置 .radio .custom 样式。我愿意更改 html 结构,但我需要文本位于 custom 范围内。如果需要,我可以去掉 inner-span。

我试过使用:

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 Demo

我只是将所有内容都包裹在父级 div 中并使其成为一个 flexbox,同时将其居中。似乎对我有用。

<div style="display: flex; justify-content: space-around">
  <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>
</div>

这是一种方法。我稍微简化了您的 HTML(删除了额外的嵌套 span)并使用了以下 CSS.

您的自定义按钮是 18px 的正方形,所以我在子元素上留出了 25px 的边距 span.custom

这允许 space 出现在 .custom 左侧的 :before 伪元素。

我为 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>