如何在div内垂直居中可变高度的内容?
当内容的高度可变时,垂直居中 div 内容的最佳方法是什么。 在我的特定情况下,容器 div 的高度是固定的,但如果有一个解决方案可以在容器高度可变的情况下工作,那就太好了。 另外,我希望有一个不使用或很少使用 CSS hack 和/或非语义标记的解决方案。
What is the best way to vertically center the content of a div when the height of the content is variable. In my particular case, the height of the container div is fixed, but it would be great if there were a solution that would work in cases where the container has a variable height as well. Also, I would love a solution with no, or very little use of CSS hacks and/or non-semantic markup.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是我针对具有动态(百分比)高度的
div
的出色解决方案。CSS
HTML
自己尝试一下。
This is my awesome solution for a
div
with a dynamic (percentaged) height.CSS
HTML
Try this by yourself.
对我来说,最好的方法是:
优点是不必明确高度
For me the best way to do this is:
The advantage is not having to make the height explicit
只需添加
到内部 div 即可。
它的作用是将内部 div 的顶部边框移动到外部 div 的一半高度 (
top: 50%;
),然后将内部 div 向上移动其高度的一半 (transform:translateY( -50%)
)。 这适用于position:absolute
或relative
。请记住,
transform
和translate
具有供应商前缀,为简单起见,未包含这些前缀。Codepen:http://codepen.io/anon/pen/ZYprdb
Just add
to the inner div.
What it does is moving the inner div's top border to the half height of the outer div (
top: 50%;
) and then the inner div up by half its height (transform: translateY(-50%)
). This will work withposition: absolute
orrelative
.Keep in mind that
transform
andtranslate
have vendor prefixes which are not included for simplicity.Codepen: http://codepen.io/anon/pen/ZYprdb
这似乎是我发现的解决此问题的最佳解决方案,只要您的浏览器支持
::before
伪元素: CSS 技巧:以未知为中心。它不需要任何额外的标记,而且似乎工作得非常好。 我无法使用
display: table
方法,因为table
元素不遵守max-height
属性。This seems to be the best solution I’ve found to this problem, as long as your browser supports the
::before
pseudo element: CSS-Tricks: Centering in the Unknown.It doesn’t require any extra markup and seems to work extremely well. I couldn’t use the
display: table
method becausetable
elements don’t obey themax-height
property.这是我需要做很多次的事情,一致的解决方案仍然需要您添加一些非语义标记和一些特定于浏览器的技巧。 当我们获得对 css 3 的浏览器支持时,您将获得垂直居中而不会犯错。
为了更好地解释该技术,您可以查看我改编自的文章,但基本上它涉及添加额外的元素并在 IE 和支持非表格元素上的
position:table\table-cell
的浏览器中应用不同的样式。有很多方法(黑客)可以在特定的浏览器集中应用样式。 我使用了条件注释,但请查看上面链接的文章以了解其他两种技术。
注意:如果您提前知道一些高度,如果您尝试将单行文本居中,或者在其他几种情况下,有一些简单的方法可以实现垂直居中。 如果您有更多详细信息,请输入它们,因为可能有一种不需要浏览器黑客或非语义标记的方法。
更新:我们开始获得更好的浏览器支持CSS3,引入了弹性盒和变换作为获得垂直居中(以及其他效果)的替代方法。 有关现代方法的更多信息,请参阅另一个问题,但请记住,浏览器支持对于 CSS3 来说仍然是粗略的。
This is something I have needed to do many times and a consistent solution still requires you add a little non-semantic markup and some browser specific hacks. When we get browser support for css 3 you'll get your vertical centering without sinning.
For a better explanation of the technique you can look the article I adapted it from, but basically it involves adding an extra element and applying different styles in IE and browsers that support
position:table\table-cell
on non-table elements.There are many ways (hacks) to apply styles in specific sets of browsers. I used conditional comments but look at the article linked above to see two other techniques.
Note: There are simple ways to get vertical centering if you know some heights in advance, if you are trying to center a single line of text, or in several other cases. If you have more details then throw them in because there may be a method that doesn't require browser hacks or non-semantic markup.
Update: We are beginning to get better browser support for CSS3, bringing both flex-box and transforms as alternative methods for getting vertical centering (among other effects). See this other question for more information about modern methods, but keep in mind that browser support is still sketchy for CSS3.
使用子选择器,我采取了 Fadi 的上述答案令人难以置信,并将其归结为我可以应用的一条 CSS 规则。 现在我所要做的就是将
contentCentered
类名添加到我想要居中的元素中:分叉的 CodePen: http://codepen.io/dougli/pen/Eeysg
Using the child selector, I've taken Fadi's incredible answer above and boiled it down to just one CSS rule that I can apply. Now all I have to do is add the
contentCentered
class name to elements I want to center:Forked CodePen: http://codepen.io/dougli/pen/Eeysg
您可以使用自动保证金。 使用 flex 时,div 似乎也垂直居中。
You can use margin auto. With flex, the div seems to be centered vertically too.
您可以使用 Flex 显示,如下面的代码:
you can use flex display such as below code:
到目前为止对我来说最好的结果:
div居中:
Best result for me so far:
div to be centered: