2024-03-20 10:00:03

将按钮右对齐

我使用这个代码来右对齐一个按钮。

< p align =“正确”> <input type="button" value="Click Me" /> < / p >

但是<p>标签浪费了一些空间,所以考虑对<span>或<div>做同样的事情。


当前回答

如果按钮是块上唯一的元素:

.border { 边框:2px蓝色虚线; } .mr-0 { margin-right: 0; } .ml-auto { margin-left:汽车; } .d-block { 显示:块; } < p class = "边境”> <input type="button" class="d-block mr-0 ml-auto" value="The button" > < / p >

如果有其他元素在块上:

.border { 边界:2px靛蓝色虚线; } .d-table { 显示:表; } .d-table-cell { 显示:表格单元; } .w - 100 { 宽度:100%; } . tar { text-align:正确; } <div class="border d-table w-100"> <p class="d-table-cell">段落.....lorem ipsum…等。< / p > <div class="d-table-cell tar"> <button >按钮</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>

祝你好运…

其他回答

保持按钮在页面流中:

<input type="button" value="Click Me" style="margin-left: auto; display: block;" />

(将该样式放在.css文件中,不要使用这个HTML内联,以便更好地维护)

< span style = " font - family:宋体;justify-content: flex-end”> < >按钮点击我!< /按钮> < / div >

<div style = "display: flex; justify-content: flex-end">
    <button>Click me!</button>
</div>

这个解决方案依赖于Bootstrap 3,如@günther-jena所指出的

Try <a class="btn text-right">Call to Action</a>. "这样就不需要额外的标记或规则来清除浮动元素。

你使用哪种对齐技术取决于你的情况,但基本的是float: right;:

<input type="button" value="Click Me" style="float: right;">

你可能会想清除你的浮动,但这可以通过overflow:隐藏在父容器或显式<div style="clear: both;"></div>在容器底部。

例如:http://jsfiddle.net/ambiguous/8UvVg/

浮动元素被从正常的文档流中移除,因此它们可以溢出父元素的边界并弄乱父元素的高度,clear:both CSS会处理这个问题(overflow:hidden也是如此)。摆弄一下我添加的JSFiddle示例,看看浮动和清除是如何表现的(不过你会想先删除overflow:hidden)。

另一种可能是使用一个向右的绝对定位:

<input type="button" value="Click Me" style="position: absolute; right: 0;">

这里有一个例子:https://jsfiddle.net/a2Ld1xse/

这种解决方案有其缺点,但在某些用例中它非常有用。