html - 不透明的当前颜色

标签 html css

是否有一种方法可以更改设置为从 currentColor 继承其颜色的边框的不透明度?即继承 #inner2 中的 currentColor 并将其不透明度设置为 0.25

请搜索纯 css 解决方案。

例如,类似于以下内容:

#outer{
  color: rgba(255,0,0,1);
}

.inner{
  display: block;
  width: 100%;
  height: 10px;
  margin-bottom: 5px;
  border: 2px solid currentColor;
}

#inner2{
  /* This is where I want to inherit current color */
  /* ... but still set it to 0.25 opacity */
  border-color: rgba(255,0,0,0.25);
}
<div id='outer'>
  <div id='inner1' class='inner'></div>
  <div id='inner2' class='inner'></div>
</div>

最佳答案

您将 currentColor 值与 inherit 混淆了,后者是默认值。您不使用 currentColor 作为边框属性,因为它是边框的默认值。你只将它用于背景

#inner1#inner2 都继承自最近的父级,该父级设置了颜色(红色),边框默认使用该颜色。

无论颜色的来源如何
(内联style 属性、外部CSS 或远祖继承),以下解决方案100% 有效:

#outer{ color:red; }
#inner1, #inner2{ 
    padding: 2em; 
    margin-top: 1em;  
}
#inner1{ border:5px solid; } 

#inner2{ position:relative; }

#inner2::before{  
  content:'';
  position:absolute;
  top:0;
  right:0;
  bottom:0;
  left:0;
  border:5px solid;
  opacity:.5;
}
<div id='outer'>
  <div id='inner1'>inner 1</div>
  <div id='inner2'>inner 2</div>
</div>

另请参阅我的 other answer here它使用最先进的语法。

关于html - 不透明的当前颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45075231/

相关文章:

html - 如何使用 CSS float 将此 HTML 显示为表格?

html - 表格的第一列已卡住,另一列可滚动

javascript - 如何在半圆中移动一个div

css - 在 Safari 中使用 webkitTransform 缩放 iframe 会创建白色边框

jquery - 按高度和重量自动调整容器图像

javascript - 尝试导致循环的 window.location 或 window.location.href 重定向

javascript - JQuery Daterangepicker onchange 事件

windows - 将 Web 应用程序加载为 Windows 桌面应用程序的简单/最佳方法?

css - IE7 和 IE8 在 TTF 中抛出 404 错误 - 字体? CSS?

html - 制作父级包含列的 Bootstrap 导航栏宽度