如何垂直对齐 div 中的元素?
我有一个包含两个图像的 div
和一个 h1
。 所有这些都需要在 div 内垂直对齐,彼此相邻。 其中一张图像需要绝对
定位在div
内。
需要什么 CSS 才能在所有常见浏览器上运行?
<div id="header">
<img src=".." ></img>
<h1>testing...</h1>
<img src="..."></img>
</div>
I have a div
with two images and an h1
. All of them need to be vertically aligned within the div, next to each other. One of the images needs to be absolute
positioned within the div
.
What is the CSS needed for this to work on all common browsers?
<div id="header">
<img src=".." ></img>
<h1>testing...</h1>
<img src="..."></img>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
将 child(
div
) 居中对齐的四种方法。演示
Four ways to to align child(
div
) in center.Demo
我尝试使用 CSS flex 来做到这一点,但如果它超出了块(使用“overflow:auto”),它就会截断对象。
通过实验,我发现如果使用网格,很容易将任何对象(线或块)居中。
我真的很喜欢这个解决方案,所以我在 CMS Effcore 中实现了它。
这是一个代码示例:
通过网格的示例:
通过“flex”的示例。
通过“垂直对齐”的示例。
可以在此处找到实时代码示例: https://developer.mozilla .org/ru/play?id=AE9iTZBMsHnSmr4lfRPvmQU3p6DMFO4uGxZY%2BM7ABDgHuUotnvJdG5VISHO5%2BkYdb7YGTMQtKr2HlhGc
I tried to do it using CSS flex but it truncates the object if it goes beyond the block (with "overflow: auto").
Through experimentation, I discovered that it is very easy to center any object (line or block) if you use Grid.
I really liked the solution, so I implemented it in the CMS Effcore.
Here is a code example:
Example via Grid:
Example via "flex".
Example via "vertical-align".
A live code example can be found here: https://developer.mozilla.org/ru/play?id=AE9iTZBMsHnSmr4lfRPvmQU3p6DMFO4uGxZY%2BM7ABDgHuUotnvJdG5VISHO5%2BkYdb7YGTMQtKr2HlhGc
就是这样:
div 内的一个单元格表格处理垂直对齐,并且向后兼容回到石器时代!
Just this:
A one-cell table inside the div handles the vertical-align and is backward compatible back to the Stone Age!
这是另一种(响应式)方法:
http://jsfiddle.net/herrfischerhamburg/JcVxz/
Here is just another (responsive) approach:
http://jsfiddle.net/herrfischerhamburg/JcVxz/
...
或 CSS
浏览器覆盖范围
...
or CSS
Browser Coverage
我已经使用以下解决方案(没有定位和行高)一年多了。 它可与 Internet Explorer 7 和 Internet Explorer 8 也是如此。
I have been using the following solution (with no positioning and no line height) for over a year. It works with Internet Explorer 7 and Internet Explorer 8 as well.
使用display flex,首先需要包装要对齐的项目的容器:
然后应用以下CSS内容来包装我的示例中的div或outdiv:
Using display flex, first you need to wrap the container of the item that you want to align:
Then apply the following CSS content to wrap div or outdiv in my example:
使用CSS垂直居中,您可以让外部容器像表格一样,而内容像表格单元格一样。 在这种格式中,您的对象将保持居中。 :)
我在 JSFiddle 中嵌套了多个对象作为示例,但核心思想是这样的:
HTML
CSS
多个对象示例:
HTML
<强>CSS
结果
https:/ /jsfiddle.net/martjemeyer/ybs032uc/1/
Using CSS to vertical center, you can let the outer containers act like a table, and the content as a table cell. In this format your objects will stay centered. :)
I nested multiple objects in JSFiddle for an example, but the core idea is like this:
HTML
CSS
The multiple objects example:
HTML
CSS
Result
https://jsfiddle.net/martjemeyer/ybs032uc/1/
我找到了一种新的解决方法,可以使用 CSS 3 垂直对齐 div 中的多个文本行(并且我还使用 bootstrap v3 网格系统来美化 UI),如下所示:
根据我的理解,包含元素的文本必须有一定的高度。
I have found a new workaround to vertically align multiple text-lines in a div using CSS 3 (and I am also using bootstrap v3 grid system to beautify the UI), which is as below:
As per my understanding, the immediate parent of text containing element must have some height.
这一天已经到来了!
块布局的
align-content
属性可用于对齐。浏览器支持:
This day has come!
align-content
property for block layouts can be used for aligning.Browser Support:
我们可以使用 CSS 函数计算来计算元素的大小,然后相应地定位子元素。
示例 HTML:
和 CSS:
此处创建的演示: https://jsfiddle.net/xnjq1t22/
此解决方案有效以及响应式
div
height
和width
。注意:calc 函数未测试与旧浏览器的兼容性。
We may use a CSS function calculation to calculate the size of the element and then position the child element accordingly.
Example HTML:
And CSS:
Demo created here: https://jsfiddle.net/xnjq1t22/
This solution works well with responsive
div
height
andwidth
as well.Note: The calc function is not tested for compatiblity with old browsers.
仅使用 Bootstrap 类:
class="container d-flex"
class="m-auto"
Using only a Bootstrap class:
class="container d-flex"
class="m-auto"
默认情况下,h1 是一个块元素,将在第一个 img 之后的行上渲染,并将导致第二个 img 出现在块后面的行上。
要阻止这种情况发生,您可以将 h1 设置为具有内联流行为:
至于将 img 绝对定位在 div 内,您需要将包含的 div 设置为具有“已知大小”,然后才会出现这种情况。好好工作。 根据我的经验,您还需要更改位置属性,使其远离默认值 -position:relative 对我来说很有效:
如果您可以使其正常工作,您可能需要尝试逐步从 div.header 中删除高度、宽度、位置属性获得所需的最少属性以获得您想要的效果。
更新:
以下是适用于 Firefox 3 的完整示例:
By default h1 is a block element and will render on the line after the first img, and will cause the second img to appear on the line following the block.
To stop this from occurring you can set the h1 to have inline flow behaviour:
As for absolutely positioning the img inside the div, you need to set the containing div to have a "known size" before this will work properly. In my experience, you also need to change the position attribute away from the default - position: relative works for me:
If you can get that to work, you might want to try progressively removing the height, width, position attributes from div.header to get the minimal required attributes to get the effect you want.
UPDATE:
Here is a complete example that works on Firefox 3:
我最喜欢的新方法是使用 CSS 网格:
My new favorite way to do it is with a CSS grid:
只需在 div 内使用一个单元格表格即可! 只需将单元格和表格的高度设置为 100%,即可使用垂直对齐。
div 内的一个单元格表格处理垂直对齐,并且向后兼容回到石器时代!
Just use a one-cell table inside the div! Just set the cell and table height and with to 100% and you can use the vertical-align.
A one-cell table inside the div handles the vertical-align and is backward compatible back to the Stone Age!
对我来说,它是这样工作的:
使用 Bootstrap 类将“a”元素转换为按钮,现在它在外部“div”内部垂直居中。
For me, it worked this way:
The "a" element converted to a button, using Bootstrap classes, and it is now vertically centered inside an outer "div".
这是我针对
div
内的i
元素的个人解决方案。JSFiddle 示例
HTML
CSS
This is my personal solution for an
i
element inside adiv
.JSFiddle Example
HTML
CSS
我的技巧是在div内放一个表格,一行一列,宽度和高度设置为100%,属性vertical-align:middle:
Fiddle:
http://jsfiddle.net/joan16v/sbqjnn9q/
My trick is to put a table inside the div with one row and one column, set 100% of width and height, and the property vertical-align:middle:
Fiddle:
http://jsfiddle.net/joan16v/sbqjnn9q/
如何对齐? 图像的顶部与文本的顶部对齐吗?
相对于 DIV 绝对定位? 也许你可以勾勒出你正在寻找的东西......?
fd 描述了绝对定位的步骤,以及调整
H1
元素的显示,使图像与其内联显示。 为此,我将补充一点,您可以使用vertical-align
样式对齐图像:...这会将标题和图像放在一起,并对齐顶部边缘。 存在其他对齐选项; 请参阅文档。 您可能还会发现删除 DIV 并将图像移动到
H1
元素内是有益的 - 这为容器提供了语义值,并且无需调整H1
的显示代码>:Aligned how? Tops of the images aligned with the top of the text?
Absolutely positioned relative to the DIV? Perhaps you could sketch out what you're looking for...?
fd has described the steps for absolute positioning, as well as adjusting the display of the
H1
element such that images will appear inline with it. To that, i'll add that you can align the images by use of thevertical-align
style:...this would put the header and images together, with top edges aligned. Other alignment options exist; see the documentation. You might also find it beneficial to drop the DIV and move the images inside the
H1
element - this provides semantic value to the container, and removes the need to adjust the display of theH1
:几乎所有方法都需要指定高度,但通常我们没有任何高度。
所以这里有一个 CSS 3 三行技巧,不需要知道高度。
即使在 Internet Explorer 9 中也支持它。
及其供应商前缀:
来源:垂直只需 3 行 CSS 即可对齐任何内容
Almost all methods needs to specify the height, but often we don't have any heights.
So here is a CSS 3 three-line trick that doesn't require to know the height.
It's supported even in Internet Explorer 9.
with its vendor prefixes:
Source: Vertical align anything with just 3 lines of CSS
要将块元素定位到中心(适用于 Internet Explorer 9 及更高版本),需要包装器
div
:To position block elements to the center (works in Internet Explorer 9 and above), it needs a wrapper
div
:使用这个公式,它将始终有效而不会出现裂缝:
Use this formula, and it will work always without cracks:
我朋友的一个技巧:
演示此处。
A technique from a friend of mine:
Demo here.
它对我有用:
It worked for me:
垂直和水平对齐元素
使用其中之一。 结果是相同的:
1. Bootstrap 4.3+
对于垂直对齐:
d-flexalign-items-center
对于水平对齐:
d- flex justify-content-center
对于垂直和水平对齐:
d-flexalign-items-center justify-content-center
2.CSS3
Vertically and horizontally align element
Use either of these. The result would be the same:
1. Bootstrap 4.3+
For vertical alignment:
d-flex align-items-center
For horizontal alignment:
d-flex justify-content-center
For vertical and horizontal alignment:
d-flex align-items-center justify-content-center
2. CSS 3
我使用了这个非常简单的代码:
显然,无论您使用
.class
还是#id
,结果都不会改变。I used this very simple code:
Obviously, whether you use a
.class
or an#id
, the result won't change.现在 Flexbox 支持不断增加,应用于包含元素的 CSS 将垂直居中所有包含的项目(除了那些本身指定对齐方式的项目,例如
align-self:start
)如果您还需要定位 Internet Explorer 10 及更早版本 (<4.4 (KitKat)) Android 浏览器:
Now that Flexbox support is increasing, this CSS applied to the containing element would vertically center all contained items (except for those items that specify the alignment themselves, e.g.
align-self:start
)Use the prefixed version if you also need to target Internet Explorer 10, and older (< 4.4 (KitKat)) Android browsers:
哇,这个问题很流行。 它基于对
vertical-align
属性的误解。 这篇优秀的文章对此进行了解释:了解
vertical-align
,或“如何(不是)垂直居中内容” 作者:Gavin Kistner。“如何在 CSS 中居中” 是一个很棒的网络工具,可以帮助找到必要的 CSS针对不同情况的居中属性。
简而言之(并防止链接失效):
vertical-align: middle
在上下文中对齐。 然而,“上下文”并不是整个父容器的高度,而是它们所在文本行的高度。 jsfiddle示例绝对
并指定其高度
、边距-顶部和顶部位置。 jsfiddle 示例
line- height
来填充其高度。 根据我的经验,这种方法非常通用。 jsfiddle 示例Wow, this problem is popular. It's based on a misunderstanding in the
vertical-align
property. This excellent article explains it:Understanding
vertical-align
, or "How (Not) To Vertically Center Content" by Gavin Kistner.“How to center in CSS” is a great web tool which helps to find the necessary CSS centering attributes for different situations.
In a nutshell (and to prevent link rot):
vertical-align: middle
. However, the “context” isn’t the whole parent container height, it’s the height of the text line they’re in. jsfiddle exampleabsolute
and specify itsheight
,margin-top
andtop
position. jsfiddle exampleline-height
to fill its height. This method is quite versatile in my experience. jsfiddle example