lighten the background colour from another class
Clash Royale CLAN TAG#URR8PPP
lighten the background colour from another class
Can we use lighten, Darken CSS attributes without colour value?
I have a class with the background colour
Ex:
.master
background-color:green;
<div class="master">Master</div>
I have to lighten the background of the "Div" using a different CSS class. How can I do that?
Note:
I have a few themes and predefined background colours so I have to use those colours and have a shade of it using a different class
Opacity is not what I'm looking for.
Can you use SCSS ?
– Arkellys
Aug 10 at 6:17
@Arkellys I'm using LESS in my project
– Raashid A.
Aug 10 at 6:18
@RaashidA. Okay, have you tried to use the
lighten()
function ? That the only thing I can think of right know.– Arkellys
Aug 10 at 6:20
lighten()
@Arkellys Yes that doesn't help my problem :(
– Raashid A.
Aug 10 at 7:55
4 Answers
4
You can consider a pseudo element to create another layer and inherit the background-color
then you can apply filter without any issue:
background-color
.master
background-color:green;
.master2
background-color:red;
.light,
.dark
position:relative;
z-index:0;
.light:before,
.dark:before
content:"";
position:absolute;
z-index:-1;
top:0;
left:0;
right:0;
bottom:0;
background-color:inherit;
filter:brightness(200%);
.dark:before
filter:brightness(50%);
<div class="master light">Master</div>
<div class="master2 light">Master</div>
<div class="master dark">Master</div>
<div class="master2 dark">Master</div>
Thank you this helped me to sort my problem :)
– Raashid A.
Aug 13 at 5:53
You can use the filter
CSS property to tweak the brightness as follows:
filter
.master
background-color: green;
.lighten
filter: brightness(150%);
.darken
filter: brightness(50%);
<div class="master">Master</div>
<div class="master lighten">Master lighten</div>
<div class="master darken">Master darken</div>
Filter applied for the whole div on top of the layer, its mean filtering applied for content as well like opacity. I need to reduce the background colour only.
– Raashid A.
Aug 10 at 5:23
I think it is quite easy to add a different class so that you can lighten the background of your div element. Try this example,
.master-light
background-color:rgba(0,0,0,0.5); /*where 0.5 stands for 50% opacity*/
When you have 0 values it brings the black colour in, in my case, I can't have a colour value in second class. I just need to lighten the background colour which applied through another class.
– Raashid A.
Aug 10 at 5:26
This might help you, I think this is the only way to achieve your goal.
As you can see both the images were same, no filter is applied to the content.
.master
color:#fff;
position:absolute;
top:10px;
left:10px;
.mmaster
-webkit-filter: brightness(200%);
background:green;
position:relative;
padding:80px 0;
<div class="mmaster"></div>
<div class="master"><img src="https://www.w3schools.com/css/bgdesert.jpg" alt="desert">Desert</div>
<img src="https://www.w3schools.com/css/bgdesert.jpg">
The filter doesn't meet my requirement its filter whole content inside the div I only need to reduce the background colour to lighten or darken.
– Raashid A.
Aug 10 at 6:16
I've edited it with an image try now
– Viira
Aug 10 at 6:22
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Welcome to SO, Please add your code into snippet, so someone has quickly fixed your issue. Have a nice day!!
– jaydeep patel
Aug 10 at 5:08