html - 如何在 CSS 中按百分比定义不透明度?

标签 html css

是否可以在 CSS 中按百分比(例如 30%)定义 CSS 中的不透明度?似乎不起作用,现在我只能按小数点进行。

https://css-tricks.com/almanac/properties/o/opacity/

.test{
   opacity: 0.3;
}

预期目标:
.test{
   opacity: 30%;
}

最佳答案

根据规范,使用百分比是完全有效的 css:
https://developer.mozilla.org/en-US/docs/Web/CSS/opacity#Values

alpha-value A number in the range 0.0 to 1.0, inclusive, or a percentage in the range 0% to 100%, inclusive, representing the opacity of the channel (that is, the value of its alpha channel). Any value outside the interval, though valid, is clamped to the nearest limit in the range.



因此,根据规范,其中任何一个都应该没问题:

.foo {
  opacity: .3;
}

.foo {
  opacity: 30%;
}

但是请记住,如果您使用的是 Sass,它不会处理 percentage值,并将其编译为 1% .

关于html - 如何在 CSS 中按百分比定义不透明度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57012564/

相关文章:

jquery - 单击时删除 html 图像上的蓝色突出显示

html - CSS中的响应式水平图像布局

jQuery 动画背景位置,在 ie 中不起作用

javascript - 如何将 <stdio.h> 视为简单文本而不是 html 标签

html - 使用 Bootstrap 类,为什么我的子 div 在父级之外流动,而​​不是只占用剩余的高度?

html - 停止图像覆盖表格单元格大小

python - Django 模型自动填充外键

html - 流经下一行的总百分比为 100%

html - freemarker 中用于表单的 CSS

html - 以 % 调整图像大小不会改变父容器的宽度