如何使用 CSS 将 HTML 中的 div 元素内的图像垂直居中?
我有这样的标记:
<div>
<img />
</div>
div 高于 img:
div {
height: 100px;
}
img {
height: dynamic-value-smaller-than-100px;
}
我需要图像位于 div 的中间(其上方和下方具有相同数量的空白)。
我尝试了这个,但它不起作用:
div {
vertical-align: middle;
}
I have a markup like this:
<div>
<img />
</div>
The div is higher than img:
div {
height: 100px;
}
img {
height: dynamic-value-smaller-than-100px;
}
I need the image to be in the middle of the div (have same amout of white space above and below it).
I tried this and it does not work:
div {
vertical-align: middle;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
如果您的图像纯粹是装饰性的,那么将其用作背景图像可能是一个更具语义的解决方案。然后,您可以指定背景的位置。
如果它不是装饰性的并且构成有价值的信息,那么 img 标签是合理的。在这种情况下,您需要做的是使用以下属性设置包含 div 的样式:
了解有关此技术的更多信息在这里。据报告不适用于 IE6/7(适用于 IE8)。
if your image is purely decorative, then it might be a more semantic solution to use it as a background-image. You can then specify the position of the background
If it is not decorative and constitutes valuable information then the img tag is justified. What you need to do in such case is style the containing div with the following properties:
Read more about this technique here. Reported to not work on IE6/7 (works on IE8).
另一种方法是在容器 div 中设置行高,并使用 Vertical-align: middle 将图像与图像对齐。
html:
css:
这超出了我的想象。但我以前用过这个——它应该可以解决问题。也适用于较旧的浏览器。
Another way is to set your line-height in the container div, and align your image to that using vertical-align: middle.
html:
css:
It's off the top of my head. But I've used this before - it should do the trick. Works for older browsers as well.
假设您想将图像 (40px X 40px) 放在 div class="box" 的中心(水平和垂直)。所以你有以下 html:
你所要做的就是应用 CSS:
你的 div 甚至可以改变它的大小,图像将始终位于它的中心。
Let's say you want to put the image (40px X 40px) on the center (horizontal and vertical) of the div class="box". So you have the following html:
What you have to do is apply the CSS:
Your div can even change it's size, the image will always be on the center of it.
这是我之前在 CSS 中用来实现垂直居中的解决方案。这适用于所有现代浏览器。
http://www.jakpsatweb.cz/css/css-vertical- center-solution.html
摘录:(
用于演示目的的内联样式)
This is a solution I've used before to accomplish vertical centering in CSS. This works in all the modern browsers.
http://www.jakpsatweb.cz/css/css-vertical-center-solution.html
Excerpt:
(Inline styles for demonstration purposes)
另一种选择是在
img
上设置display:block
,然后设置margin: 0px auto;
Another option is to set
display:block
on theimg
and then setmargin: 0px auto;
由于我也经常对跨浏览器 CSS 感到失望,因此我想在这里提供一个 JQuery 解决方案。这需要每个图像的父 div 的高度,将其除以二并将其设置为图像和 div 之间的上边距:
As I too am constantly being let down by cross-browser CSS, I'd like to offer a JQuery solution here. This takes the height of each image's parent div, divide it by two and set it as a top margin between the image and the div:
使用纯 CSS 将任意尺寸的图像居中有五种可能的方法。
使用
flex
并使img
标签位于内部(现代浏览器的最佳解决方案):将图像放入
背景中-图像
并使用background-position
(如@pixeline解释):使用
display: table
作为父元素,并使用display: table-cell
和vertical-align: middle
子元素的代码>:Using
position:absolute
和transform
的图像和父元素位置不能取消设置:使用
line-height
作为图像的相同高度元素,然后使用vertical-align
(在我看来,这是支持更多浏览器(如 IE9>)的最佳解决方案)。注意:在一些旧的浏览器中,有时为了安全地使用这种方式,您需要在图像存在的行中至少有一个字符。为了解决这个问题,我在父元素的伪元素中使用了不可破坏的空格。
如下例所示:
There are five possible ways for centering an image with any size with pure CSS.
Using
flex
and making theimg
tag be inside (best solution for modern browsers):Putting the image in
background-image
and usingbackground-position
(as @pixeline explained):Using
display: table
for parent element, and usingdisplay: table-cell
withvertical-align: middle
for child element:Using
position:absolute
withtransform
for the image and parent element position be not unset:Using
line-height
as same height of the element, then usingvertical-align
(in my opinion, the best solution for supporting more browsers like IE9>).Note: In some old browsers, sometimes for using this way safely, you need to have at least one character in the line that the image exist. For fixing this issue, I used a non-breakable space in a pseudo-element of the parent.
As in the following example:
我已经以跨浏览器的方式发布了有关垂直对齐的信息(垂直居中使用 CSS 的多个框)
创建单单元格表格。只有表格具有跨浏览器垂直对齐功能
I've posted about vertical alignment it in cross-browser way (Vertically center multiple boxes with CSS)
Create one-cell table. Only table has cross-browser vertical-align
图像位于 div 的中间
image to be in the middle of the div
在您的示例中,div 的高度是静态的,图像的高度也是静态的。为图像指定
margin-top
值为( div_height - image_height ) / 2
如果图像为 50px,则
In your example, the div's height is static and the image's height is static. Give the image a
margin-top
value of( div_height - image_height ) / 2
If the image is 50px, then
您是否尝试过在 div 上设置边距?例如
顶部和底部。您也许还可以使用百分比:
Have you tried setting margin on the div? e.g.
for top and bottom. You may also be able to use a percentage:
接受的答案对我不起作用。
vertical-align
需要一个合作伙伴,以便它们可以在中心对齐。因此,我创建了一个空的 div,它具有父 div 的完整高度,但没有可供图像对齐的宽度。两个对象需要inline-block
才能保持在一行中。CSS:
The accepted answer did not work for me.
vertical-align
needs a partner so that they can be aligned at their centers. So I created an empty div with full height of the parent div but with no width for the image to align with.inline-block
is needed for both objects to stay in one line.CSS:
如果您希望内容成为 div 中需要的内容,这对我来说很有效:
If you want content to be what ever you need to have inside a div, this did the job for me: