【问题标题】:Align radio buttons css issue对齐单选按钮css问题
【发布时间】:2017-01-06 03:15:05
【问题描述】:

我正在尝试将我的自定义单选按钮对齐成一行,但我遇到了问题。如果文本很长但单选按钮相互重叠。我不想为它们中的任何一个分配固定宽度。此外,如果我想更改单选按钮的边框以进行错误处理,我需要在那里设置.radio .custom 样式。我愿意更改 html 结构,但我需要将 Text 放在 custom 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

【问题讨论】:

    标签: html css styles radio


    【解决方案1】:

    我只是将所有内容都包装在一个父 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>
    

    【讨论】:

      【解决方案2】:

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

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

      这为出现在.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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-05
        • 2013-03-09
        • 1970-01-01
        • 1970-01-01
        • 2011-07-31
        • 2017-11-17
        • 2015-09-30
        • 1970-01-01
        相关资源
        最近更新 更多