We are changing the look and feel of our website. The colors of many elements are changing. Our secondary button is changing from a purple background with white text:
To a red border surrounding an inherited background:
We use this button in hundreds of locations on many pages. It resides in sections with various background colors (and a few background images).
With the old button treatment, I didn't have to worry about the background color when determining the text color. The text color was always white:
.secondary-button {
color: white;
}
Now the font color is going to have to change based on the background. It either needs to be white or purple depending on how dark the background is. The white just doesn't work on light colored backgrounds:
Because this button is used in so many places, I don't want to have to manually go through all of them and choose on a per button basis. Is there a way in CSS to choose one of two background colors based on the darkness of the background? Something like:
.secondary-button {
color: calc(background-color>#999?purple:white);
}
I found ways to accomplish this with JavaScript: Change text color based on brightness of the covered background area? and also language agnostic algorithms to calculate how dark a color is: Determine font color based on background color but I was unable to locate a pure CSS solution.
.secondary-button {color: white; }
and.lightBG .secondary-button {color: black;}
you are using the parent which has a lighter background to change the text colour of the button – Lysandercanvas
). – Cuccuckoldbackground-color:rgba(0,0,0,0.3);
– Lysanderlight-background
class to each button that is known to be placed on a light background, (which was already suggested). – Cuccuckold