Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

currentColor with opacity

Tags:

html

css

Is there a method in which I can change the opacity of a border which is being set to inherit it's color from currentColor? i.e. inherit currentColor in #inner2 and set it's opacity to 0.25.

Searching for a pure css solution please.

For example, something which resembles the following:

#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>
like image 554
Zze Avatar asked Jul 13 '17 08:07

Zze


People also ask

What is normal opacity?

The value of the opacity property ranges between 0 and 1. The lower the value of the opacity property, the more transparent an element will appear. So, a value of 0 would make an element fully opaque or fully transparent, and a value of 1 would make an element appear as normal.

What does color opacity mean?

The term opaque originated from the Latin, meaning 'dark' meaning 'not transparent' and opaque substance does not let any light pass through at all. A paint that is opaque will give a solid colour. Blacks and whites are always opaque and any colour mixed with them will become more opaque.

What is an opacity of 1?

The opacity-level describes the transparency-level, where 1 is not transparent at all, 0.5 is 50% see-through, and 0 is completely transparent.

What is currentColor?

The currentcolor keyword represents the value of an element's color property. This lets you use the color value on properties that do not receive it by default. If currentcolor is used as the value of the color property, it instead takes its value from the inherited value of the color property.


2 Answers

You are confusing currentColor value with inherit which is the default. you don't use currentColor for border properties because that it the default value for the border. you only use it for background.

#inner1 and #inner2 both inherit from the closest parent which has a color set to it (red) and the border is using that color by default.

Below solution will work 100% of the time, regardless of the color's source
(inline style attribute, external CSS or distant ancestor inheritance):

#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>
like image 114
vsync Avatar answered Sep 26 '22 10:09

vsync


You can use css-variables to achieve similar behaviour:

#outer{
  --border-r: 255;
  --border-g: 0;
  --border-b: 0;
  color: rgba(var(--border-r),var(--border-g),var(--border-b),1);
}

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

#inner2{
  color: rgba(var(--border-r),var(--border-g),var(--border-b),0.25);
}
<div id='outer'>
  <div id='inner1' class='inner'></div>
  <div id='inner2' class='inner'></div>
</div>
like image 45
fen1x Avatar answered Sep 22 '22 10:09

fen1x