简体   繁体   English

使用div对齐元素

[英]aligning elements using div

i have my form and it looks like this 我有我的表格,它看起来像这样

<form>
<fieldset>
name:textbox
lastname:textbox
address:textbox
</fieldset>
</form>

and i want my page to align its elements like 我希望我的页面对齐其元素,例如

<form>
<fieldset>
name:      textbox
lastname:  textbox
address:   textbox
</fieldset>
</form>

i am just using divs is there any other way to achieve this other than using tables?i need to align this elements to give my page a better navigation of inputs. 我只是使用div,除了使用表格外,还有其他方法可以实现此目的吗?我需要对齐此元素以使页面更好地导航输入。

Use the table-related CSS display values to style block elements as though they were in a table: 使用与表相关的CSS显示值来对块元素进行样式设置,就像它们在表中一样:

display: table
display: table-row
display: table-cell

This will cause your block elements to behave as though they were <table> , <tr> , and <td> elements respectively. 这将导致您的块元素的行为就像分别是<table><tr><td>元素一样。

Yes, it's possible just to use divs. 是的,可以只使用div。 Also, I'd use <input> so the user can actually type something in for your inputs. 另外,我将使用<input>以便用户可以实际输入内容。

HTML 的HTML

<div>

name: <input type="text" />
lastname:  <input type="text" /> 
address:  <input type="text" /> 

</div>

CSS 的CSS

input { 
  display: block;
}

http://jsfiddle.net/XzSGd/ http://jsfiddle.net/XzSGd/

<form>
<fieldset>
<label>name:</label>      textbox
<label>lastname:</label>  textbox
<label>address:</label>   textbox
</fieldset>
</form>

If you use an additional tag around, you can set the css to be as follows: 如果在周围使用其他标签,则可以将css设置如下:

label {
   display: inline-block;
   width: 100px; //whatever width suits you
}

This way, all the labels will be the same width and, as long as the text is shorter than that, they will make the textboxes align. 这样,所有标签将具有相同的宽度,并且只要文本短于该宽度,它们将使文本框对齐。

You could use classes for the labels. 您可以使用标签类。 See code below 见下面的代码

HTML 的HTML

<form>
<fieldset>
<span class="label">name:</span>   textbox
<span class="label">lastname:</span>  textbox
<span class="label">address:</span>  textbox
</fieldset>
</form>

CSS 的CSS

.label{
display:inline-block;
width:150px;
}

*Change the width according to your need. *根据需要更改宽度。

This is how I would do it. 这就是我要做的。 Float everything. 漂浮一切。 Each textbox/label combo gets its own div, which makes it easy to apply a responsive design framework. 每个文本框/标签组合都有其自己的div,这使应用响应式设计框架变得容易。 Make this as narrow as you want by styling the fieldset. 通过设置字段集的样式使其尽可能窄。 Obviously(?) you would want to include names for each input, and for-attributes for the labels. 显然(?),您希望为每个输入都包括名称,并为标签提供for属性。

http://jsfiddle.net/s3jn3/ http://jsfiddle.net/s3jn3/

input {
    float: right;
    margin-top: 0.5em;
}

label {
    float: left;
}

fieldset>div {
    clear: both;
}

<fieldset>
    <div>
        <label>Item A</label>
        <input type="text" />
    </div>
    <div>
        <label>Item B</label>
        <input type="text" />
    </div>
    <div>
        <label>Item C</label>
        <input type="text" />
    </div>
</fieldset>

Try to put it in table, like so: 尝试将其放在表格中,如下所示:

<form>
<fieldset>
<table>
<tr><td align="left">name:</td>      <td>textbox</td>
<tr><td align="left">lastname:</td> <td> textbox</td>
<tr><td align="left">address:</td>  <td>textbox</td>
</table>
</fieldset>
</form>

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

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