我的意思是,字面上说,就像有一幅肖像图像,并将其设置为景观大小,以及额外的背景,如填补,这样图像就不会看起来奇怪?
例如,这张是原始图片:
现在它被扭曲了,因为我将高度和宽度设置为固定值如下:
有什么办法可以把这幅肖像画变成一张基于陆地地形的照片吗?一切都显示得很完美--好吧--不仅仅是改变分辨率,我的意思是,就像会调整图片的大小,然后在图片上增加额外的背景,这样图片就不会显得奇怪了吗?
在HTML,CSS,PHP,JS中有可能吗?
发布于 2017-06-07 22:10:27
尽管其他的答案都表明了这一点,但我还是会说,我认为在这种情况下为每个图像添加CSS规则是不合适的,因为它看起来像是在创建某种图库。最好在HTML中直接使用<img>
标记,这样就可以动态生成src
,而不会使事情变得过于复杂:
.container {
border: 1px solid #777;
text-align: center;
padding: 10px;
margin: 10px;
}
.container img {
max-height:100%;
max-width: 100%;
vertical-align: middle;
}
.helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
<div class='container' style="width:200px;height:200px">
<span class="helper"></span><img src="https://i.stack.imgur.com/5ZNYR.png">
</div>
<div class='container' style="width:400px;height:200px">
<span class="helper"></span><img src="https://i.stack.imgur.com/5ZNYR.png">
</div>
<div class='container' style="width:200px;height:400px">
<span class="helper"></span><img src="https://i.stack.imgur.com/5ZNYR.png">
</div>
只需确保.helper
和img
之间没有任何空白。它应该适用于容器和图像的任何尺寸。
发布于 2017-06-07 21:33:24
这将调整图像的大小,使其适合不失真的比率。
.container {
width: 200px;
height: 100px;
background-image: url("https://i.stack.imgur.com/5ZNYR.png");
background-size: contain;
background-repeat: no-repeat;
background-position: 50% 50%;
background-color: blue;
}
<div class='container'></div>
发布于 2017-06-07 21:33:22
试试这里的代码
html
<div class="img-con">
<img src="http://www.mybligr.com/wp-content/uploads/2017/01/most-beautiful-tiger-animals-pics-images-photos-pictures-6.jpg" alt="" />
</div>
css
.img-con
{
height:300px;
width:500px;
border:1px solid black;
position: relative;
max-height:300px;
max-width:500px;
padding:30px;
}
.img-con > img
{
position: absolute;
left:0;
right: 0;
top:0;
bottom:0;
margin:auto;
max-height:inherit;
max-width:inherit;
}
以下是参考链接代码链接
https://stackoverflow.com/questions/44427262
复制相似问题