简体   繁体   English

CSS - 如何对齐单选按钮?

[英]CSS - How can I align my radio buttons?

This is my HTML form code:这是我的 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>

this is my current css:这是我目前的 css:

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

this is what my form looks like right now:这就是我的表单现在的样子: 单选按钮 How can i align the radio button's buttons我如何对齐单选按钮的按钮

you could wrap the inputs in a div, then center that div and give it text-align left.您可以将输入包装在一个 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>

Use this css使用这个css

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

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

I'm not seeing where the multiple radio boxes are being generated, but I suggest placing all of them within the same TD if you want to continue to use a table to organize content.我没有看到生成多个单选框的位置,但如果您想继续使用表格来组织内容,我建议将它们全部放在同一个 TD 中。

Otherwise, you could place a second <div> tag inside the <div> you already have, and set custom dimensions for the width, padding, margin of this inner <div> .否则,您可以在已有的<div>放置第二个<div>标签,并为这个内部<div>的宽度、内边距和边距设置自定义尺寸。

 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>

It's gonna be a bit messy, but I've think I found the right solution.这会有点乱,但我想我找到了正确的解决方案。

Something like this: JS Fiddle像这样: 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>

You better place the radio button in <td>...</td>你最好把单选按钮放在<td>...</td>

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

become:变得:

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

It's aligning with the html.它与 html 对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM