垂直对齐 div 内具有响应高度的图像

我有下面的代码,设置了一个容器的高度,随着宽度的变化,当浏览器是重新调整大小(以维持一个正方形高宽比)。

超文本标示语言

<div class="responsive-container">
<div class="dummy"></div>
<div class="img-container">
<IMG HERE>
</div>
</div>

CSS

.responsive-container {
position: relative;
width: 100%;
border: 1px solid black;
}


.dummy {
padding-top: 100%; /* forces 1:1 aspect ratio */
}


.img-container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}

如何垂直对齐容器内的 IMG?我所有的图片都有可变的高度和容器不能有一个固定的高度/线高度,因为它的响应... 请帮助!

350106 次浏览

试试这个

  .responsive-container{
display:table;
}
.img-container{
display:table-cell;
vertical-align: middle;
}

这里有一种技术,可以在 家长中同时水平和垂直地对齐内联元素:

垂直对齐

在这种方法中,我们创建一个 inline-block(伪)元素作为 家长的第一个(或最后一个)子元素,并将其 height属性设置为 100%以获取其 家长的所有高度。

2) 另外,添加 vertical-align: middle会使行内(- block)元素保持在行空间的中间。因此,我们将 CSS 声明添加到 第一个孩子我们的元素(形象)。

3) 最后,为了消除 内联(- 块)元素之间的空白字符,我们可以通过 font-size: 0;家长的字体大小设置为零。

注意: 我在下面使用了 Nicolas Gallagher 的 图像置换技术图像置换技术

有什么好处?

  • 容器(家长)可以具有动态维度。
  • 不需要显式地指定 image 元素的尺寸。

  • 我们也可以很容易地将这种方法用于 使 <div>元件垂直对齐; 它可能具有动态内容(高度和/或宽度)。但是请注意,您必须重新设置 divfont-size属性以显示内部文本。在线演示.

<div class="container">
<div id="element"> ... </div>
</div>
.container {
height: 300px;
text-align: center;  /* align the inline(-block) elements horizontally */
font: 0/0 a;         /* remove the gap between inline(-block) elements */
}


.container:before {    /* create a full-height inline block pseudo=element */
content: ' ';
display: inline-block;
vertical-align: middle;  /* vertical alignment of the inline element */
height: 100%;
}


#element {
display: inline-block;
vertical-align: middle;  /* vertical alignment of the inline element */
font: 16px/1 Arial sans-serif;        /* <-- reset the font property */
}

输出

Vertically align an element in its container

响应式集装箱

本节不回答这个问题,因为 OP 已经知道如何创建响应性容器。不过,我会解释它是如何工作的。

为了使容器元素的 身高随着它的 宽度(关于高宽比)发生变化,我们可以为 top/bottom padding属性使用一个百分比值。

顶部/底部填充或边距上的 百分比价值相对于包含块的宽度。

例如:

.responsive-container {
width: 60%;


padding-top: 60%;    /* 1:1 Height is the same as the width */
padding-top: 100%;   /* width:height = 60:100 or 3:5        */
padding-top: 45%;    /* = 60% * 3/4 , width:height =  4:3   */
padding-top: 33.75%; /* = 60% * 9/16, width:height = 16:9   */
}

下面是 在线演示。注释掉底部的线条,调整面板的大小,看看效果如何。

此外,我们可以将 padding属性应用于 笨蛋子元素或 :before/:after伪元素以获得相同的结果。但是 纸条,在这种情况下,padding上的百分比值是相对于 .responsive-container本身的 宽度的。

<div class="responsive-container">
<div class="dummy"></div>
</div>
.responsive-container { width: 60%; }


.responsive-container .dummy {
padding-top: 100%;    /*  1:1 square */
padding-top: 75%;     /*  w:h =  4:3 */
padding-top: 56.25%;  /*  w:h = 16:9 */
}

演示 # 1
演示 # 2 < em > (使用 :after伪元素)

添加内容

使用 padding-top属性 造成了巨大的空间在内容的顶部或底部,在 集装箱内。

为了解决这个问题,我们使用包装器元素包装内容,使用 绝对定位从文档正常流中删除该元素,最后扩展包装器(使用 toprightbottomleft属性)以填充其父元素 集装箱的整个空间。

开始了:

.responsive-container {
width: 60%;
position: relative;
}


.responsive-container .wrapper {
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
}

这是 在线演示


聚在一起

<div class="responsive-container">
<div class="dummy"></div>


<div class="img-container">
<img src="http://placehold.it/150x150" alt="">
</div>
</div>
.img-container {
text-align:center; /* Align center inline elements */
font: 0/0 a;       /* Hide the characters like spaces */
}


.img-container:before {
content: ' ';
display: inline-block;
vertical-align: middle;
height: 100%;
}


.img-container img {
vertical-align: middle;
display: inline-block;
}

这是 网站: http://jsfiddle.net/hashem/46psK/”rel = “ noReferrer”> WORKING DEMO

显然,您可以避免对 浏览器兼容性使用 ::before伪元素,并创建一个元素作为 .img-container的第一个子元素:

<div class="img-container">
<div class="centerer"></div>
<img src="http://placehold.it/150x150" alt="">
</div>
.img-container .centerer {
display: inline-block;
vertical-align: middle;
height: 100%;
}

更新的 DEMO

使用 max-*属性

为了保持图像在较低的宽度框内,您可以设置图像的 max-heightmax-width属性:

.img-container img {
vertical-align: middle;
display: inline-block;
max-height: 100%;  /* <-- Set maximum height to 100% of its parent */
max-width: 100%;   /* <-- Set maximum width to 100% of its parent */
}

这是 更新后的 DEMO

使用这个 css,因为你已经有了它的标记:

.img-container {
position: absolute;
top: 50%;
left: 50%;
}


.img-container > img {
margin-top:-50%;
margin-left:-50%;
}

这是一个可用的 JsBin: http://jsbin.com/ihilUnI/1/edit

此解决方案仅适用于正方形图像(因为百分比边距-顶部值取决于容器的宽度,而不是高度)。对于随机尺寸的图像,您可以执行以下操作:

.img-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* add browser-prefixes */
}

工作 JsBin 解决方案: http://jsbin.com/ihilUnI/2/edit

试试看

Html

<div class="responsive-container">
<div class="img-container">
<IMG HERE>
</div>
</div>

CSS

.img-container {
position: absolute;
top: 0;
left: 0;
height:0;
padding-bottom:100%;
}
.img-container img {
width:100%;
}

使用 margin: auto和绝对定位,你可以在水平和垂直方向上使图像居中。另外:

  1. 可以通过使用伪元素来丢弃额外的标记。
  2. 可以通过使用负的左、上、右和底值来显示大图像的中间部分。

.responsive-container {
margin: 1em auto;
min-width: 200px;       /* cap container min width */
max-width: 500px;       /* cap container max width */
position: relative;
overflow: hidden;       /* crop if image is larger than container */
background-color: #CCC;
}
.responsive-container:before {
content: "";            /* using pseudo element for 1:1 ratio */
display: block;
padding-top: 100%;
}
.responsive-container img {
position: absolute;
top: -999px;            /* use sufficiently large number */
bottom: -999px;
left: -999px;
right: -999px;
margin: auto;           /* center horizontally and vertically */
}
<p>Note: images are center-cropped on &lt;400px screen width.
<br>Open full page demo and resize browser.</p>
<div class="responsive-container">
<img src="http://lorempixel.com/400/400/sports/9/">
</div>
<div class="responsive-container">
<img src="http://lorempixel.com/400/200/sports/8/">
</div>
<div class="responsive-container">
<img src="http://lorempixel.com/200/400/sports/7/">
</div>
<div class="responsive-container">
<img src="http://lorempixel.com/200/200/sports/6/">
</div>

使用 Flexbox 很简单:

小提琴

只需将以下内容添加到图像容器中:

.img-container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex; /* add */
justify-content: center; /* add to align horizontal */
align-items: center; /* add to align vertical */
}

这里有一个技巧,可以让你在垂直和水平方向上集中任何内容!

基本上,您只需要两个容器,并确保您的元素满足以下条件。

另一个容器:

  • 应该有 display: table;

内容器:

  • 应该有 display: table-cell;
  • 应该有 vertical-align: middle;
  • 应该有 text-align: center;

内容框:

  • 应该有 display: inline-block;

如果您使用这种技术,只需将您的图像(以及任何其他内容,您希望与它一起)添加到内容框。

演示:

body {
margin : 0;
}


.outer-container {
position : absolute;
display: table;
width: 100%;
height: 100%;
background: #ccc;
}


.inner-container {
display: table-cell;
vertical-align: middle;
text-align: center;
}


.centered-content {
display: inline-block;
background: #fff;
padding : 12px;
border : 1px solid #000;
}


img {
max-width : 120px;
}
<div class="outer-container">
<div class="inner-container">
<div class="centered-content">
<img src="https://i.stack.imgur.com/mRsBv.png" />
</div>
</div>
</div>

也看到 这把小提琴

Html 代码

< div class = “ image-Container”> < img src = “”/>

CSS 代码

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

创建另一个 div,并在 div 中添加“虚拟”和“ img 容器”

像下面这样做 HTML 和 CSS

html , body {height:100%;}
.responsive-container { height:100%; display:table; text-align:center; width:100%;}
.inner-container {display:table-cell; vertical-align:middle;}
<div class="responsive-container">
<div class="inner-container">
<div class="dummy">Sample</div>
<div class="img-container">
Image tag
</div>
</div>
</div>

你可以给出你想要的高度,而不是100% 的响应容器,

我在寻找解决方案的过程中偶然发现了这个线索:

  • 使用给定图像宽度的100%
  • 保持图像的长宽比
  • 保持图像垂直对准中间
  • 在不完全支持 flex 的浏览器中工作

测试上面提到的解决方案的 一些,我没有找到满足所有这些标准的解决方案,所以我把这个简单的解决方案放在一起,它可能对其他需要做同样工作的人有用:

.container {
width: 30%;
float: left;
border: 1px solid turquoise;
margin-right: 3px;
margin-top: 3px;
}


.container:last-of-kind {
margin-right: 0px;
}


.image-container {
position: relative;
overflow: hidden;
padding-bottom: 70%;
/* this is the desired aspect ratio */
width: 100%;
}


.image-container img {
position: absolute;
/* the following 3 properties center the image on the vertical axis */
top: 0;
bottom: 0;
margin: auto;
/* uses image at 100% width (also meaning it's horizontally center) */
width: 100%;
}
<div class="container">
<div class="image-container">
<img src="http://placehold.it/800x800" class="img-responsive">
</div>
</div>


<div class="container">
<div class="image-container">
<img src="http://placehold.it/800x800" class="img-responsive">
</div>
</div>


<div class="container">
<div class="image-container">
<img src="http://placehold.it/800x800" class="img-responsive">
</div>
</div>

JSFiddle的工作实例