繁体   English   中英

具有100%高度并向左右延伸的图像

[英]image that has 100% height and stretches to the right and left

我正在寻找CSS(首选)或javascript / jquery,其中我的图像高度为100% 图像应保持宽高比,并且如果屏幕的宽度小于图像的宽度,则应从左右均匀裁剪。 基本上,图像应在div居中

在此处输入图片说明

试试object-fit:cover; 它会裁剪您的图像。

#image {
    width: 100%;;
    height: 200px;
    object-fit:cover
 }

加上@media您可以使图像适应屏幕分辨率。

  1. 试试Flexbox:

 .container { display: flex; justify-content: center; align-items: center; } .image { flex: 0 1 auto; } 
 <div class="container"> <img class="image" src="http://blog.conservation.org/wp-content/uploads/2014/06/ci_19290600_cropped.jpg" /> </div> 

  1. 您可以将图像作为DIV的背景:

 .image { background-repeat: no-repeat; background-position: center center; height: 500px; /* whatever you need */ } 
 <div class="image" style="background-image: url(http://blog.conservation.org/wp-content/uploads/2014/06/ci_19290600_cropped.jpg);"></div 

暂无
暂无

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

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