如何让按钮在div中居中?
我有一个宽度为 100% 的 div。
我想在其中放置一个按钮,我该怎么做?
<div style="width:100%; height:100%; border: 1px solid">
<button type="button">hello</button>
</div>
I have a div that's width is 100%.
I'd like to center a button within it, how might I do this?
<div style="width:100%; height:100%; border: 1px solid">
<button type="button">hello</button>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
更新答案
更新是因为我注意到这是一个活跃的答案,但是 Flexbox 现在是正确的方法。
现场演示
垂直和水平对齐。
只是水平(只要主弹性轴是水平的,这是默认的)
使用固定宽度且没有弹性框的原始答案
如果原始海报想要垂直和居中对齐,那么对于固定宽度和高度来说非常容易按钮,尝试以下
实时演示
CSS< /strong>
仅水平对齐使用
或
Updated Answer
Updating because I noticed it's an active answer, however Flexbox would be the correct approach now.
Live Demo
Vertical and horizontal alignment.
Just horizontal (as long as the main flex axis is horizontal which is default)
Original Answer using a fixed width and no flexbox
If the original poster wants vertical and center alignment its quite easy for fixed width and height of the button, try the following
Live Demo
CSS
for just horizontal alignment use either
or
你可以做:
或者你也可以这样做:
第一个将 div 内的所有内容居中对齐。另一个将仅将按钮居中对齐。
You could just make:
Or you could do it like this instead:
The first one will center align everything inside the div. The other one will center align just the button.
et voila:
更新:如果OP正在寻找水平和垂直中心,这个答案就可以了它用于固定宽度/高度的元素。
et voila:
Update: If OP is looking for horizontal and vertical centre, this answer will do it for a fixed width/height element.
保证金:0 自动;是仅水平居中的正确答案。
对于像这样的居中方式,使用jquery:
更新...五年后,可以在父 DIV 元素上使用 flexbox 轻松地将按钮居中水平和垂直。
包括所有浏览器前缀,以获得最佳支持
Margin: 0 auto; is the correct answer for horizontal centering only.
For centering both ways something like this will work, using jquery:
Update ... five years later, one could use flexbox on the parent DIV element to easily center the button both horizontally and vertically.
Including all browser prefixes, for best support
由于提供的详细信息有限,我将假设最简单的情况,并说您可以使用
text-align: center
:With the limited detail provided, I will assume the most simple situation and say you can use
text-align: center
:使用 flexbox
然后将类添加到您的按钮中。
Using flexbox
And then adding the class to your button.
您应该简单一点:
首先您有文本或按钮的初始位置:
通过将此 css 代码行添加到 h2 标记或div 包含按钮标签的标签
最终结果代码将是:
< img src="https://i.sstatic.net/YmCJD.png" alt="在此处输入图像描述">
You should take it simple here you go :
first you have the initial position of your text or button :
By adding this css code line to the h2 tag or to the div tag that holds the button tag
Finaly The result code will be :
要将
To center a
<button type = "button">
both vertically and horizontally within a<div>
which width is computed dynamically like in your case, this is what to do:text-align: center;
to the wrapping<div>
: this will center the button whenever you resize the<div>
(or rather the window)For the vertical alignment, you will need to set
margin: valuepx;
for the button. This is the rule on how to calculatevaluepx
:valuepx = (wrappingDIVheight - buttonHeight)/2
Here is a JS Bin demo.
假设div是#div,button是#button:
然后像往常一样将按钮嵌套到div中。
Supposing div is #div and button is #button:
Then nest the button into div as usual.
最简单的事情是将其输入为“div”,给它一个“margin:0 auto”,但如果你希望它居中,你需要给它一个宽度
Easiest thing is input it as a "div" give it a "margin:0 auto " but if you want it to be centered u need to give it a width
响应式 CSS 选项可将按钮垂直和水平居中,而无需关心父元素大小(为了清晰和分离问题使用数据属性挂钩):
HTML
CSS
Fiddle:https://jsfiddle.net/crrollyson/zebo1z8f/
Responsive CSS option to center a button vertically and horizontally without being concerned with parent element size (using data attribute hooks for clarity and separation concerns):
HTML
CSS
Fiddle: https://jsfiddle.net/crrollyson/zebo1z8f/
将按钮置于 div 中心的响应式方法:
Responsive way to center your button in a div:
适用于大多数情况的超级简单答案是将边距设置为
0 auto
并将显示设置为block
。您可以在 CodePenSuper simple answer that will apply to most cases is to just make set the margin to
0 auto
and set the display toblock
. You can see how I centered my button in my demo on CodePen