我正在尝试计算父元素中子元素应用的正确比例,但我无法在transform: scale
CSS函数内使用calc()
。
我已经用javascript完成了这个函数,但我对纯CSS解决方案很感兴趣,以尽可能避免用脚本使页面变得臃肿。
示例: CSS
.anyclass{
height:250px; /*this value can change dinamically */
transform:scale(calc(100% / 490 )); /*is using height value*/
}
此定义返回一个无效的属性值。其他用途如翻译:
.anyclass{
transform:translate(calc(100% - 50px ));
}
没有问题地运作。 有没有办法只使用CSS使用calc计算出一个div的正确比例?
编辑:为了更好地解释,这不是关于在0和1之间找到一个值的问题,因为计算可以做到这一点。如果我使用百分比,我只会得到无效的属性值。
测试1 我尝试使用CSS3变量,在缩放函数中没有结果。
--height: calc(100% + 0px);
/* height: calc(var(--height) / 490); Calculates the right value: 0.5 */
--soom: calc(var(--height) / 490);
/* height: var(--soom); has the right value: 0.5 */
transform: scale(var(--soom)); /*Does not operate in the value stored in --soom*/
在Chrome中,它似乎可以接受数值,但无法操作比例。
ZOOM CSS属性好像只有在Chrome中才能正常工作。这个是好的。
zoom: calc(100% / 1.490); /* It needs the original value to be divided by 1000 to work properly*/
示例:
在链接地址MiWeb中, 具有类名:class="cs_6c537e46d973809bcbd9abb882d9c7db cssprite"的DIV元素使用精灵作为背景图像,因此具有background-image属性。 CSS
background-position: 0 -840px;
width: 800px;
height: 490px;
background-image: url(https://cdn.arturoemilio.es/wp-content/cache/CSS_Sprite/93db60ac3df9134d96d56dd178d4ebf3279fdb39_S.png);
background-repeat: no-repeat;
display: inline-block;
position: initial;
现在原始图像比可见的div
(高度约为250px)
要大,我想只使用CSS来缩放图像,因为我希望避免使用JS以提高与禁用JS的浏览器的兼容性。我希望将背景图像缩放到正确的大小,其正确值应为250px / 490px (scale(calc(100% / 490))。 CSS是在输出之前和CMS的其他部分生成的,因此我不知道在生成CSS规则时父DIV的实际大小。
et_pb_portfolio_image
的width
和height
吗? - Mihai Ttransform: scale
中的calc
是问题所在。 - Luke