CSS - 如何对齐我的单选按钮?

时间:2016-05-30 06:08:30

标签: html css

这是我的HTML表单代码:

<div id="myForm">
        <form action="<?php echo $__SERVER["PHP_SELF"];?>" method="post">
            <table>
                <input type="radio" name="tableNames" value="<?php echo $tempName; ?>" > <?php echo $tempName ?> <br/>

                <tr> 
                    <td><input type="submit" value="submit"></td>
                    <td><input type="reset" value="reset"></td>
                </tr>
            </table>
        </form>
    </div>

这是我目前的css:

#myForm {
    margin: auto;
    width: 700px;
}
#myForm form {
    background-color: #E0E0E0;
    text-align: center;
    padding: 5px;
}
#myForm table {
    margin: auto;
    width: 60%;
    padding: 15px;
}

这就是我的表单现在的样子: radio buttons 如何对齐单选按钮的按钮

6 个答案:

答案 0 :(得分:2)

你可以将输入包装在一个div中,然后将该div居中并给它文本左对齐。

#myForm {
  margin: auto;
  width: 700px;
}
#myForm form {
  background-color: #E0E0E0;
  text-align: center;
  padding: 5px;
}
#myForm table {
  margin: auto;
  width: 60%;
  padding: 15px;
}
.test {
  width: 300px;
  margin: 0 auto;
  text-align: left;
}
<div id="myForm">
  <form method="post">
    <table>
      <div class="test">
        <input type="radio" name="tableNames" value="asdasd123123">asdasdasd
        <br/>
        <input type="radio" name="tableNames" value="asd">asdasdasd
        <br/>

        <input type="radio" name="tableNames" value="asdasdasdas?>">123
        <br/>
        <input type="radio" name="tableNames" value="123123123123">asdasdasd1231231231231231231231
        <br/>
      </div>
      <tr>
        <td>
          <input type="submit" value="submit">
        </td>
        <td>
          <input type="reset" value="reset">
        </td>
      </tr>
    </table>
  </form>
</div>

答案 1 :(得分:1)

使用此css

------------------------  

 #myForm form {
    background-color: #E0E0E0;
    text-align: left;
    padding: 5px;
}

答案 2 :(得分:0)

我没有看到生成多个无线电盒的位置,但如果您想继续使用表格来组织内容,我建议将所有这些无线电盒放在同一个TD中。

否则,您可以在已有的<div>内放置第二个<div>标记,并为此内部<div>的宽度,填充,边距设置自定义尺寸。

答案 3 :(得分:0)

label,input{
  float:left;
  }
<div id="myForm">
  <form action="" method="post">
    <table>
      <input type="radio" name="tableNames1" value="1"><label>1</label>
      <input type="radio" name="tableNames2" value="2"><label>2</label>
      <input type="radio" name="tableNames3" value="3"><label>3</label>
      <input type="radio" name="tableNames4" value="4"><label>4</label>

      <br/>

      <tr>
        <td>
          <input type="submit" value="submit">
        </td>
        <td>
          <input type="reset" value="reset">
        </td>
      </tr>
    </table>
  </form>
</div>

答案 4 :(得分:0)

这会有点混乱,但我认为我找到了正确的解决方案。

像这样:JS Fiddle

#myForm {
    margin: auto;
    width: 700px;
}
#myForm form {
    background-color: #E0E0E0;
    padding: 5px;
}
#myForm table {
    margin: auto;
    width: 60%;
    padding: 15px;
    text-align: center;
}
.radio_holder{
  width: 45%;
  display:inline-block;
  
}
.radio_label_holder {
  display:block; 
  text-align: right;
}
label{
  width:53%;
  display:inline-block;
  text-align:left;
}
.all_radio{
  display:block;
  margin:auto;
  width:60%;
}
<div id="myForm">
        <form>
            <table>
            <span class="all_radio">
            <span class="radio_label_holder"><span class="radio_holder"><input type="radio" id="administrators" name="tableNames" value="administrators" ></span> <label for="administrators">administrators</label></span>
            
            <span class="radio_label_holder"><span class="radio_holder"><input type="radio" id="bookings" name="tableNames" value="bookings" ></span> <label for="bookings">bookings</label></span>
                
            <span class="radio_label_holder"><span class="radio_holder"><input type="radio" id="rooms" name="tableNames" value="rooms" ></span> <label for="rooms">rooms</label></span>
               
            <span class="radio_label_holder"><span class="radio_holder"><input type="radio" id="customers" name="tableNames" value="customers" ></span> <label for="customers">customers</label></span>
            </span>  

                <tr> 
                    <td><input type="submit" value="submit"></td>
                    <td><input type="reset" value="reset"></td>
                </tr>
            </table>
        </form>
    </div>

答案 5 :(得分:-1)

您最好将单选按钮放在<td>...</td>

<input type="radio" name="tableNames" value="<?php echo $tempName; ?>" > <?php echo $tempName ?> <br/>

成为:

<td colspan='2' align='left'><input type="radio" name="tableNames" value="<?php echo $tempName; ?>" > <?php echo $tempName ?></td>

它正在与html对齐。