简体   繁体   English

使用宽高比调整div的大小以适合内容CSS图像

[英]Resize div with aspect ratio to fit content css image

Hello I have a div and as background I have an image with width 1920px and height 1200px. 您好,我有一个div,作为背景,我的图像宽度为1920px,高度为1200px。 divs have 100% width and window.innerHeight on document ready; div在文档就绪时具有100%的width和window.innerHeight

The background css images have the follwoing rule: background-size: 100% auto !important; 背景CSS图像具有以下规则: background-size: 100% auto !important; so that they are responsive 这样他们就可以响应

What I would like to do is based on window.resize calculate the new height for the div element and set it so that it matches the background-images css height. 我想做的是基于window.resize计算div元素的新高度并将其设置为与背景图像css高度匹配。

newHeight = (1200 * divWidth) / 1920

If I understand you right you don't need js 如果我了解你的话你就不需要js

background: url(../img/image.png) no-repeat center top fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;

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

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