DEV Community


Posted on • Originally published at


Change Text Color Based On Background Color Using Javascript

In this tutorial, we will see a change in text color based on background color using javascript. sometimes we have requirments to change text or font color dependent on background color and at that time if you are changing text color manully then this post will definitely help you.

In this article, we are learning how to change text color based on background color using jquery.

So, let's see how to dynamically change text color based on a background color javascript.

Create File And Add Below Code

Create an HTML file and the below code in your file,

    <title>Change Text Color Based On Background Color Using Javascript
    <script src=""> 

<body id="body" align="center"> 
    <h1 style="margin-top:20px;margin-bottom:20px;font-size:18px;">   
            <b>Change Text Color Based On Background Color Using Javascript - Techsolutionstuff</b>
    <p id="col_UP" style="font-size: 15px; font-weight: bold;"> </p> 
    <button onclick="col_Fun()" style="padding:10px;">
        click here 
    <div id="backG">Welcome to Techsolutionstuff</div> 

Enter fullscreen mode Exit fullscreen mode

Read Also : How To Create List And Grid View Using JavaScript


Now, add some CSS in head of the file.

#backG { 
    width: 200px; 
    color: white; 
    background: #000; 
    margin: 0 auto;
Enter fullscreen mode Exit fullscreen mode

Add Javascript Code

In this step, we are adding some jquery code in footer of file.

         var el_up = document.getElementById('col_UP'); 
        var rgbValue = [255, 0, 0]; 

        function setColor() { 
            rgbValue[0] = Math.round(Math.random() * 255); 
            rgbValue[1] = Math.round(Math.random() * 255); 
            rgbValue[2] = Math.round(Math.random() * 255); 
            var color = Math.round(((parseInt(rgbValue[0]) * 299) + 
                (parseInt(rgbValue[1]) * 587) + 
                (parseInt(rgbValue[2]) * 114)) / 1000); 
            var textColor = (color > 125) ? 'black' : 'white'; 
            var backColor =  
                'rgb(' + rgbValue[0] + ', ' + rgbValue[1] + ', ' 
             + rgbValue[2] + ')'; 

            $('#backG').css('color', textColor); 
            $('#backG').css('background-color', backColor); 

        function col_Fun() { 
Enter fullscreen mode Exit fullscreen mode

Read Also : Carbon Add Years To Date In Laravel

Output :


You might also like :

Top comments (0)

The JavaScript Brief

1. Top 5 MERN STACK projects to improve your practical understanding

Boost your MERN Stack development skills by undertaking interesting beginner projects. These five engaging projects cover web applications and range from social media website applications to geo-social networking maps. Hone your understanding and apply modern techniques backed up by hands-on experience.

2. How To Optimize Your React App’s Performance

Learn the best optimizing techniques to make your React applications faster and more efficient. Focusing on the identification of performance bottlenecks and common pitfalls to avoid, these optimization strategies will keep your applications running smoothly even when faced with growing complexity.

3. A story of let, const, object mutation, and a bug in my code

In the pursuit of bug-free code, explore an incident involving a mix-up between const and let, making sure your custom code works effectively with third

party documentation. Discover best practices on program flow and learn about JavaScript's unpredictable aspects to ensure your core code is robust.