对齐单选按钮时Flexbox问题

时间:2017-06-15 06:53:35

标签: html css css3 flexbox

我正在尝试使用包装器上的flex属性对齐单选按钮和它的标签。但是当标签是多行时,它的单选按钮看起来很高。看一下屏幕截图。enter image description here

这是我的html / css



.input-answer-wrapper{}
    .input-answer-wrapper .input-radio-item .radio-label{
        font-size: 12pt;
        color: #36464E;
        display: flex;
    }
    .input-answer-wrapper .input-radio-item .radio-label p{
        display: inline-block;
    }
    .input-answer-wrapper .input-radio-item .radio-label .label-text{
        margin-left: 10px;
    }
    .mg-radio{
      line-height: 16px;
      min-width: 20%;
      padding: 10px 10px 5px 10px;
      cursor: pointer;
    }

    .mg-radio! > input[type=radio] { display: none; }

    .mg-radio input[type="radio"] {
      position: absolute;
      opacity: 0;
    }
    .mg-radio input[type="radio"] + .radio-label:before {
      content: '';
      background: #f4f4f4;
      border-radius: 100%;
      border: 1px solid #b4b4b4;
      display: inline-block;
      width: 1.4em;
      height: 1.4em;
      position: relative;
      top: -0.2em;
      margin-right: 1em;
      vertical-align: middle;
      cursor: pointer;
      text-align: center;
      -webkit-transition: all 250ms ease;
      transition: all 250ms ease;
    }
    .mg-radio input[type="radio"]:checked + .radio-label:before {
      background-color: #64B3F5;
      box-shadow: inset 0 0 0 4px #f4f4f4;
    }
    .mg-radio input[type="radio"]:focus + .radio-label:before {
      outline: none;
      border-color: #3197EE;
    }
    .mg-radio input[type="radio"]:disabled + .radio-label:before {
      box-shadow: inset 0 0 0 4px #f4f4f4;
      border-color: #b4b4b4;
      background: #b4b4b4;
    }
    .mg-radio input[type="radio"] + .radio-label:empty:before {
      margin-right: 0;
    }

<div class="input-answer-wrapper">
    <div class="input-radio">
        <div class="input-radio-item" ng-repeat="pa in question.possible_answers">
            <label class="mg-radio">
                <input type="radio" />
                <span class="radio-label">Lorem ipsum..Lorem ipsum..Lorem ipsum..Lorem ipsum..Lorem ipsum..Lorem ipsum..Lorem ipsum..Lorem ipsum..</span>
            </label>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:5)

这里发生的是当文本变得更长并且需要断行时,它首先尽可能地扩展,并且因为flex-shrink的默认值是1而伪实际上没有任何内容,它会缩小。

为避免这种情况,请将flex-shrink: 0添加到其CSS规则

.mg-radio input[type="radio"] + .radio-label:before {
  content: '';
  flex-shrink: 0;
  background: #f4f4f4;
  border-radius: 100%;
  border: 1px solid #b4b4b4;
  display: inline-block;
  width: 1.4em;
  height: 1.4em;
  position: relative;
  top: -0.2em;
  margin-right: 1em;
  vertical-align: middle;
  cursor: pointer;
  text-align: center;
  -webkit-transition: all 250ms ease;
  transition: all 250ms ease;
}

Updated fiddle

由于min-width默认为auto,您也可以将其设置为与宽度相同

.mg-radio input[type="radio"] + .radio-label:before {
  content: '';
  background: #f4f4f4;
  border-radius: 100%;
  border: 1px solid #b4b4b4;
  display: inline-block;
  width: 1.4em;
  min-width: 1.4em;                     /*  added  */
  ...

Updated fiddle 2

答案 1 :(得分:0)

指定输入的宽度。

&#13;
&#13;
.mg-radio{
display: flex;
}
.mg-radio input{
min-width: 30px;
height: 16px;
margin: 0 10px 0 0;
}
&#13;
<label class="mg-radio">
    <input type="radio"  >
    <span class="radio-label" >Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate maxime debitis commodi totam, quia perspiciatis vel blanditiis iste sed tempore adipisci illum aperiam, obcaecati voluptatum mollitia natus at, neque nihil.</span>
  </label>
&#13;
&#13;
&#13;