html - div 中动态居中对齐按钮?

标签 html css

我有一个 div,它可以根据不同的场景有 2 个或 4 个按钮。我希望 div 内的按钮居中对齐。怎么做到的。我已经在下面提交了我的代码。

<div style="margin: 0 auto; width: 656px;">
    <input type="submit" style="float:left;"  value="Prev"/><input type="reset"  value="Reset" style="float:left;"/>
   <input type="submit"  value="Submit" style="float:left;"/><input style="float:left;" type="submit"  value="Close"/></div>

问题是它在显示四个按钮时有效,但不适用于 3,2 和 1 个按钮的情况。如何在不指定宽度的情况下居中对齐?请帮忙。

最佳答案

您需要在容器元素上使用 text-align: center。此外,避免使用内联 样式...

<div style="margin: 0 auto; width: 656px; text-align: center;">

Demo

Note: Make sure that you use text-align: left; if you have any text inside that div else they will be centered as well.


正如你评论的那样,它看起来像是我的中心

enter image description here


您仍然不相信它们是完美居中的,我会调整容器 width 并向您展示它们是居中的....

enter image description here

enter image description here

有 2 个按钮

enter image description here

enter image description here

Note: Make sure you use CSS Reset as well... So that you get consistent styles in all browsers though this should be centered regardless of resetting the styles.

关于html - div 中动态居中对齐按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21722432/

相关文章:

javascript - 如何显示动态数据下拉列表中的选择选项

html - 元素底部的多个框阴影相互分离

javascript - 为什么当 jquery NiceSelect 展开时我的自定义滚动条不更新?

html - 一个 div 中的多个背景图像

html - 虚幻引擎 4 如何将我的游戏发布到网站?

javascript - 打开选项卡的计数 - Jquery

html - Bootstrap 网格留下空列

html - 使整个列表区域可点击

html - 如何将未知/动态高度的图像置于未知/动态高度的 float div 中?

javascript - 第二次访问用户后用 cookie 隐藏 div