我需要在 CSS 中获取另一个属性的值。假设我在 CSS 文件中有这个:
.something {
width: 256px;
}
然后在 something2 中我想使用值:
.something2 {
width: .something.width;
}
如果没有 JavaScript,我怎么能做到这一点?我需要这个的原因是因为我使用的是 CSS 3 动画:
@-webkit-keyframes somethinghere {
0% {
background-position: -48px 0px;
}
100% {
background-position: .something.width 0px;
}
}
和动画结束背景的 x 值我想成为“某物”的宽度。这可能在 CSS (3) 中实现吗?即使它是规范草案或只有一个浏览器支持它,我也想听听。
提前致谢。
最佳答案
这是 CSS 做不到的。但是你可以用 Sass 来实现。或 Compass .
来自 Sass 的引述:
Sass makes CSS fun again. Sass is CSS, plus nested rules, variables, mixins, and more, all in a concise, readable syntax.
基本上它是另一种类似于CSS的语言。然后将这些文件编译为 CSS 文件。因此,您需要一个额外的步骤来创建 CSS 文件,但您创建它们所使用的语言更强大。
关于css - 棘手的 CSS 问题——从其他属性获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2190546/