如何使用 CSS 将 HTML 中的 div 元素内的图像垂直居中?

发布于 2024-08-20 18:35:51 字数 332 浏览 6 评论 0原文

我有这样的标记:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

走过海棠暮 2024-08-27 18:35:52

如果您的图像纯粹是装饰性的,那么将其用作背景图像可能是一个更具语义的解决方案。然后,您可以指定背景的位置。

background-position: center center;

如果它不是装饰性的并且构成有价值的信息,那么 img 标签是合理的。在这种情况下,您需要做的是使用以下属性设置包含 div 的样式:

div{
    display: table-cell; vertical-align: middle 
}

了解有关此技术的更多信息在这里。据报告不适用于 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

background-position: center center;

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:

div{
    display: table-cell; vertical-align: middle 
}

Read more about this technique here. Reported to not work on IE6/7 (works on IE8).

也只是曾经 2024-08-27 18:35:52

另一种方法是在容器 div 中设置行高,并使用 Vertical-align: middle 将图像与图像对齐。

html:

<div class="container"><img></div>

css:

.container {
  width: 200px; /* or whatever you want */
  height: 200px; /* or whatever you want */
  line-height: 200px; /* or whatever you want, should match height */
  text-align: center;
}

.container > img {
  vertical-align: middle;
}

这超出了我的想象。但我以前用过这个——它应该可以解决问题。也适用于较旧的浏览器。

Another way is to set your line-height in the container div, and align your image to that using vertical-align: middle.

html:

<div class="container"><img></div>

css:

.container {
  width: 200px; /* or whatever you want */
  height: 200px; /* or whatever you want */
  line-height: 200px; /* or whatever you want, should match height */
  text-align: center;
}

.container > img {
  vertical-align: middle;
}

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.

∞觅青森が 2024-08-27 18:35:52

假设您想将图像 (40px X 40px) 放在 div class="box" 的中心(水平和垂直)。所以你有以下 html:

<div class="box"><img /></div>

你所要做的就是应用 CSS:

.box img {
    position: absolute;
    top: 50%;
    margin-top: -20px;
    left: 50%;
    margin-left: -20px;
}

你的 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:

<div class="box"><img /></div>

What you have to do is apply the CSS:

.box img {
    position: absolute;
    top: 50%;
    margin-top: -20px;
    left: 50%;
    margin-left: -20px;
}

Your div can even change it's size, the image will always be on the center of it.

执笔绘流年 2024-08-27 18:35:52

这是我之前在 CSS 中用来实现垂直居中的解决方案。这适用于所有现代浏览器。

http://www.jakpsatweb.cz/css/css-vertical- center-solution.html

摘录:(

  <div style="display: table; height: 400px; position: relative; overflow: hidden;">
    <div style="position: absolute; top: 50%;display: table-cell; vertical-align: middle;">
      <div style="position: relative; top: -50%">
        any text<br>
        any height<br>
        any content, for example generated from DB<br>
        everything is vertically centered
      </div>
    </div>
  </div>

用于演示目的的内联样式)

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:

  <div style="display: table; height: 400px; position: relative; overflow: hidden;">
    <div style="position: absolute; top: 50%;display: table-cell; vertical-align: middle;">
      <div style="position: relative; top: -50%">
        any text<br>
        any height<br>
        any content, for example generated from DB<br>
        everything is vertically centered
      </div>
    </div>
  </div>

(Inline styles for demonstration purposes)

热风软妹 2024-08-27 18:35:52

另一种选择是在 img 上设置 display:block,然后设置 margin: 0px auto;

img{
    display: block;
    margin: 0px auto;
}

Another option is to set display:block on the img and then set margin: 0px auto;

img{
    display: block;
    margin: 0px auto;
}
徒留西风 2024-08-27 18:35:52

由于我也经常对跨浏览器 CSS 感到失望,因此我想在这里提供一个 JQuery 解决方案。这需要每个图像的父 div 的高度,将其除以二并将其设置为图像和 div 之间的上边距:

$('div img').each(function() {
 m = Math.floor(($(this).parent('div').height() - $(this).height())/2);
 mp = m+"px";
 $(this).css("margin-top",mp);
});

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:

$('div img').each(function() {
 m = Math.floor(($(this).parent('div').height() - $(this).height())/2);
 mp = m+"px";
 $(this).css("margin-top",mp);
});
执妄 2024-08-27 18:35:52

使用纯 CSS 将任意尺寸的图像居中有五种可能的方法。

  1. 使用 flex 并使 img 标签位于内部(现代浏览器的最佳解决方案):

    div {
        显示:柔性;
        对齐项目:居中;
        调整内容:居中
    }
    
  2. 将图像放入 背景中-图像并使用background-position(如@pixeline解释):

    div {
        背景图像: url(...);
        背景位置:center 中心
    }
    
  3. 使用 display: table 作为父元素,并使用 display: table-cellvertical-align: middle子元素的代码>:

    div.parent {
        显示:表;
    }
    div.child {
        显示:表格单元格;
        垂直对齐:居中;
    }
    
  4. Using position:absolutetransform 的图像和父元素位​​置不能取消设置:

    div {
        位置:相对;
    }
    分区>图像{
        位置:绝对;
        左:50%;
        顶部:50%;
        变换:翻译(-50%,-50%);
    }
    
  5. 使用 line-height 作为图像的相同高度元素,然后使用 vertical-align在我看来,这是支持更多浏览器(如 IE9>)的最佳解决方案)。

    注意:在一些旧的浏览器中,有时为了安全地使用这种方式,您需要在图像存在的行中至少有一个字符。为了解决这个问题,我在父元素的伪元素中使用了不可破坏的空格。

    如下例所示:

    div {
        显示:块;
        高度:200px;
        宽度:200px;
        背景颜色:紫色;
        行高:200px;
        文本对齐:居中;
    }
    div:之后{
      内容:“\a0”;
    }
    分区>图像{
        垂直对齐:居中;
    }

There are five possible ways for centering an image with any size with pure CSS.

  1. Using flex and making the img tag be inside (best solution for modern browsers):

    div {
        display: flex;
        align-items: center;
        justify-content: center
    }
    
  2. Putting the image in background-image and using background-position (as @pixeline explained):

    div {
        background-image: url(...);
        background-position:center center
    }
    
  3. Using display: table for parent element, and using display: table-cell with vertical-align: middle for child element:

    div.parent {
        display: table;
    }
    div.child {
        display: table-cell;
        vertical-align: middle;
    }
    
  4. Using position:absolute with transform for the image and parent element position be not unset:

    div {
        position: relative;
    }
    div > img {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
    }
    
  5. Using line-height as same height of the element, then using vertical-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:

    div {
        display: block;
        height: 200px;
        width: 200px;
        background-color: purple;
        line-height: 200px;
        text-align: center;
    }
    div:after {
      content: "\a0";
    }
    div > img {
        vertical-align: middle;
    }
    <div><img src="https://via.placeholder.com/100.png/09f/fff" /></div>

陪我终i 2024-08-27 18:35:52

我已经以跨浏览器的方式发布了有关垂直对齐的信息(垂直居中使用 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

场罚期间 2024-08-27 18:35:52

图像位于 div 的中间

div img{ 
    bottom: 0;
    left: 0;
    margin: auto;
    position: absolute;
    right: 0;
    top: 0;
    height:50px;
    width:50px;
}

image to be in the middle of the div

div img{ 
    bottom: 0;
    left: 0;
    margin: auto;
    position: absolute;
    right: 0;
    top: 0;
    height:50px;
    width:50px;
}
橘味果▽酱 2024-08-27 18:35:52

在您的示例中,div 的高度是静态的,图像的高度也是静态的。为图像指定 margin-top 值为 ( div_height - image_height ) / 2

如果图像为 50px,则

img {
    margin-top: 25px;
}

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

img {
    margin-top: 25px;
}
晚风撩人 2024-08-27 18:35:52

您是否尝试过在 div 上设置边距?例如

div {
    padding: 25px, 0
}

顶部和底部。您也许还可以使用百分比:

div {
    padding: 25%, 0
}

Have you tried setting margin on the div? e.g.

div {
    padding: 25px, 0
}

for top and bottom. You may also be able to use a percentage:

div {
    padding: 25%, 0
}
倒数 2024-08-27 18:35:52
<div style="background-color:#006600; width:300px; text-align:center; padding:50px 0px 50px 0px;">
<img src="imges/import.jpg" width="200" height="200"/>
</div>
<div style="background-color:#006600; width:300px; text-align:center; padding:50px 0px 50px 0px;">
<img src="imges/import.jpg" width="200" height="200"/>
</div>
來不及說愛妳 2024-08-27 18:35:52

接受的答案对我不起作用。 vertical-align 需要一个合作伙伴,以便它们可以在中心对齐。因此,我创建了一个空的 div,它具有父 div 的完整高度,但没有可供图像对齐的宽度。两个对象需要 inline-block 才能保持在一行中。

<div>
    <div class="placeholder"></div>
    <img />
</div>

CSS:

.class {
    height: 100%;
    width: 0%;
    vertical-align: middle;
    display: inline-block
}
img {
    display: inline-block;
    vertical-align: middle;
}

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.

<div>
    <div class="placeholder"></div>
    <img />
</div>

CSS:

.class {
    height: 100%;
    width: 0%;
    vertical-align: middle;
    display: inline-block
}
img {
    display: inline-block;
    vertical-align: middle;
}
柳若烟 2024-08-27 18:35:52
div {

width:200px; 
height:150px; 

display:-moz-box; 
-moz-box-pack:center; 
-moz-box-align:center; 

display:-webkit-box; 
-webkit-box-pack:center; 
-webkit-box-align:center; 

display:box; 
box-pack:center; 
box-align:center;

}

<div>
<img src="images/logo.png" />
</div>
div {

width:200px; 
height:150px; 

display:-moz-box; 
-moz-box-pack:center; 
-moz-box-align:center; 

display:-webkit-box; 
-webkit-box-pack:center; 
-webkit-box-align:center; 

display:box; 
box-pack:center; 
box-align:center;

}

<div>
<img src="images/logo.png" />
</div>
眼中杀气 2024-08-27 18:35:52
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
(function ($) {

$.fn.verticalAlign = function() {
    return this.each(function(i){
    var ah = $(this).height();
    var ph = $(this).parent().height();
    var mh = Math.ceil((ph-ah)/2);
    $(this).css('margin-top', mh);
    });
};
})(jQuery);

$(document).ready(function(e) {


$('.in').verticalAlign();


});

</script>

<style type="text/css">
body { margin:0; padding:0;}
.divWrap { width:100%;}
.out { width:500px; height:500px; background:#000; text-align:center; padding:1px; }
.in { width:100px; height:100px; background:#CCC; margin:0 auto; }
</style>
</head>

<body>
<div class="divWrap">
<div class="out">
<div class="in">
</div>
</div>
</div>

</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
(function ($) {

$.fn.verticalAlign = function() {
    return this.each(function(i){
    var ah = $(this).height();
    var ph = $(this).parent().height();
    var mh = Math.ceil((ph-ah)/2);
    $(this).css('margin-top', mh);
    });
};
})(jQuery);

$(document).ready(function(e) {


$('.in').verticalAlign();


});

</script>

<style type="text/css">
body { margin:0; padding:0;}
.divWrap { width:100%;}
.out { width:500px; height:500px; background:#000; text-align:center; padding:1px; }
.in { width:100px; height:100px; background:#CCC; margin:0 auto; }
</style>
</head>

<body>
<div class="divWrap">
<div class="out">
<div class="in">
</div>
</div>
</div>

</body>
</html>
不喜欢何必死缠烂打 2024-08-27 18:35:52

如果您希望内容成为 div 中需要的内容,这对我来说很有效:

<div style="
  display: table-cell;
  vertical-align: middle;
  background-color: blue;
  width: ...px;
  height: ...px;
">
    <div style="
      margin: auto;
      display: block;
      width: fit-content;
    ">
        <!-- CONTENT -->
        <img src="...">
        <p> some text </p>
    </div>
 </div>

If you want content to be what ever you need to have inside a div, this did the job for me:

<div style="
  display: table-cell;
  vertical-align: middle;
  background-color: blue;
  width: ...px;
  height: ...px;
">
    <div style="
      margin: auto;
      display: block;
      width: fit-content;
    ">
        <!-- CONTENT -->
        <img src="...">
        <p> some text </p>
    </div>
 </div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文