2024-03-20 10:00:03

将按钮右对齐

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

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

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


当前回答

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

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

其他回答

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

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

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

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

2019年使用弹性盒的现代方法

带div标签

< div风格= "显示:flex;justify-content: flex-end;宽度:100%;填充:0;" > <input type="button" value="Click Me"/> < / div >

带跨度标签

<跨风格= "显示:flex;justify-content: flex-end;宽度:100%;填充:0;" > <input type="button" value="Click Me"/> < / span >

另一种可能是使用一个向右的绝对定位。你可以这样做:

style="position: absolute; right: 0;"

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

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

保持按钮在页面流中:

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

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