我使用这段代码将一个按钮右对齐。
<p align="right">
<input type="button" value="Click Me" />
</p>
但是使用 <p>
标签会浪费一些空间,所以希望尝试使用 <span>
或者 <div>
来实现同样的效果。
你采用哪种对齐技术取决于你的情况,但基本的一种是 float: right;
:
<input type="button" value="Click Me" style="float: right;">
你可能需要清除浮动,可以在父容器上使用 overflow:hidden
或在容器底部添加一个显式的 <div style="clear: both;"></div>
。
例如: http://jsfiddle.net/ambiguous/8UvVg/
浮动元素会被从正常文档流中移除,因此它们可以溢出其父元素边界并破坏父元素的高度,clear:both
CSS 可以解决这个问题(overflow:hidden
也可以)。可以玩一下我添加到 JSFiddle 的例子,了解浮动和清除是如何行为的(不过首先需要删除 overflow:hidden
)。
元素
:.border {
border: 2px blue dashed;
}
.mr-0 {
margin-right: 0;
}
.ml-auto {
margin-left:auto;
}
.d-block {
display:block;
}
<p class="border">
<input type="button" class="d-block mr-0 ml-auto" value="The Button">
</p>
块
上有其他的元素
:.border {
border: 2px indigo dashed;
}
.d-table {
display:table;
}
.d-table-cell {
display:table-cell;
}
.w-100 {
width: 100%;
}
.tar {
text-align: right;
}
<div class="border d-table w-100">
<p class="d-table-cell">The paragraph.....lorem ipsum...etc.</p>
<div class="d-table-cell tar">
<button >The Button</button>
</div>
</div>
flex-box
:.flex-box {
display:flex;
justify-content:space-between;
outline: 2px dashed blue;
}
.flex-box-2 {
display:flex;
justify-content: flex-end;
outline: 2px deeppink dashed;
}
<h1>Button with Text</h1>
<div class="flex-box">
<p>Once upon a time in a ...</p>
<button>Read More...</button>
</div>
<h1>Only Button</h1>
<div class="flex-box-2">
<button>The Button</button>
</div>
<h1>Multiple Buttons</h1>
<div class="flex-box-2">
<button>Button 1</button>
<button>Button 2</button>
</div>
祝你好运...
margin-left:auto
真是太好了!但是 !important
是最糟糕的,它会在未来引起问题。 - The Studentdisplay: flex;
更新了答案。它会处理一切。 - Aakash另一个可能的选择是使用相对于右侧定位的绝对定位:
<input type="button" value="Click Me" style="position: absolute; right: 0;">
这里有一个例子:https://jsfiddle.net/a2Ld1xse/
这种解决方案有其缺点,但在某些情况下非常有用。
<div style = "display: flex; justify-content:flex-end">
<button>Click me!</button>
</div>
<div style = "display: flex; justify-content: flex-end">
<button>Click me!</button>
</div>
使用 flex-box 的 2019 年现代方法
使用 div 标签
<div style="display:flex; justify-content:flex-end; width:100%; padding:0;">
<input type="button" value="Click Me"/>
</div>
带有span标签的内容
<span style="display:flex; justify-content:flex-end; width:100%; padding:0;">
<input type="button" value="Click Me"/>
</span>
正如@günther-jena所指出的,此解决方案依赖于Bootstrap 3。
尝试使用<a class="btn text-right">Call to Action</a>
。这样你就不需要额外的标记或规则来清除浮动元素。
<input type="button" value="Click Me" style="margin-left: auto; display: block;" />
这并不总是那么简单,有时候对齐方式必须在容器中定义而不是在按钮元素本身定义!
针对您的情况,解决方案应该是:
<div style="text-align:right; width:100%; padding:0;">
<input type="button" value="Click Me"/>
</div>
width=100%
,以确保<div>
占据其容器的全部宽度。padding:0
,以避免出现与<p>
元素一样的前后空格。<div>
定义在FSF/Primefaces表格的页脚中,则float:right
无法正确工作,因为按钮的高度会比页脚的高度还要高!text-align:right
。<div style="text-align:right; width:100%; padding:0;">
<input type="button" value="Click Me 1"/>
<input type="button" value="Click Me 2"/>
<input type="button" value="Click Me 3"/>
<input type="button" value="Click Me 4"/>
<input type="button" value="Click Me 5"/>
<input type="button" value="Click Me 6"/>
</div>
另一个可能性是使用绝对定位,相对于右侧定位。您可以这样做:
style="position: absolute; right: 0;"
div
,这将强制忽略其限制。 - Hassan Baig在我的情况下
<p align="right"/>
运行良好