Search This Blog

Saturday, 20 May 2017

CSS3 - Automatically contrast font color according to background image

Requirement statement: For a dynamically changing background, implement a contrasting font

Well, this was something I was trying out, where there was a html container changing images at random with some writing embedded in it.

To make it very simple and scalable, I needed a css that would automatically detect the background shades and adjust the font color of the writing overlayed.

So let me explain this.
I have a <div> in my html that keeps loading random images as its background with each request.
And I have some description embeded inside this <div>.

There are two ways to tackle this scenario

  1. Write javascript to check the background shades and accordingly contrast the font color.
  2. Write a css that does the same.


As everyone, I was more interested in the CSS way of controlling styles.

It is a good practice to keep the behavioral aspects put into javascript / jquery / angular and so on, while the styling aspects to be controlled purely by css or frameworks of the likes.

In CSS, I  found two ways.

1. Use the CSS property: mix-blend-mode

  • This property blends a given element with its backdrop
  • This means, I could have a <span> with some description inside a <div> with a background image, and this property will automatically manage the font color of the <span> according to its background.
  • If the background changes dynamically, this property will dynamically assign font-color to the <span> such that it blends with its background.
  • It can take several values, all of which are available at this link
  • But this property has a drawback with respect to browser compatibility. If you are targetting more than one browsers and IE is one of them, then this property will not show the desired effect.
2. Use the CSS property: filter

  • This property also helps in blending the font with its backdrop. Basically it provides access to effects like blur / sepia / invert colors or anything related to shifting of colors before rendering them to the browser.
  • This property works well with webkits and is also supported by all browsers including IE. The latest version of IE is recommended.
  • Please refer to the details of filter property at this link
So this is what is done

1. In the Html file, we have the following markup

 <!DOCTYPE html>  
 <html>  
  <head>  
   <link rel="stylesheet" href="style.css">    
  </head>  
  <body>  
   <div class='bg'>  
    <span class='bg-contrast-font'>Beautiful Scenary</span>  
   </div>  
  </body>  
 </html>  

2. In style.css, the following css classes are added


 .bg{  
   background: url('https://unsplash.it/350/300/?random') no-repeat;  
   box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);  
   height: 400px;  
   opacity:0.6;  
   color: white;  
   padding: 10px;  
   font-size: 40px;  
 }  
 .bg-contrast-font{  
  position: relative;  
   background-image: inherit;  
   -webkit-background-clip: text;  
   color: white;  
   -webkit-filter: invert(100%) sepia(100%);  
   filter: invert(100%) sepia(100%);  
   text-align: left;  
 }  

Explanation of the css

  1. The css class .bg is used to 
    • randomly load backgrounds using the background property
    • The div forces the font color to be white (color:white). This is not required. Just shown for demonstration
    • A base height and padding is given so as to view the background image
  2. The css class .bg-contrast-font is used
    • Provide a contrasting font color to the <span> inside the <div>
    • Note, that this has a background-image:inherit, which means we are inheriting the <div> background for the <span> also
    • There are two properties contributing to this.
      • background-clip: In the above sample, the webkit is applied to have browser compatibility. This property does something similar to "crop"  effect in photoshop. It clips a part of the element as specified. It can have the following values
        • background-clip: border-box;
        • background-clip: padding-box;
        • background-clip: content-box;
        • background-clip: text;
      • We are using "text" option to clip the text part of the span element.
      • Try removing background-clip in the plunk and running it in the browser. You will be able to spot the difference.
      • filter: The filter property as specified above, helps us work with color corrections. In this case, the color correction used are
        • invert(): Defines the proportion of inversion of colors. invert(100%) completely invert the colors with respect to its background.
        • sepia(): Converts a given input image to sepia form.
  3.  The output
             

An example of the usage of filter property is given in the following plunk.
https://embed.plnkr.co/hnNhYiOrvoCaov5BYMHs/

Try the following also

  1. Give the color: transparent  in the css class bg-contrast-font
  2. Try removing background-clip in the plunk and running it in the browser. You will be able to find the reason it is used.
Just hope this helps you.



Way of Life


Time is precious, 
But life is georgeous.

Mirth is present,
With sadness as an assistant

What we can infer,
Is that Life is a mixture,

Of happy and sad moments,
One cherishes in the present.

You need not be depressed,
For hard moments you've suppressed

For, If you are keen,
On what you had been,

You'll get the gist,
In your list,

That, the hardest moments,
Gave you most valuable lessons !

Composed By,
Samprada Nagasubramanian

** Featured as world's top 30 poems at a contest (www.poetry.com) in 2002. Re-published few months ago **


Creative Commons License
Way of Life by Samprada Nagasubramanian is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
Based on a work at http://inspireinnovativelearning.blogspot.com/2017/05/way-of-life.html.

Chakravyuh mukti

I made one mistake,
Realised it late,
Its correction,
Led to confusion, aggression and separation.

All done for the good,
But I remain misunderstood,
As the other was not able to see,
The good,
As I could.

So I stay in the bad books of thee,
But so I let it be.
As its all done with an intention,
For good, peace but stays unmentioned.

Why was it so
That I let it go
Unexplained, incomplete?
I really didnt intend to mistreat.
Sometimes words cannot explain it all,
Time unfolds to solve it once and for all.

This put me into a chakra,
Of Karma

And now time exchanged the role,
Where I now fell into a hole,
That had people with heart of evil,
Heartaches in store to reveal

I suffered then and I suffered now,
Left alone but do not know how.
I would separate the evil,
From associated people.

I tried to show them light,
Or eradicating the dark in my fight,
Unrealised when I slipped into the bad,
As I was so insecure, hurt and sad.

Then, as I introspected,
And listened and waited,
For help from God, my guide,
That resides as my inner voice,
On the path of good to get me back,
And so with God, I converted the setback,
Into a stronger comeback!

By now I had travelled long in years,
Alone I was but had faced my fears,
The innocence in me saved and guarded,
By strong intellect and awareness added.

When put into garbage,
Remember, in it you must not age.
Clean it or keep yourself away,
Or you may get dirty in it some day.

So is it in life,
Garbage could be people, situations that might,
Make you one of them,
If, not careful then.

Do good even if ridiculed,
For the gift of inner peace is the most beautiful.

And so peaceful was this gift of my new home,
In it I experienced bliss more and more.
A house made from bricks, cement and tiles,
That could make it my home built with love and life.

The once evil associations gradually dispelled,
Cycle of karma kept me happy and well,
And brought me out of the hole,
That I was once forced to fall.

Thankful, I pray to Almighty,
To always strip away all evil, ignorant people and the sorts,
From my life, once and for all.

Let me now come to you O God,
Be free from Karmic cycle with its good and odds,
Travelling the path of goodwill and delight,
Permit me to merge in your divine light!

Composed By,
Samprada Nagasubramanian



Creative Commons License
Chakravyuh Mukti by Samprada Nagasubramanian is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
Based on a work at http://inspireinnovativelearning.blogspot.in/2017/05/chakravyuh-mukti.html.