收音机按钮内联和中心标签

时间:2011-02-17 20:16:29

标签: css forms button inline radio

我有以下表格:

        <form action="post.php" method="POST">
            <fieldset>
                 <div class="ratingClass">
                      <input type="radio" class="radio" name="rate" value="1" id="1"/>
                      <label for="1">1</label>
                      <input type="radio" class="radio" name="rate" value="2" id="2"/>
                      <label for="2">2</label>
                      <input type="radio" class="radio" name="rate" value="3" id="3"/>
                      <label for="3">3</label> 
                      <input type="radio" class="radio" name="rate" value="4" id="4"/>
                      <label for="4">4</label>
                      <input type="radio" class="radio" name="rate" value="5" id="5"/>
                      <label for="5">5</label>                                                                
                 </div>
            </fieldset>
            <input type="submit" value="Rate">
        </form>

由以下CSS设计:

fieldset { 
 overflow:hidden; 
}
.ratingClass { 
 float:left; 
 clear:none;
}
label { 
 float:left; 
 clear:none; 
 display:block; 
 padding: 2px 1em 0 0; 
}
input[type=radio], input.radio { 
 float:left;
 clear:none; 
 margin: 2px 0 0 2px; 
}

所有内容都在另一个具有text-align: center;样式的div中。

我意识到这种行为是因为浮动,但如果我删除它们,那么单选按钮就不再显示内联。

如何让它们内联并居中?

3 个答案:

答案 0 :(得分:3)

您不需要浮动所有内容,也不需要使标签块元素。用这个替换你的CSS会使一切都集中在一起:

fieldset {
  overflow: hidden;
}
label {
  padding: 2px 1em 0 0;
}
input[type=radio], input.radio {
  margin: 2px 0 0 2px;
}

<div class="ratingClass">也是多余的,可以删除。

答案 1 :(得分:0)

尝试制作.ratingClass容器:

.ratingClass { 
 float:left; 
 clear:none;
 width: 100%;
 text-align: center; 
}

.ratingClass { 
 float:none;
 text-align: center; 
}

答案 2 :(得分:0)

如果你可以处理ratingClass div的固定宽度,你可以按照以下方式处理......

.ratingClass { 
    width:300px; /* insert desired width here */
    margin:auto;
}
相关问题