简体   繁体   English

使图像居中而不使用CSS拉伸和调整大小

[英]Center the image without stretching and resizing using css

I was trying to bring the position of images inside div to center in a gallery. 我试图将div中图像的位置放在画廊的中心。 look at the following link http://geeksdoor.in/ghouri/portfolio.php 请看以下链接http://geeksdoor.in/ghouri/portfolio.php

.gallery-item, .grid-sizer {
    width:25%;
    height:200px;    
    display:block; 
    position:relative;
    overflow:hidden; 
}

.gallery-item img, .grid-sizer img {
    position:relative;
    top:0;
    left:0; 
    width:100%;
    max-height:100%;
    display: block;
    overflow:hidden;
    z-index:1;
}

and in this page http://geeksdoor.in/ghouri/portfolio-single.php?type=int&imgnumb=0 并在此页面http://geeksdoor.in/ghouri/portfolio-single.php?type=int&imgnumb=0

position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
/* background-size: cover; */
background-attachment: scroll;
background-position: center;
background-repeat: no-repeat;
background-origin: content-box;

the 1st image is good and other images in slider are getting stretched. 第一张图像好,并且滑块中的其他图像被拉长。

please help if there is any wrong in the CSS!! 如果CSS有任何错误,请提供帮助!! and Thanks in advance guys. 并预先感谢大家。

First of all, please compress your images. 首先,请压缩您的图像。 I'm loading images of 4 megabytes, which absolutely destroys performance and eats through your mobile data bundle. 我正在加载4兆字节的图像,这绝对会破坏性能并吞噬您的移动数据包。

Secondly, it's probably better to resize your images up front to prevent weird stretching and deforming. 其次,最好预先调整图像大小,以防止奇怪的拉伸和变形。 Adding CSS now would only mess up your images because they are in no way fit to fit in your containers. 现在添加CSS只会弄乱您的图像,因为它们根本不适合您的容器。

Unfortunately, the pictures doesn't even load with my poor internet connection. 不幸的是,我的互联网连接不畅,图片甚至无法加载。 Maybe you could use tinyjpg to compress your images. 也许您可以使用tinyjpg来压缩图像。

But maybe try width: 100% and height: auto;? 但是也许尝试一下width:100%and height:auto ;? Or, if it is an option, try it using jquery. 或者,如果可以选择,请使用jquery尝试。

You need to resize your images to a standard size, so that all your images can be in a same size and resolution.. 您需要将图像调整为标准尺寸,以便所有图像都可以具有相同的尺寸和分辨率。

For you portfolio i can suggest the size of (1980px X 960px) .. and use the "background-position: center;" 对于您的投资组合,我可以建议(1980px X 960px) ..的大小,并使用“ background-position:center;” property so that you can easily align in tablet and mobile screen sizes, by just setting the height of the div as per your image visibility.. 属性,以便您只需根据图像的可见性设置div的高度即可轻松调整平板电脑和移动屏幕的尺寸。

Hope it helped you.. 希望它对您有所帮助。

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

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