如何使用CSS垂直将DIV元素集成为DIV元素?
我想用CSS垂直将 Div
中心。我不想要桌子或JavaScript,而只需要纯CSS。我找到了一些解决方案,但是所有解决方案都缺少Internet Explorer 6支持。
<body>
<div>Div to be aligned vertically</div>
</body>
我如何在包括Internet Explorer 6在内的所有主要浏览器中垂直将 Div
归为中心?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
不幸的是,但毫不奇怪 - 该解决方案比人们希望的要复杂。同样不幸的是,您需要在想要垂直居中的DIV周围使用其他DIV。
对于符合标准的浏览器,例如Mozilla,Opera,Safari等。 /em> - 然后可以垂直以中心为中心。对于Internet Explorer,您需要位置 在外部div中绝对,然后将 top 指定为 50%。以下页面很好地解释了此技术,并提供了一些代码示例:
也有一种技术可以使用JavaScript进行垂直中心。 < CSS 证明了这一点。
Unfortunately — but not surprisingly — the solution is more complicated than one would wish it to be. Also unfortunately, you'll need to use additional divs around the div you want vertically centered.
For standards-compliant browsers like Mozilla, Opera, Safari, etc. you need to set the outer div to be displayed as a table and the inner div to be displayed as a table-cell — which can then be vertically centered. For Internet Explorer, you need to position the inner div absolutely within the outer div and then specify the top as 50%. The following pages explain this technique well and provide some code samples too:
There is also a technique to do the vertical centering using JavaScript. Vertical alignment of content with JavaScript & CSS demonstrates it.
如果有人关心Internet&nbsp; explorer&nbsp; 10(及以后),请使用
Flexbox支持: http://caniuse.com/flexbox
If someone cares for Internet Explorer 10 (and later) only, use Flexbox:
Flexbox support: http://caniuse.com/flexbox
一种垂直集中元素的现代方法是使用
flexbox
。您需要父母来决定身高和一个孩子以中心。
下面的示例将以浏览器内的中心为中心。重要的是(在我的示例中)是设置
高度:100%
tobody
和html
html ,然后min-heivy:100%
到您的容器。A modern way to center an element vertically would be to use
flexbox
.You need a parent to decide the height and a child to center.
The example below will center a div to the center within your browser. What's important (in my example) is to set
height: 100%
tobody
andhtml
and thenmin-height: 100%
to your container.相关:中心图像
Related: Center a Image
仅在您不关心Internet Explorer 6和7时垂直居中
,您可以使用涉及两个容器的技术。
外部容器:
显示:表;
内置容器:
display:table-cell;
fertical-align:中间;
内容框:
显示:inline-block;
您可以将您想要的任何内容添加到内容框中,而无需关心其宽度或高度!
演示:
另请参见 这个小提琴 !
水平和垂直居中,
如果您想水平和垂直居中,则还需要以下内容。
内部容器:
文本壁:中心;
内容框:
text-align:left; left;
ore代码> text-align:正确; ,除非您希望文本为中心demo:
另请参见 这个小提琴 !
Centering only vertically
If you don't care about Internet Explorer 6 and 7, you can use a technique that involves two containers.
The outer container:
display: table;
The inner container:
display: table-cell;
vertical-align: middle;
The content box:
display: inline-block;
You can add any content you want to the content box without caring about its width or height!
Demo:
See also this Fiddle!
Centering horizontally and vertically
If you want to center both horizontally and vertically, you also need the following.
The inner container:
text-align: center;
The content box:
text-align: left;
ortext-align: right;
, unless you want text to be centeredDemo:
See also this Fiddle!
它可以通过两种方式完成
,也可以
使用Flex
Align-Items:Center;
使内容垂直中心Jusify-content:Center; 使内容水平中心
It can be done in two ways
OR
Using flex
align-items:center;
makes the content vertically centerjustify-content: center;
makes the content horizontally center当我必须回到时,我总是去的地方。
对于那些不想进行跳跃的人:
位置:相对
或位置:绝对
。位置:绝对
和顶部:在子容器上的50%
将上移到父母中间。代码中的一个示例:
This is always where I go when I have to come back to this issue.
For those who don't want to make the jump:
position:relative
orposition:absolute
.position:absolute
andtop:50%
on the child container to move the top down to the middle of the parent.An example of this in code:
我刚刚写了这篇CSS并了解更多信息,请仔细阅读:本文具有垂直的任何内容,只需3行CSS 。
I just wrote this CSS and to know more, please go through: This article with vertical align anything with just 3 lines of CSS.
CSS网格
CSS Grid
对于新来者,请尝试:
For newcomers, please try:
使用
转换
实际上在现代浏览器和Internet&nbsp“ Explorer上使用的三行代码:我正在添加此答案,因为我在以前的版本中发现了此答案的某些不完整性(并且堆栈溢出不允许我简单评论)。
'位置'相对会使样式弄乱,如果当前的DIV在体内并且没有容器div。但是“固定”似乎有效,但显然可以修复视口中心的内容
'>
也我也使用了这种造型将某些覆盖层的divs居中,并在Mozilla中发现了这种转换的所有元素Div失去了底部的边界。可能是一个渲染问题。但是,仅在其中一些添加最小的填充物将其正确渲染。 Chrome和Internet&nbsp; Explorer(令人惊讶的是)渲染了盒子,而无需填充

The three lines of code using
transform
works practically on modern browsers and Internet Explorer:I am adding this answer since I found some incompleteness in the previous version of this answer (and Stack Overflow won't allow me to simply comment).
'position' relative messes up the styling if the current div is in the body and has no container div. However 'fixed' seems to work, but it obviously fixes the content in the center of the viewport

Also I used this styling for centering some overlay divs and found that in Mozilla all elements inside this transformed div had lost their bottom borders. Possibly a rendering issue. But adding just the minimal padding to some of them rendered it correctly. Chrome and Internet Explorer (surprisingly) rendered the boxes without any need for padding


Billbad的答案仅适用于
.inner
div的固定宽度。该解决方案可通过添加属性<代码>文本align:中心到
.outer
div来实现动态宽度。The answer from Billbad only works with a fixed width of the
.inner
div.This solution works for a dynamic width by adding the attribute
text-align: center
to the.outer
div.只需做到:在您的
div
上添加类:并阅读本文用于解释。注意:
高度
是必要的。Just do it: Add the class at your
div
:And read this article for an explanation. Note:
Height
is necessary.没有回答浏览器兼容性,而是要提及新的网格和不那么新的Flexbox功能。
Grid
From:
浏览器支持: noreflow noreferrer“> grid browser support
CSS:
html:
flexbox
浏览器支持: flexbox浏览器支持
CSS:
Not answering for browser compatibility but to also mention the new Grid and the not so new Flexbox feature.
Grid
From: Mozilla - Grid Documentation - Align Div Vertically
Browser Support: Grid Browser Support
CSS:
HTML:
Flexbox
Browser Support: Flexbox Browser Support
CSS:
我这样做了(相应地更改宽度,高度,边缘顶部和边缘左侧):
I did it with this (change width, height, margin-top and margin-left accordingly):
我认为无需使用Flexbox的所有浏览器一个可靠的解决方案 - “ Align -Items:Center;”;是显示的组合:桌子和垂直空间:中间;。
CSS
html‣demo
: https://jsfiddle.net/6m640rpp/
I think a solid solution for all browsers without using Flexbox - "align-items: center;" is a combination of display: table and vertical-align: middle;.
CSS
HTML
‣Demo: https://jsfiddle.net/6m640rpp/
现在,我们可以在块布局上使用
Align-Content
,而无需FlexBox或CSS网格。您可以使用最新版本的Chrome测试以下内容。
Now we can use the
align-content
on block layouts without the need of flexbox or CSS grid.You can test the below using the latest version of chrome.
特别是对于具有相对(未知)高度的父级,以未知为中心解决方案对我有好处。文章中有一些非常不错的代码示例。
它在Chrome,Firefox,Opera和Internet&nbsp; Explorer中进行了测试。
Especially for parent divs with relative (unknown) height, the centering in the unknown solution works great for me. There are some really nice code examples in the article.
It was tested in Chrome, Firefox, Opera, and Internet Explorer.
下面是我可以在垂直和水平中构建的最好的全能解决方案,柔性高度内容框。它经过了最近版本的Firefox,Opera,Chrome和Safari的测试和工作。
查看一个带有动态内容的工作示例
我构建了一些动态内容来测试灵活性,并希望喜欢知道是否有人看到了任何问题。它也适用于集中的覆盖层 - 灯箱,弹出式等。
Below is the best all-around solution I could build to vertically and horizontally center a fixed-width, flexible height content box. It was tested and worked for recent versions of Firefox, Opera, Chrome, and Safari.
View A Working Example With Dynamic Content
I built in some dynamic content to test the flexibility and would love to know if anyone sees any problems with it. It should work well for centered overlays also -- lightbox, pop-up, etc.
最简单的方法是以下 CSS的三行:
1)位置:相对;
2)顶部:50%;
3 )变换:translatey(-50%);
以下是示例:
The simplest way would be the following three lines of CSS:
1) position: relative;
2) top: 50%;
3) transform: translateY(-50%);
Following is an example:
我再也看不到列表:
高度
必须声明(请参见“ nofollow noreferrer”>变量高度来源:绝对水平和垂直中心CSS
One more I can't see on the list:
height
must be declared (see Variable Height)overflow: auto
to prevent content spillover (see Overflow)Source: Absolute Horizontal And Vertical Centering In CSS
现在 flexbox 解决方案是现代浏览器的一种非常简单的方法,所以我为您推荐这一点,所以我推荐这一点:
Now the Flexbox solution is a very easy way for modern browsers, so I recommend this for you:
实际上,您需要两个Div才能进行垂直居中。包含内容的DIV必须具有宽度和高度。
这是结果。
Actually, you need two div's for vertical centering. The div containing the content must have a width and height.
Here is the result.
编辑2020:仅在需要支持旧浏览器之类的 Internet&nbsp; explorer&nbsp; 8 (哪个(哪个)(您应该拒绝做
Edit 2020: only use this if you need to support old browsers like Internet Explorer 8 (which you should refuse to do ????). If not, use Flexbox.
This is the simplest method I found and I use it all the time
(jsFiddle demo here).
Thank Chris Coyier from CSS Tricks for this article.
Support starts with Internet Explorer 8.
经过大量研究,我终于找到了最终解决方案。它甚至适用于漂浮元素。 查看源
After a lot of research I finally found the ultimate solution. It works even for floated elements. View Source
要将Div居中在页面上,检查小提琴链接。
另一个选项是使用Flex Box,检查小提琴链接。
另一个选择是使用CSS 3转换:
To center the div on a page, check the fiddle link.
Another option is to use flex box, check the fiddle link.
Another option is to use a CSS 3 transform:
最简单的解决方案如下:
The easiest solution is below:
有多种方法可以实现这一目标。
使用CSS的Flex属性。
解决方案#1
或使用使用
显示:flex;
and保证金:auto;
解决方案#2
显示文本中心
解决方案#3
使用百分比(%)高度和宽度。
解决方案#4
There are multiple ways to achieve this.
Using flex property of CSS.
Solution #1
or by using
display: flex;
andmargin: auto;
Solution #2
show text center
Solution #3
Using percentage(%) height and width.
Solution #4