简体   繁体   English

如何使这些左/右对齐的标签和表格输入居中?

[英]How do I page center these left/right aligned label and form inputs?

I have this set (JsFiddle link) of labels and text inputs. 我有这组标签和文本输入(JsFiddle链接)。

How do I center the whole thing in the middle of the page? 如何将整个内容放在页面中间?

I tried wrapping them in a div and setting it's alignment to cetner - didn't do what i expected at all. 我尝试将它们包装在div中并将其对齐方式设置为cetner-完全没有达到我的期望。

Any help is appreciated, than you. 任何帮助都比您感激。

Code for reference: 参考代码:

<div>
<div class="left">
    label
</div>
<div class="right">
    input element
</div>
</div>



<div>
<div class="left">
    another label
</div>
<div class="right">
    another input element
</div>
</div>

//align the labels and input nicely

.left {
    width: 20%;
    float: left;
    text-align: right;
}
.right {
    width: 65%;
    margin-left: 10px;
    float:left;
}

If you're going to use float, than you need to wrap the whole thing in a DIV and apply margin: 0 auto; 如果要使用float,则需要将整个内容包装在DIV中并应用margin: 0 auto;


I'd do this in this case: 在这种情况下,我会这样做:

<style>
.field {
    width: 80%;
    margin: 0 auto; 
}
.left {
    width: 20%;
    text-align: right;
    display: block;
    float: left;
    margin-right: 5%;
}
</style>

<div class="field">
   <label class="left">label</label>
   <input type="text">
</div>

<div class="field">
   <label class="left">another label</label>
   <input type="text">
</div>

wrap the whole thing in a div and set 将整个内容包装在div中并设置

         margin: auto;

also set a width, if you want to use the text-align: center; 如果您想使用text-align: center;还要设置一个宽度text-align: center; method , that should be applied to the parent pf the div to be centered. 方法,应将其应用于要居中的div的父对象。

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

相关问题 如何在div的中心向左对齐文本? - How do I have left-aligned text in the center of a div? 如何将Outlook电子邮件中的左对齐文本居中? - How do I Center Left-aligned Text in an Outlook Email? 我的标签页向左对齐...如何将它们移到中心? - My tabs are aligned left…how do I moved them to the center? 如何将左对齐图像的div块居中? - How do I center a div block of left aligned images? 如何垂直居中 li 导航菜单但将其水平对齐到右侧,同时保持徽标图标水平对齐左和 ctrd vert? - How do I vertically center li navigation menu but horizontally align it to the right while keeping logo icon horizontally aligned left and ctrd vert? 左,右和中心对齐的div - Left, right and center aligned divs 如何将此表格的标签和输入居中? - How do I center the label and input of this form? 如何创建一个表,其TD左对齐,其兄弟姐妹右对齐? - How do I create a table with a TD aligned to the left and its sibling aligned to the right? 如何将 dd 和 dt 元素设置在同一行,dt 右对齐和 dd 左对齐? - How do I style dd and dt elements to be on the same line with dt right aligned and dd left aligned? 如何使两个对象水平对齐(一个在另一个的右侧)并居中放置一个? - How can I have two objects aligned horizontally (one to the right of the other) and center the left one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM