简体   繁体   English

保持背景图像全高,同时仅裁剪边以保持比例

[英]Keep background image full height while cropping only sides to keep ratio

I have a mobile page with full screen background image. 我有一个带全屏背景图片的移动页面。

I have one image for portrait and one for landscape. 我有一个肖像图像和一个景观图像。 My mission is to keep image height full screen while cropping the sides to fit screen and keep aspect ratio. 我的任务是保持图像高度全屏,同时裁剪侧面以适应屏幕并保持纵横比。

I tried this css tricks post : 我试过这个css技巧帖子

  html { 
  background: url(images/bg.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  }

While working great (as I needed) on portrait, it doesn't meet my needs on landscape: It keeps the image full screen width and crops it from the top and bottom (My requirement from the designers is keep full screen height and crop the sides). 虽然在肖像上工作得很好(我需要)但它不能满足我对风景的需求:它保持图像全屏宽度并从顶部和底部裁剪(我的要求来自设计师保持全屏高度并裁剪侧)。 There are a lot of examples on the internet for keeping image's ratio etc. (for example ), but I could find a solution for my situation... 互联网上有很多例子用于保持图像的比例等( 例如 ),但我可以找到适合我情况的解决方案......

Thanks! 谢谢! Yaniv 的Yaniv

This managed to work for me, this is based on the image being a large enough size to cover large screen sizes... 这设法适合我,这是基于图像足够大,以涵盖大屏幕尺寸......

 html { 
  background: url(http://lolcat.com/images/lolcats/1338.jpg) no-repeat center center fixed; 
  -webkit-background-size: auto 100%;
  -moz-background-size: auto 100%;
  -o-background-size: auto 100%;
  background-size: auto 100%;
  }

Jsfiddle here Jsfiddle在这里

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

相关问题 保持div与背景图像的比例高度响应 - Keep ratio height of div with background image in responsive 保持图像居中,同时保持100%的高度,并且仅延伸/裁剪侧面 - Keeping an image centered, while maintaining 100% height and only extending/cropping the sides SVG图像作为图案-全宽-保持比率 - SVG image as pattern - full width - keep ratio 调整图像大小 - 保持比例 - 没有背景图像 - Resize image - keep ratio - without background image Flexbox图像缩放到高度并保持纵横比 - Flexbox image scale to height and keep aspect ratio 如何在由8个不同的“棱镜”图像组成的全宽背景上保持100%的高度+宽高比 - How to keep 100% height + aspect ratio on full width background consisting of 8 different “prism” images SVG图像-固定高度-保持比例-切片 - SVG image - fixed height - keep ratio - slice 高度:100%不会保持图像的比例 - height: 100% won't keep ratio of image 保持图像比例的最佳方法,同时具有响应能力并获得最小高度 - Best way to keep the image ratio, while it is responsive and gets a min-height 如何保持背景图像的宽高比并分别选择在宽度和高度之间设置为标准的哪一侧? - How can I keep the width-height ratio of the background image and respectively choose which side to set to standard between width and height?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM