简体   繁体   English

HTML对齐挑战

[英]HTML alignment challenge

If I have a stack of textboxes and a button, eg 如果我有一堆文本框和一个按钮,例如

<input type="text" /><br/>
<input type="text" /><br/>
<input type="text" /><br/>
<button>Right align me</button>

How can I right align the right edges of the textboxes and the button without specifying the size of everything? 如何在不指定所有内容大小的情况下右对齐文本框和按钮的右边缘?

Update - perhaps my question wasn't clear. 更新-也许我的问题不清楚。 Some of the answers propose right aligning everything. 一些答案建议正确对齐所有内容。 Which would look like this: 看起来像这样:

右对齐

But I actually want the textboxes to be left aligned, but the button to right align to the edge of those text boxes. 但是我实际上希望文本框保持左对齐,而按钮右对齐则与这些文本框的边缘对齐。 Is there a way to do this? 有没有办法做到这一点?

If you wrap them all in a block element (such as a <div> ), you can specify text-align: right; 如果将它们全部包装在块元素(例如<div> )中,则可以指定text-align: right; on that <div> , and it will align everything to the right, regardless of size. 在该<div> ,无论大小如何,它都会将所有内容对齐到右侧。

Here's an example : 这是一个例子

<div id="container">
    <input type="text" /><br/>
    <input type="text" /><br/>
    <input type="text" /><br/>
    <button>Right align me</button>
</div>
#container{
    text-align: right;
}

Update: 更新:

Based on the changes to your question, would something like this work better? 根据修改你的问题,会像这样工作更好吗?
It aligns the button to the very right of the longest input, and to the bottom. 它将按钮与最长输入的最右端和最底部对齐。 The only disadvantage is that if you have content below the box, you'll need to add some extra margin, so it doesn't overlap with the button. 唯一的缺点是,如果框下方有内容,则需要添加一些额外的边距,因此该边距不会与按钮重叠。

<div id="container">
    <input type="text" /><br/>
    <input type="text" /><br/>
    <input type="text" /><br/>
    <button>Right align me</button>
</div>
#container{
    display: inline-block;
    position: relative;

    /* Add 25px margin to prevent overlap with the button */
    margin-bottom: 25px;
}

button{
    position: absolute;
        top: 100%;
        left: 100%;

    /* Make it look more normal-sized */
    width: 120px;
    height: 25px;
}

Set the widths of your input and button elements to 100%, and use a container to set their actual widths: inputbutton元素的宽度设置为100%,并使用容器设置其实际宽度:

HTML: HTML:

<div id="container">
    <input type="text" />
    <input type="text" />
    <input type="text" />
    <button>Right align me</button>
</div>

CSS: CSS:

#container { width: 200px; }
input { width: 100%; }
button { width: 100%; }

Wrap it in a div and use text-align:right and either float:left or display:inline-block on it. 将其包装在div中,并在其上使用text-align:right和float:left或display:inline-block。 The first one works in ie6. 第一个在ie6中起作用。

Like this: 像这样:

<div style=" text-align: right; float:left;">
     <input type="text" /><br/>
     <input type="text" /><br/>
     <input type="text" /><br/>
     <button>Right align me</button>
</div>

or 要么

<div style=" text-align: right; display: inline:block">
     <input type="text" /><br/>
     <input type="text" /><br/>
     <input type="text" /><br/>
     <button>Right align me</button>
</div>

You can also use a good, old table, it's not recommended though. 您也可以使用一张好的旧桌子,但是不建议这样做。

<table style="text-align:right">
    <tr><td>
         <input type="text" /><br/>
         <input type="text" /><br/>
         <input type="text" /><br/>
         <button>Right align me</button>
    </td></tr>
</table>

Just float the inputs and buttons, while clearing the floats in the br tag. 只需浮动输入和按钮,同时清除br标签中的浮动。

<style>
    input { float: right; }
    br { clear:both;}
    button { float:right;}
</style>

只需float:right指定元素内的元素,就可以将它们右对齐,如本例所示

<div style="float: left;">
    <input type="text" /><br />
    <input type="text" /><br />
    <input type="text" /><br />
    <button style="float: right;">Right align me</button>
</div>
<div style="clear: both;"></div>

Wrap the elements in a div. 将元素包装在div中。 Floating the div left will left-align it and make its size conform to the size of its contents. 左移div将使其左对齐,并使它的大小与内容的大小一致。 Floating the button right will flush the button to the right. 向右浮动按钮会将按钮向右浮动。 The clear div is there in case you want elements you add after this div to be below it, not to its right side. 如果您希望在此div之后添加的元素位于其下方而不是其右侧,则存在clear div。

to accomplish what you are looking for try the following: 要完成您要寻找的内容,请尝试以下操作:

<table>
<tr>
<td>
<input type="text" /><br/>
<input type="text" /><br/>
<input type="text" /><br/>
</td>
<td></td>
</tr>
<tr>
<td></td>
<td><button>Right align me</button></td>
</tr>
</table>

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

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