简体   繁体   English

CSS 如何根据高度缩放和纵横比宽度?

[英]CSS how to Scale and Aspect Ratio width based on height?

assume the height is 80% of parent div, and I want the width is twice the height.假设高度是父 div 的 80%,我希望宽度是高度的两倍。 so can I do this:所以我可以这样做:

.scal{ height:'80%', width:2 x height } .scal{ 高度:'80%',宽度:2 x 高度 }

can it be possible or I have another way to achieve this?有可能还是我有另一种方法来实现这一目标?

I think what you're asking can be achieved in CSS provided you know the height of the parent element如果您知道父元素的高度,我认为您的要求可以在 CSS 中实现

CSS CSS

width: calc(whateverElementsHeightInUnits * 2)

However I think the best way to do this is with jQuery as below但是我认为最好的方法是使用 jQuery 如下

Jquery Jquery

$(window).resize(function(){
   $('.scal').css({ width: $(whateverElement).innerHeight() * 2 });
});

You can do something approaching this using a combination of CSS custom properties (variables) and the calc function:您可以使用CSS 自定义属性(变量)calc function 的组合来做一些接近此的事情:

 :root { --rect-height: 100px; }.rect { background: red; display: block; height: var(--rect-height); width: calc(var(--rect-height) * 2); }
 <div class="rect"></div>

暂无
暂无

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

相关问题 仅使用CSS保持基于宽度和高度的宽高比 - Maintaining aspect ratio based on width and height with CSS only 如何根据 HTML CSS JavaScript 中的纵横比调整高度或宽度? - How can I fit either height or width based on aspect ratio in HTML CSS JavaScript? 如何调整大小<canvas>基于窗口宽度/高度并保持纵横比? - How to resize <canvas> based on window width/height and maintain aspect ratio? CSS Aspect Ratio - 根据动态父元素高度设置子元素的宽度 - CSS Aspect Ratio - set width of child element based on dynamic parent height 保持div的纵横比但在CSS中填充屏幕宽度和高度? - Maintain aspect ratio of div but fill screen width and height in CSS? 如何在使用 ZC7A62​​8CBA22E28EB17B5F5C6AE2A266AZ 纵横比属性时设置最大高度/宽度? - How to set max height/width while using css aspect ratio property? 如何让浏览器遵守应用 css 的宽度和高度 html 属性的纵横比? - How to get browsers to honor the aspect ratio from width and height html attributes with applied css? 如何在html&css中居div,始终保持纵横比,并填充最大屏幕宽度/高度? - How to center a div in html & css, always keep the aspect ratio, and fill max screen width/height? 如何在保持纵横比的同时缩放背景图像以适应屏幕的整个宽度? 当我使用“封面”时,高度被切断 - How to scale a background image to fit the full width of the screen while maintaining aspect ratio? Height is cut off when I use "cover" css根据父高度保持1:1(平方)的宽高比 - css to keep aspect ratio of 1:1 (square) based on parent height
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM