11

假设以下标记:

<fieldset>
    <legend>Radio Buttons</legend>
    <ol>
        <li>
            <input type="radio" id="x">
            <label for="x"><!-- Insert multi-line markup here --></label>
        </li>
        <li>
            <input type="radio" id="x">
            <label for="x"><!-- Insert multi-line markup here --></label>
        </li>
    </ol>
</fieldset>

如何设置单选按钮标签的样式,使它们在大多数浏览器(IE6+、FF、Safari、Chrome)中看起来如下所示:

单选按钮标签

4

5 回答 5

12

我相信这一切。但是,您没有提到它必须验证,所以我使用了 inline-block (-moz-inline-box) 显示。实际上,我的最爱之一。

这是一份工作副本

在 Safari 3、FireFox 3 和 IE7 中测试。

    <style type="text/css">
ol{
    padding-left: 0;
    margin-left:0;
}

ol>li {
    list-style-type: none;
    margin-bottom: .5em;
}

ol>li input[type=radio] {
    display: -moz-inline-box;
    display: inline-block;
    vertical-align: middle;
}

ol>li label {
    display: -moz-inline-box;
    display: inline-block;
    vertical-align: middle;
}
</style>
于 2009-05-26T20:40:05.523 回答
4

使用以下标记和 css,我能够生成不包含在单选按钮下的多行标签:

<style type="text/css">
    fieldset input, label {
      float: left;
      display: block;
    }

    fieldset li {
      clear: both;
    }
</style>

<fieldset>
  <ol>
    <li>
      <input type="radio" id="x" />
      <label for="x">
        stuff<br/>
        stuff1
      </label>
    </li>
    <li>
      <input type="radio" id="x" />
      <label for="x">
        stuff<br/>
        stuff1
      </label>
    </li>
  </ol>
</fieldset>

但是我无法使用:

fieldset label {
  vertical-align: middle;
}

将标签垂直居中在单选按钮上,即使在应用宽度时(Dmitri Farkov 的回答中的两个建议。我的主要目的是防止在单选按钮下换行,所以这个解决方案暂时可以。

于 2009-05-26T17:15:04.610 回答
3

由于我在上面询问了如何处理非常长的标签,我终于自己解决了。这是我的问题的解决方案。也许它可以帮助你?

<style type="text/css">
  #master_frame { 
      background: #BBB;
      height: 300px;
      width: 300px;
  } 
  fieldset.radios { 
      border: none;
  } 
  fieldset fields { 
      clear: both;
  } 
  input { 
      float: left;
      display: block;
  } 
  label { 
      position: relative;
      margin-left: 30px;
      display: block;
  } 
</style>

<div id="master_frame">
  <fieldset class='radios'>
    <div class='field'>
      <input type="radio" id="a" />
      <label for="a">Short</label>
    </div>
    <div class='field'>
      <input type="radio" id="b" />
      <label for="b">
        A really long and massive text that does not fit on one row!
      </label>
    </div>
  </fieldset>
</div>
于 2010-05-12T10:52:20.227 回答
1

输入和标记两者

float: left;
display: block;

设置标签和输入的宽度。


申请

clear: both;
 vertical-align: middle;

给所有的李。

于 2009-05-26T16:46:52.220 回答
0

您应该white-space: normal;在标签中使用多行

于 2020-09-21T11:16:33.130 回答