简体   繁体   English

如何在较小的容器内动态居中放置图像?

[英]How to dynamically center image inside a smaller container?

I have a gallery slider, with random images from the forum. 我有一个图库滑块,带有来自论坛的随机图像。 So, the size is pretty random but the gallery(container frame) itself is fix sized. 因此,大小是相当随机的,但是图库(容器框架)本身的大小是固定的。 So, we decided to set the image height to a fixed size but the width is set to auto. 因此,我们决定将图像height设置为固定大小,但将width设置为自动。 This way, the image will not be squeezed inside the container if its ratio different is too much from the container ratio. 这样,如果图像的比率与容器比率相差太大,则图像不会被挤压在容器内部。

Then, I set the container's text-align to center in order to center the image. 然后,我将容器的text-align为居中,以使图像居中。 But, this only works for images smaller than the container. 但是,这仅适用于小于容器的图像。 If the image is still bigger than the container (after resize), the image is aligned to the left instead. 如果图像仍大于容器(调整大小后),则图像将向左对齐。

The jsffidle example . jsffidle示例

NOTE: Using background-image is not a solution because resizing background image currently is still not supported by many browsers (especially IE and some Chinese browsers). 注意:使用背景图像不是解决方案,因为许多浏览器(尤其是IE和某些中文浏览器)目前仍不支持调整背景图像的大小。

Hope there is enough information here. 希望这里有足够的信息。 So, how do I center the image in this situation? 那么,在这种情况下如何使图像居中?

I have found another solution 我找到了另一种解决方案

<style type="text/css">
    .container {
        width:600px; //set how much you want
        overflow: hidden; 
        position: relative;
     }
     .containerSecond{
        position: absolute; 
        top:0px; 
        left:-100%; 
        width:300%;
     }
     .image{
        width: 800px; //your image size 
     }           
</style>

and in body 并且在身体上

<div class="container">
   <div class="containerSecond">
       <image src="..." class="" />
   </div>
</div>

This will center your image whenever your container is bigger or smaller. 每当容器变大或变小时,这将使图像居中。 In this case your image should be bigger than 300% of container to not be centered, but in that case you can make with of containerSecond bigger, and it will work 在这种情况下,您的图片应大于容器的300%而不居中,但在这种情况下,您可以使containerSecond变大,它将起作用

I had similar problem, but the solution was about to crop right and left margin, while the image should be centered. 我遇到了类似的问题,但是解决方案是要裁剪左右边缘,而图像应居中。 Smaller images are stretched. 较小的图像被拉伸。

My solution is also in this JS Fiddle: http://jsfiddle.net/david_binda/9tTRQ/ 我的解决方案也在此JS Fiddle中: http : //jsfiddle.net/david_binda/9tTRQ/

HTML HTML

<div class="thumb-wrapper">
   <a href="" title="" class="img">
      <img src="http://upload.wikimedia.org/wikipedia/commons/4/40/Tectonic_plate_boundaries.png" alt="" />    
</a>
</div>

CSS CSS

.thumb-wrapper{
    width: 200px; // desired thumbnail width
    height: 150px; // desired thumbnail height        
    overflow: hidden;    
    position: relative;
    margin: 0 auto;
}
.thumb-wrapper .img{
    display: block;    
    position: absolute;
    width: 300px; // should be wider than final thumbnail
    height: 150px; // desired thumbnail height
    left: 50%;
    margin-left: -150px; // half of above defined width eg. 300/2 = 150
}
.thumb-wrapper .img img{
    width: auto !important;
    max-width: 300px !important; // should be wider than final thumbnail
    min-width: 200px !important; // desired width of thumbnail
    height: 150px !important; // desired thumbnail height
    margin: 0 auto;
    display: block;
}​

The solution that I've found is: 我发现的解决方案是:

img{
    position:absolute;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%);//You have to add all the prefixes 
                                  //of transform
}
div.container{
    position:relative;
    overflow:hidden;
}

You would use max sizes: 您将使用最大尺寸:

img {
    max-width: 200px;
    max-height: 200px;
}

Take a look: http://jsfiddle.net/fabianhjr/zW6eh/ 看看: http : //jsfiddle.net/fabianhjr/zW6eh/

Edit: still having centring problems, I will get back to you on that. 编辑:仍然有居中的问题,我会在那儿给您回复。

Okay, I think this is your best solution. 好的,我认为这是您最好的解决方案。

You set your wrapper around each image to display: table; 您在每个要display: table;图像周围设置包装器display: table; and then one more wrapper inside that with a display: table-row; 然后在其中的另一个包装中display: table-row; and set your img 's to display: table-cell 并将您的img设置为display: table-cell

This way you can resize anyway you like while keeping the ratio. 这样,您可以在保持比例的情况下随意调整大小。

http://jsfiddle.net/zW6eh/17/ http://jsfiddle.net/zW6eh/17/

You can also simply set your height: to 200px; 您也可以简单地将自己的高度设置为:200px; This will keep your width auto by default. 默认情况下,这将使您的宽度保持自动。

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

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