ChatGPT解决这个技术问题 Extra ChatGPT

如何垂直对齐div内的图像

如何在包含 div 的内部对齐图像?

例子

在我的示例中,我需要将 <div> 中的 <img>class ="frame" 垂直居中:

<div class="frame" style="height: 25px;">
    <img src="http://jsfiddle.net/img/logo.png" />
</div>

.frame 的高度是固定的,图像的高度是未知的。如果这是唯一的解决方案,我可以在 .frame 中添加新元素。我正在尝试在 Internet 上执行此操作  Explorer 7 及更高版本、WebKit、Gecko。

请参阅 jsfiddle here

.frame { 高度:25px; /* 等于最大图像高度 */ line-height: 25px;宽度:160px;边框:1px 纯红色;文本对齐:居中;边距:1em 0; } img { 背景:#3A6F9A;垂直对齐:中间;最大高度:25px;最大宽度:160px; }

< /div>

您好,抱歉,但我不同意在这里使用助手是最有价值的解决方案。但这不是唯一的方法。其他浏览器同样支持。我在 stackoverflow.com/a/43308414/7733724 和 W3C.org 上提供了有关信息的解决方案。你可以检查一下。干杯
阅读有关 W3C 的 Centering Things 文章会很有用:w3.org/Style/Examples/007/center.en.html
我认为关键是 .frame 中的行高来完成这项工作

Y
Yashwardhan Pauranik

据我所知,唯一(也是最好的跨浏览器)方法是在两个元素上使用带有 height: 100%vertical-align: middleinline-block 助手。

所以有一个解决方案:http://jsfiddle.net/kizu/4RPFa/4570/

.frame { 高度:25px; /* 等于最大图像高度 */ width: 160px;边框:1px 纯红色;空白:nowrap; /* 这是必需的,除非你把助手 span 放在 img 附近 */ text-align: center;边距:1em 0; } .helper { 显示:内联块;高度:100%;垂直对齐:中间; } img { 背景:#3A6F9A;垂直对齐:中间;最大高度:25px;最大宽度:160px; }

< div class="frame">
< span class="helper">

或者,如果您不想在现代浏览器中添加额外元素并且不介意使用 Internet Explorer 表达式,则可以使用伪元素并使用方便的表达式将其添加到 Internet Explorer,每个元素仅运行一次,所以不会有任何性能问题:

用于 Internet Explorer 的 :beforeexpression() 解决方案:http://jsfiddle.net/kizu/4RPFa/4571/

.frame { 高度:25px; /* 等于最大图像高度 */ width: 160px;边框:1px 纯红色;空白:nowrap;文本对齐:居中;边距:1em 0; } .frame:before, .frame_before { 内容:“”;显示:内联块;高度:100%;垂直对齐:中间; } img { 背景:#3A6F9A;垂直对齐:中间;最大高度:25px;最大宽度:160px; } /* 将此移至条件注释 */ .frame { list-style:none;行为:表达式(函数(t){ t.insertAdjacentHTML('afterBegin',''); t.runtimeStyle.behavior = 'none'; }(this) ); }

< /div>

这个怎么运作:

当您有两个彼此靠近的 inline-block 元素时,您可以彼此对齐,因此使用 vertical-align: middle 您将得到如下结果:当您有一个固定高度的块时(以 px、em 或其他为单位绝对单位),您可以设置内部块的高度,以 % 为单位。因此,在具有固定高度的块中添加一个具有 height: 100% 的 inline-block 将在其中垂直对齐其中的另一个 inline-block 元素(在您的情况下为 )。


请注意:<img src=""/> 不在已添加的 <span></span> 帮助程序内。它在外面。我只是因为没有意识到这一点而把我的每一根头发都拉了下来。
看来您还需要添加“空白:nowrap;”到框架,否则如果您的图像和助手跨度之间有换行符,您将遇到问题。我花了一个小时为什么这个解决方案对我不起作用。
我不确定这是不是真的。我已将相同的方法应用于没有 .helperimg,并且效果很好。只需制作 imginline-block,它就可以正确垂直对齐。
请注意,这只适用于 img 的高度小于框架的高度。如果 img 高于 frame,则它不与中间对齐。
T
Tahir Yasin

这可能有用:

div {
    position: relative;
    width: 200px;
    height: 200px;
}
img {
    position: absolute;
    top: 0;
    bottom: 0;
    margin: auto;
}
.image {
    min-height: 50px
}

如果您还希望水平对齐图像,请添加 left:0;右:0;到img
如果您事先不知道图像的大小,请制作一个重复的图像:.img1 { position: absolute; top: 0; bottom: 0; margin: auto; } .img2 { visibility: hidden; }
P
Peter Mortensen

matejkramny 的解决方案是一个好的开始,但超大图像的比例错误。

这是我的叉子:

演示:https://jsbin.com/lidebapomi/edit?html,css,output

https://i.imgur.com/Fgh89yb.png

HTML:

<div class="frame">
  <img src="foo"/>
</div>

CSS:

.frame {
    height: 160px; /* Can be anything */
    width: 160px; /* Can be anything */
    position: relative;
}
img {
    max-height: 100%;
    max-width: 100%;
    width: auto;
    height: auto;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}

我试过了,效果很好,谢谢
P
Peter Mortensen

三线解决方案:

position: relative;
top: 50%;
transform: translateY(-50%);

这适用于任何事情。

here


如果这个解决方案是跨浏览器的,它肯定应该在顶部!
d
dota2pro

纯 CSS 解决方案:

.frame { 边距:1em 0;高度:35px;宽度:160px;边框:1px 纯红色;位置:相对; } img { 最大高度:25px;最大宽度:160px;位置:绝对;顶部:0;底部:0;左:0;右:0;边距:自动;背景:#3A6F9A; }

< /div>

关键的东西

// position: relative; - in .frame holds the absolute element within the frame
// top: 0; bottom: 0; left: 0; right: 0; - this is key for centering a component
// margin: auto; - centers the image horizontally & vertically

R
Ricardo Zea

对于更现代的解决方案,如果不需要支持旧版浏览器,您可以这样做:

.frame { 显示:弹性; /** 取消注释下方的“justify-content”以水平居中。 ✪ 阅读以下内容以获得更好的垂直和水平居中方式。 **/ /* justify-content: center; */ 对齐项目:居中; } img { 高度:自动; /** ✪ 要使图像垂直和水平居中,在上面的 .frame 规则中注释 'justify-content' 和 'align-items' 声明,然后取消注释 'margin: auto;'以下。 **/ /* 边距:自动; */ } /* 演示不需要的样式 */ .frame { max-width: 900px;高度:200px;边距:自动;背景:#222; } p { 最大宽度:900px;边距:20px 自动 0; } img { 宽度:150px; }

这是使用 Flexbox 的 Pen:http://codepen.io/ricardozea/pen/aa0ee8e6021087b6e2460664a0fa3f3e

编辑 1/13/22

使用 CSS Grid 和 place-content 简写有更好的方法:

.frame-text-grid { 显示:网格;地点内容:中心; /** ✪ “place-content”是“align-content”和“justify-content”的简写。 ✪ “place-content”简写需要两个值,第一个用于“align-content”,第二个用于“justify-content”。如果仅存在一个值(如在此演示中),则该单个值将应用于两个方向。 ✪ 评论“地点内容:中心;”上面的声明以查看元素如何沿容器的高度分布。 **/ }

使用Grid和place-content

垂直和水平居中只需要两行.

这是使用 CSS 网格的钢笔:https://codepen.io/ricardozea/pen/c4e27f1e74542618d73e21f7c2276272?editors=0100


这个“网格”解决方案很简单,感谢您更新我们。它还有助于将长的水平图像与 overflow:hiddenmax-width 相结合,以减少奇怪的纵横比的失真。换句话说,除了解决垂直图像的主要问题之外,您可能还想裁剪长图像的侧面。
P
Peter Mortensen

这样您就可以将图像垂直居中 (demo):

div{
  height: 150px; // Internet Explorer 7 fix
  line-height: 150px;
}
img{
  vertical-align: middle;
  margin-bottom: 0.25em;
}

P
Peter Mortensen

此外,您可以使用 Flexbox 来获得正确的结果:

.parent { 对齐项目:中心; /* 垂直对齐 */ background: red;显示:弯曲;高度:250px; /* 对齐内容:中心; <- 水平对齐 */ width: 250px; }


谢谢,这终于对我有用了!
B
BBlackwo

flexbox 有一个超级简单的解决方案!

.frame {
    display: flex;
    align-items: center;
}

是的,但您几乎无法在所有答案中看到它。我试图简单而清晰地强调它,因为它比所有其他答案都容易得多。
这是对我也有用的最佳答案。但在我的情况下,框架“inline-flex”对我来说效果更好。对于其中的图像,我添加了:“垂直对齐:中间”。
Flexbox 现在有非常广泛的支持。这是最简单的解决方案(可能您的响应者会感谢您)。支持文档:caniuse.com/#feat=flexbox
f
fdrv

想象一下你有

<div class="wrap">
    <img src="#">
</div>

和CSS:

.wrap {
    display: flex;
}
.wrap img {
    object-fit: contain;
}

Y
Yeodave

您可以尝试将 PI 的 CSS 设置为 display: table-cell; vertical-align: middle;


m
m4n0

CSS 网格

如果要在图像容器内垂直对齐单个图像,可以使用:

.img-container {
  display: grid;
}

img { 
  align-self: center;
}

.img-container { 显示:网格;网格自动流:列;背景:#BADA55;宽度:1200 像素;高度:500px; } img.vertical-align { 对齐自我:中心; }

如果你想在一个图像容器内对齐多个图像,你可以使用这个:

.img-container {
  display: grid;
  align-items: center;
}

.img-container { 显示:网格;网格自动流:列;对齐项目:居中;背景:#BADA55;宽度:1200 像素;高度:500px; }

< img src="https://picsum.photos/300/300" />

请注意,我在这两种情况下都使用了 grid-auto-flow: column,因为否则元素将换行并指定显式网格项。在问题代码中,我也看到该项目水平居中。在这种情况下,只需使用 place-items: center 而不是 align-items: center


P
Peter Mortensen

你可以试试下面的代码:

.frame{ 显示:弹性;证明内容:中心;对齐项目:居中;宽度:100%; }


P
Peter Mortensen

背景图片解决方案

我完全删除了图像元素并将其设置为具有 .frame 类的 div 的背景

http://jsfiddle.net/URVKa/2/

这至少在 Internet Explorer 8、Firefox 6 和 Chrome 13 上运行良好。

我查了一下,这个解决方案不能缩小大于 25 像素高度的图像。有一个名为 background-size 的属性确实设置了元素的大小,但它是 CSS 3 会与 Internet Explorer 7 要求相冲突。

如果您想使用此解决方案,我建议您重做浏览器优先级并设计最佳可用浏览器,或者获取一些服务器端代码来调整图像大小。


P
Peter Mortensen

http://jsfiddle.net/MBs64/

.frame {
    height: 35px;      /* Equals maximum image height */
    width: 160px;
    border: 1px solid red;
    text-align: center;
    margin: 1em 0;
    display: table-cell;
    vertical-align: middle;
}
img {
    background: #3A6F9A;
    display: block;
    max-height: 35px;
    max-width: 160px;
}

.frame 的键属性是 display: table-cell;Div.frame 显示为内联,因此您需要将其包装在块元素中。

这适用于 Firefox、Opera、Chrome、Safari 和 Internet Explorer 8(及更高版本)。

更新

对于 Internet Explorer 7,我们需要添加一个 CSS 表达式:

*:first-child+html img {
    position: relative;
    top: expression((this.parentNode.clientHeight-this.clientHeight)/2+"px");
}

C
Community

你可以这样做:

演示

http://jsfiddle.net/DZ8vW/1

CSS

.frame {
    height: 25px;      /* Equals maximum image height */
    line-height: 25px;
    width: 160px;
    border: 1px solid red;
    
    text-align: center; 
    margin: 1em 0;
    position: relative; /* Changes here... */
}
img {
    background: #3A6F9A;
    max-height: 25px;
    max-width: 160px;
    top: 50%;           /* Here.. */
    left: 50%;          /* Here... */
    position: absolute; /* And here */
}    

JavaScript

$("img").each(function(){
    this.style.marginTop = $(this).height() / -2 + "px";
})

好的。纯CSS解决方案的任何机会?
不幸的是,除非您想使用表格或放弃与旧版本 IE 的兼容性,否则不会。 :( 如果您事先知道图像的确切大小,那会有所不同,但并非没有这一点,单独使用 CSS 是不可能的。
@Joseph您将如何用表格修复它?
@Benjamin Udink 10 Cate 它很乱,但可以这样做:jsfiddle.net/DZ8vW/2(不建议这样做,但应该可以)
好吧,它非常适合 arnauds 的需求。没有 JS,只有 CSS 和 HTML。
J
JGallardo

这适用于现代浏览器(编辑时为 2016 年),如本 demo on codepen 中所示

.frame {
    height: 25px;
    line-height: 25px;
    width: 160px;
    border: 1px solid #83A7D3;          
}
.frame img {
    background: #3A6F9A;
    display:inline-block;
    vertical-align: middle;
}

给图像一个类或使用继承来定位您需要居中的图像是非常重要的。在此示例中,我们使用了 .frame img {},以便仅定位由类为 .frame 的 div 包装的图像。


只有ie7,只要放line-height:25px;
即使在 Firefox 和 chrome ( jsfiddle.net/4RPFa/19 ) 上,它似乎也没有正确对齐
完美运行。
P
Peter Mortensen

使用表格和表格单元格的解决方案

有时应该通过显示为表格/表格单元格来解决。例如,快速标题屏幕。这也是 W3 推荐的方式。我建议您查看来自 W3C.org 的名为 Centering a block or image 的链接。

这里使用的技巧是:

绝对定位容器显示为表格

垂直对齐到显示为表格单元格的中心内容

.container { 位置:绝对;显示:表格;宽度:100%;高度:100%; } .content { 显示:表格单元格;垂直对齐:中间; }

世界和平

就我个人而言,我实际上不同意为此目的使用助手。


I
Ilya Lysenko

我的解决方案:http://jsfiddle.net/XNAj6/2/

<div class="container">
    <div class="frame">
        <img src="http://jsfiddle.net/img/logo.png" class="img" alt="" />
    </div>
</div>

.container {
    display: table;
    float: left;
    border: solid black 1px;
    margin: 2px;
    padding: 0;
    background-color: black;
    width: 150px;
    height: 150px;
}
.frame {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    border-width: 0;
}
.img {
    max-width: 150px;
    max-height: 150px;
    vertical-align: middle;
}

P
Peter Mortensen

用纯 CSS 试试这个解决方案 http://jsfiddle.net/sandeep/4RPFa/72/

也许这是您的 HTML 的主要问题。定义 class & 时没有使用引号image height 在您的 HTML 中。

CSS:

.frame {
    height: 25px;      /* Equals maximum image height */
    width: 160px;
    border: 1px solid red;
    position: relative;
    margin: 1em 0;
    top: 50%;
    text-align: center;
    line-height: 24px;
    margin-bottom: 20px;
}

img {
    background: #3A6F9A;
    vertical-align: middle;
    line-height: 0;
    margin: 0 auto;
    max-height: 25px;
}

当我使用 img 标记时,它会从 top 留下 3 到 2 像素的空间。现在我减少 line-height,它正在工作。

CSS:

    .frame {
        height: 25px;      /* Equals maximum image height */
        width: 160px;
        border: 1px solid red;
        margin: 1em 0;
        text-align: center;
        line-height: 22px;
        *:first-child+html line-height:24px; /* For Internet Explorer 7 */
    }

    img {
        background: #3A6F9A;
        vertical-align: middle;
        line-height: 0;    
        max-height: 25px;
        max-width: 160px;
    }
@media screen and (-webkit-min-device-pixel-ratio:0) {
    .frame {
        line-height:20px; /* WebKit browsers */
    }

line-height 属性在不同浏览器中的呈现方式不同。因此,我们必须定义不同的 line-height 属性浏览器。

检查此示例:http://jsfiddle.net/sandeep/4be8t/11/

查看此示例,了解不同浏览器中的 line-height 不同:input height differences in Firefox and Chrome


不起作用(至少在 Firefox 中)。对于缺少的引号,这在 HTML5 中是允许的(虽然不知道 jsfiddle 中使用了哪种文档类型)
P
Peter Mortensen

我不确定 Internet Explorer,但在 Firefox 和 Chrome 下,如果您在 div 容器中有一个 img,则以下 CSS 内容应该可以工作。至少对我来说效果很好:

div.img-container {
    display: table-cell;
    vertical-align: middle;
    height: 450px;
    width: 490px;
}

div.img-container img {
    max-height: 450px;
    max-width: 490px;
}

不幸的是,IE8 +。对我来说很好,但可能不适合其他人。
另请注意,display:table-cell; 不接受 % 作为宽度
P
Peter Mortensen

一种对我有用的简单方法:

img {
    vertical-align: middle;
    display: inline-block;
    position: relative;
}

它非常适用于谷歌浏览器。在不同的浏览器中试试这个。


P
Peter Mortensen

除了像这样的一些文本之外,用于在容器内居中图像(可能是徽标):

https://i.stack.imgur.com/sVHfi.png

基本上你包装图像

.outer-frame { 边框:1px 纯红色;最小高度:200px;文本对齐:居中; /* 仅水平对齐 */ } .wrapper{ line-height: 200px;边框:2px 蓝色虚线;边框半径:20px;边距:50px } img { /* 高度:自动; */ 垂直对齐:中间; /* 仅垂直对齐 */ }

一些文本


P
Peter Mortensen

如果您可以接受像素大小的边距,只需将 font-size: 1px; 添加到 .frame。但请记住,现在在 .frame 1em = 1px 上,这意味着您还需要以像素为单位设置边距。

http://jsfiddle.net/feeela/4RPFa/96/

现在它不再以 Opera 为中心了……


P
Peter Mortensen

我有同样的问题。这对我有用:

<style type="text/css">
    div.parent {
        position: relative;
    }

    img.child {
        bottom: 0;
        left: 0;
        margin: auto;
        position: absolute;
        right: 0;
        top: 0;
    }
</style>

<div class="parent">
    <img class="child">
</div>

实际上,图像上的“位置:固定”对我有用,我对人们提出的许多错误答案感到沮丧,这些错误答案在我的作品中不起作用。使用 algreat 建议的方法,您也不需要额外的容器。
P
Peter Mortensen

你可以使用这个:

 .loaderimage {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 60px;
    height: 60px;
    margin-top: -30px; /* 50% of the height */
    margin-left: -30px;
 }

A
Alireza

使用 tabletable-cell 方法完成这项工作,特别是因为您还针对一些旧浏览器,我为您创建了一个片段,您可以运行它并检查结果:

.wrapper { 位置:相对;显示:表格;宽度:300px;高度:200px; } .inside { 垂直对齐:中间;显示:表格单元格; }

请给我中心!!!


P
Peter Mortensen

想要对齐在文本/标题之后且都在 div 内的图像?

请参阅 JSfiddle 或运行代码片段。

只要确保所有元素(div、img、title 等)都有一个 ID 或一个类。

对我来说,此解决方案适用于所有浏览器(对于移动设备,您必须使用:@media 调整您的代码)。

h2.h2red { 颜色:红色;字体大小:14px; } .mydivclass { 边距顶部:30px;显示:块; } img.mydesiredclass { margin-right: 10px;显示:块;向左飘浮; /* 如果要将文本与同一行的图像对齐 */ width: 100px;高度:100px; margin-top: -40px /* 改变这个值以适应你的页面 */; }


文本在div内通过负操作img位置对齐后的图像


s
svnm

我一直在使用填充来进行中心对齐。您将需要定义顶级外部容器大小,但内部容器应该调整大小,并且您可以将填充设置为不同的百分比值。

jsfiddle

<div class='container'>
  <img src='image.jpg' />
</div>

.container {
  padding: 20%;
  background-color: blue;
}

img {
  width: 100%;
}

P
Peter Mortensen

最好的解决方案是

.block{
    /* Decor */
    padding:0 20px;
    background: #666;
    border: 2px solid #fff;
    text-align: center;
    /* Important */
    min-height: 220px;
    width: 260px;
    white-space: nowrap;
}
.block:after{
    content: '';
    display: inline-block;
    height: 220px; /* The same as min-height */
    width: 1px;
    overflow: hidden;
    margin: 0 0 0 -5px;
    vertical-align: middle;
}
.block span{
    vertical-align: middle;
    display: inline-block;
    white-space: normal;
}