简体   繁体   English

使div内垂直居中的响应图像

[英]making a responsive image vertically centered inside div

I have an image that is bigger than its container div. 我的图像大于其容器div。 When the browser/screen resizes, it should make the image bigger or smaller but always keeping the center of the image centered inside the div. 当浏览器/屏幕调整大小时,应该使图像更大或更小,但始终将图像的中心保持在div的中心。 One example of this is the following website: http://www.qdoba.com/ . 以下网站就是一个例子: http : //www.qdoba.com/ They have their images centered as you resize the screen making it very well responsive. 当您调整屏幕大小时,它们的图像居中,使其响应速度非常好。 What I have at the moment only makes it resize horizontally but not vertically. 我目前所拥有的只是使其水平调整大小,而不能垂直调整大小。 This is what I have so far: 这是我到目前为止的内容:

.swiper-container {
    width: 100%;
    height: 300px;
    color: #fff;
    text-align: center;
    overflow: hidden;
}
.slide{ 
    width:100%; 

} 
.slide img{ 
    width:100%; 
}
</style>

<div class="swiper-container">
   <div class="slide">
        <img src='http://www.envision-creative.com/wp-content/uploads/Tiagos01.jpg' />
        <div class="title">Slide 1</div>
    </div>
</div>

Its pretty simple. 非常简单。 [Jump to the end of the answer for the updated Fiddle] [跳至最新小提琴答案的结尾]

HTML HTML

<div class="slide">
    <span class="Centerer"></span><div class="Centered">
        <img src='http://imageshack.us/a/img19/3207/15p0213.jpg' />
        <div class="title">Slide 1</div>
    </div>
</div>

CSS CSS

*
{
    margin: 0;
    padding:0;
}
html, body
{
    height: 100%;
}
.slide
{ 
    text-align: center;
    height: 100%;
    width: 100%; /*not really needed*/
}
.Centerer
{
    height: 100%;
    display: inline-block;
    vertical-align: middle;
}
.Centered
{
    display: inline-block;
    vertical-align: middle;
}

/*you don't always want your centered stuff to be 100% width, so its in a different rule*/
.Content
{
    width: 100%;
}
.Content img
{
    max-width: 30%; /*so you can see the responsive alignment and size*/
}

If you don't set any height to the Slide, it will always have the exact height of its content, so it will look like there is no vertical alignment. 如果未对幻灯片设置任何高度,则它将始终具有其内容的确切高度,因此看起来好像没有垂直对齐。

Check out this Working Fiddle 看看这个工作小提琴

EDIT I didn't understood you correctly. 编辑我不正确地理解你。 Check out this New Fiddle 看看这个新提琴

<div class="slide">
    <img src='images/test.jpg' />
    <div class="title">Slide 1</div>
</div>

<style>
.slide{ 
    width:100%; 
    display:table-cell;
    vertical-align:middle;
    height: 200px; /* you have to make height static */
 } 

.slide img{
   width:100%; 
} 
</style>

Ok I finally got an answer to my question. 好的,我终于得到了我问题的答案。 Perhaps I was not explaining it correctly but what I initially needed was to make sure the center of my image was vertically centered inside a div. 也许我没有正确解释它,但最初需要确保图像中心在div内垂直居中。 The answer could not be done with just HTML and CSS but javascript needed to be implemented. 答案不能仅用HTML和CSS来完成,而是需要实现javascript。 so here it is CHECK OUT FIDDLE : 所以这里是CHECK OUT FIDDLE

<style>
.swiper-container {
    width:100%;
}

.slide{ 
    width:100%; 
    max-height: 200px;
    overflow: hidden; 
}

.slide img{ 
    width:100%; 
}
</style>

<div class="swiper-container">
   <div class="slide">
        <img src='http://www.envision-creative.com/wp-content/uploads/Tiagos01.jpg' />
        <div class="title">Slide 1</div>
   </div>
</div>

<script>
$(document).ready(function(){

    var imageHeight, wrapperHeight, overlap, container = $('.slide');

    function centerImage(){
       imageHeight = container.find('img').height();
       wrapperHeight = container.height();
       overlap = (wrapperHeight - imageHeight)/2;
       container.find('img').css('margin-top', overlap);
    }

    $(window).bind("load resize", centerImage);

 });

<script>

I had a very similar problem to you, also for a responsive slider component that I was building. 对于您正在构建的自适应滑块组件,我也遇到了与您非常相似的问题。

My requirements: 我的要求:

  • The slider container must have a 100% width against the viewport and it must have a fixed height. 滑块容器必须相对于视口具有100%的宽度,并且必须具有固定的高度。
  • The images within the slider must stretch to the full width of the slider. 滑块内的图像必须拉伸到滑块的整个宽度。
  • The images must maintain their aspect ratio so they are not distorted. 图像必须保持其纵横比,以免失真。
  • The images must always be vertically aligned to the middle of the slider container to create a "center crop" effect for any of the image which doesn't fit within the restricted height of the slider container. 图像必须始终与滑块容器的中间垂直对齐,以为不适合滑块容器限制高度的任何图像创建“中心裁剪”效果。

I managed to achieve this without the use of any javascript based on principles from the following smashing magazine post: http://www.smashingmagazine.com/2013/08/09/absolute-horizontal-vertical-centering-css/2 我成功地实现了这一目标,而没有使用以下smashing杂志的原理上的任何javascript: http//www.smashingmagazine.com/2013/08/09/absolute-horizo​​ntal-vertical-centering-css/2

Here is a generic form of my html, without any noise, to illustrate how to solve this problem: 这是我的html的通用形式,没有任何噪音,以说明如何解决此问题:

<div class="container">
  <img src="http://lorempixel.com/640/480/animals/1" />
</div>

And here is the minimal css required: 这是所需的最低CSS:

.container {
    width: 100%;
    height: 200px;
    overflow: hidden;
    position: relative;
}

.container img {
    position: absolute;
    top: 0; left: 0; bottom: 0; right: 0;
    margin: auto; 
    width: 100%;
}

You can see a fiddle of this in action here: http://jsfiddle.net/8kjfasbt/ 您可以在此处看到此操作的小提琴: http : //jsfiddle.net/8kjfasbt/

It's a very basic implementation, focusing on the problem, but you could easily take this and drop it into a slider implementation and add fancy media queries for some extra effect. 这是一个非常基本的实现,着重于该问题,但是您可以轻松地将其放到滑块实现中,并添加奇特的媒体查询以产生一些额外的效果。

You could of course modify the CSS to give the image other dimensions, pixel or percentage, it would still center. 您当然可以修改CSS,以为图像提供其他尺寸,像素或百分比,但它仍将居中。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM