div 中的中心文本?
我有一个 div
30px
高和 500px
宽。此 div
可以包含两行文本,一行在一行,并相应地设置样式(填充)。但有时它只包含一行,我希望它居中。这可能吗?
I have a div
30px
high and 500px
wide. This div
can contain two lines of text one under the other, and is styled (padded) accordingly. But sometimes it only contains one line, and I want it to be centered. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
要水平居中,请使用
text-align:center
。要垂直居中,只有在要对齐的同一行中存在另一个元素时,才能使用
vertical-align:middle
。此处查看其工作情况。
我们使用高度为100%的空span,然后将内容放在下一个元素中,并设置vertical-align:middle。
还有其他技术,例如使用表格单元格或将内容放入绝对定位的元素中,并将顶部、底部、左侧和右侧全部设置为零,但它们都存在跨浏览器兼容性问题。
To center horizontally, use
text-align:center
.To center vertically, one can only use
vertical-align:middle
if there is another element in the same row that it is being aligned to.See it working here.
We use an empty span with a height of 100%, and then put the content in the next element with a vertical-align:middle.
There are other techniques such as using table-cell or putting the content in an absolutely positioned element with top, bottom, left, and right all set to zero, but they all suffer from cross browser compatibility issues.
我相信您希望文本在 div 内垂直居中,而不是(仅)水平居中。我知道执行此操作的唯一可靠方法是
在您的 div 上使用:,并且它适用于任意数量的行。
您可以在此处查看测试: http://jsfiddle.net/qMtZV/1/
请务必检查浏览器对此的支持属性,因为它在 IE7 或更早版本中不受支持。
更新 02/10/2016
五年后,这项技术仍然有效,但我相信这个问题有更好、更可靠的解决方案。由于现在 Flexbox 支持很好,您可能需要按照以下方式做一些事情:http://codepen.io/michelegera/pen/gPZpqE。
I believe you want the text to be vertically centered inside your div, not (only) horizontally. The only reliable way I know of doing this is using:
on your div, and it works with any number of lines.
You can see a test here: http://jsfiddle.net/qMtZV/1/
Be sure to check browser support of this property, as it is not supported — for example — in IE7 or earlier.
UPDATE 02/10/2016
Five years later this technique is still valid, but I believe there are better and more solid solutions to this problem. Since Flexbox support is good nowadays, you might want to do something along these lines: http://codepen.io/michelegera/pen/gPZpqE.
对于父 div,使用以下 CSS:
For the parent div,use following CSS:
HTMLCSS
HTML
CSS
如果
中有文本:
If you have text inside a
<div>
:诀窍是将
line-height
设置为等于div
的height
。The trick is to set the
line-height
equal to theheight
of thediv
.如果存在尺寸问题,您可以尝试在CSS中使用属性vertical-align,以便将其垂直居中
,请注意2个文本行和填充样式很有可能具有超过30px的高度。
例如,如果您的字体大小为 12 px,div 内边距为 5 px,则单行 div 高度将为 5px (padding-top) + 12px + 5 px (padding-bottom) = 22px < 30px所以没问题,
使用2行文本div,它将是5px + 12px * 2(2行)+ 5px = 34px > 30px,你的div高度会自动改变。
尝试增加 div 高度(也许 40 像素)或减少内边距。
希望它会有所帮助
You may try to use in your CSS the property vertical-align in order to center it verticaly
if it's a size problem, please notice that 2 text lines and a padding style have great chance to have a height superior to 30px.
For example, if your font size is 12 px and your div padding is 5 px, a one text line div height will be 5px (padding-top) + 12px + 5 px (padding-bottom) = 22px < 30px so no problem,
With a 2 text lines div, it will be 5px +12px *2 (2 lines) + 5px = 34px > 30px and your div height will be automatically changed.
Try either to increase your div height (maybe 40px) or to reduce your padding.
Hope it will help
添加行高也有帮助。
Adding line height helps too.
我可能在这里遗漏了一些东西,但是你尝试过吗:
??
I may be missing something here, but have you tried:
??
这对你有用吗?
Will this work for you?
我环顾四周,这
似乎是最受欢迎的解决方案
I've looked around and the
seems to be the most popular solution
使用 flexbox 使 div 居中
}
using flexbox To center div
}
添加到包含文本的选择器
Add to the selector containing the text