简体   繁体   English

我想对 CSS 中的变量执行计算。 有没有办法在没有预处理器或 JavaScript 的情况下做到这一点? [公认]

[英]I want to perform calculation on a variable in CSS. Is there a way to do this WHITHOUT preprocessors or JavaScript? [ACCEPTED]

I want to do something like this.我想做这样的事情。 I want to make use of a single varible for multiple elements that have different values for the same property.我想对同一属性具有不同值的多个元素使用单个变量。

like this vvv喜欢这个vvv

:root {
  --brd_rad: 2rem;
}

#something {
  border-radius: var(--brd_rad);
}

#other-thing {
  border-radius: calc(var(--brd_rad) + 1);  /*obviously this doesn't work */
}

You need a unit in the value you're adding in the calc .您在calc中添加的值需要一个单位。 Is it meant to be 1px or 1rem or 1% ?它的意思是1px1rem还是1%

 :root { --brd_rad: 2rem; } #something { border-radius: var(--brd_rad); border:1px solid blue; height:100px; } #other-thing { border:1px solid red; height:100px; border-radius: calc(var(--brd_rad) + 1rem); }
 <div id="other-thing"></div> <div id="something"></div>

暂无
暂无

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

相关问题 如何使用内联 css 删除滚动条。我只想在 javascript 上执行此操作,因为我仅在 vanilla javascript 中创建了它 - how to remove scrollbar using inline css. I want to do it on just javascript as i created this in just vanilla javascript 我正在通过php获取VARCHAR变量,并希望使用HTML / CSS进行显示。 我如何自动设置其格式以使其不再是一句话 - I'm acquiring a VARCHAR variable through php, and want to show it using HTML/CSS. How do I auto format it so that it isn't one long sentence 我有一个JSFiddle,其中包括HTML,JavaScript和CSS。 有没有简单的方法可以将其实现到Dreamweaver页面中? - I have a JSFiddle that includes HTML, JavaScript, and CSS. Is there a simple way to implement it into a Dreamweaver page? Javascript / jQuery / CSS。 如何在目标div之外维护拖动事件? - Javascript/jQuery/CSS. How do I maintain drag events outside the target div? 如何在 HTML CSS 中使我的滚动始终位于页面的末尾。 我希望我的卷轴总是在页面的末尾 - How do I make my scroll always in the end of the page in HTML CSS. I want my scroll to always be at the end of the page jQuery + CSS。 如何计算innerHTML的高度和宽度? - jQuery + CSS. How do I compute height and width of innerHTML? 如何在CSS中获取页面宽度。 - How do I get the page width in a css. 我有一个包含图像和文本的 div,背景是我在 CSS 中添加的。然后我想要一个带有文本的新 div - I have a div containing images and text with a background I added in CSS. Then I want a new div with text js和css打开侧边栏的时候不想推送内容,如何独立 - I don't want to push the contents when sidebar is opened in js and css. How to make it independent 使用CSS。 水平居中放置可变数量的物品 - With CSS. Horizontally center a variable amount of items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM