Adjust the color of text using the <text-shadow> technique

The default 'text-shadow' for white text in a <div> is defined as follows: When the text color is changed to #627CA9 by clicking on it, I want the 'text-shadow' to be updated to match the new color.

text-shadow: 0 0 1px #FFFFFF, 0 0 2px #FFFFFF, 0 0 4px #FFFFFF; 

I want to include the following:

text-shadow: 0 0 1px #627CA9, 0 0 2px #627CA9, 0 0 4px #627CA9; 

I am unsure if this can be directly implemented using JavaScript. Any assistance on this matter would be greatly appreciated.

Thank you in advance for your help. Apologies for any inaccuracies in my English.

function resizeIframe(obj) {
  obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}


function changeColor(id) {
  var x = document.getElementById(id);

  if (x.style.color != "rgb(255, 255, 255)")
    x.style.color = "#FFFFFF";
  else {
    x.style.color = "#627CA9"; // forecolor
  }
}
.centered {
  cursor: pointer;
  font-family: Soft Press W00 Regular V1, cursive;
  color: #FFFFFF;
  text-shadow: 0 0 1px #FFFFFF, 0 0 2px #FFFFFF, 0 0 4px #FFFFFF;
  text-transform: uppercase;
  font-size: 80px;
  margin: 0;
  padding: 0;
  line-height: 100%;
  display: block;
  font-weight: 400;
}
<div onclick="changeColor('myid1'); return false;" id="myid1" class="centered">89 ROCK</div>

Answer №1

To achieve the desired effect, simply set the shadow to use the currentColor property and it will automatically inherit the color defined inside the color property.

function resizeIframe(obj) {
  obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}


function changeColor(id) {
  var x = document.getElementById(id);

  if (x.style.color != "rgb(255, 255, 255)")
    x.style.color = "#FFFFFF";
  else {
    x.style.color = "#627CA9"; // forecolor
  }
}
.centered {
  cursor: pointer;
  font-family: Soft Press W00 Regular V1, cursive;
  color: #FFFFFF;
  text-shadow: 0 0 1px currentColor, 0 0 2px currentColor, 0 0 4px currentColor;
  text-transform: uppercase;
  font-size: 80px;
  margin: 0;
  padding: 0;
  line-height: 100%;
  display: block;
  font-weight: 400;
}

body {
 background:pink;
}
<div onclick="changeColor('myid1'); return false;" id="myid1" class="centered">89 ROCK</div>

An alternative approach is to optimize your code using a class toggle:

function resizeIframe(obj) {
  obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}


function changeColor(id) {
  var x = document.getElementById(id);
  x.classList.toggle('color');
}
.centered {
  cursor: pointer;
  font-family: Soft Press W00 Regular V1, cursive;
  color: #FFFFFF;
  text-shadow: 0 0 1px currentColor, 0 0 2px currentColor, 0 0 4px currentColor;
  text-transform: uppercase;
  font-size: 80px;
  margin: 0;
  padding: 0;
  line-height: 100%;
  display: block;
  font-weight: 400;
}
.color {
  color:#627CA9;
}

body {
 background:pink;
}
<div onclick="changeColor('myid1'); return false;" id="myid1" class="centered">89 ROCK</div>

Answer №2

Insert the following code snippet into your CSS stylesheet:

   .forecolor_shadow{
        text-shadow: 0 0 1px #627CA9, 0 0 2px #627CA9, 0 0 4px #627CA9; 
        color:#FFFFFF;
    }

    .white_shadow{
        text-shadow: 0 0 1px #FFFFFF, 0 0 2px #FFFFFF, 0 0 4px #FFFFFF; 
        color:#627CA9;
    }

    .centered {
      cursor: pointer;
      font-family: Soft Press W00 Regular V1, cursive;
      text-transform: uppercase;
      font-size: 80px;
      margin: 0;
      padding: 0;
      line-height: 100%;
      display: block;
      font-weight: 400;
    }


Add the following script to your JavaScript file:

    function changeColor(id) {
      var x = document.getElementById(id);

      if (x.classList.contains('white_shadow')){
          x.classList.remove("white_shadow");
          x.classList.add("forecolor_shadow");

      } else {
          x.classList.remove("forecolor_shadow");
          x.classList.add("white_shadow");
      }
    }

// Implement the functionality in your HTML code 

    <div onclick="changeColor('myid1'); return false;" id="myid1" class="white_shadow centered">89 ROCK</div>

Answer №3

To achieve this effect, you can set the textShadow property of the element's style object like this:

element.style.textShadow = "0 0 15px #627CA9";

If you want to create an alternating shadow on and off behavior after multiple clicks, make sure to reset the textShadow style:

if(shouldShadow) {
    element.style.textShadow = "0 0 15px #627CA9";
} 
else {
    element.style.textShadow = ""; /* Remove shadow */
}

This code snippet will help achieve the desired effect:

function resizeIframe(obj) {
    obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}

</script>

<script>
function changeColor(id) {
    var element = document.getElementById(id);

if (element.style.color != "rgb(255, 255, 255)") {
    element.style.color = "#FFFFFF";

    /* Reset shadow */
    element.style.textShadow = "";
}
else {

    /* Define color for text and shadow */
    const color = "#627CA9";

    element.style.color = color;

    /* Set shadow with matching color */
    element.style.textShadow = `
    0 0 1px ${color}, 
    0 0 2px ${color}, 
    0 0 4px ${color}`;
}

}
<div style="cursor: pointer;font-family: Soft Press W00 Regular V1, cursive;color: #FFFFFF;text-shadow: 0 0 1px #FFFFFF, 0 0 2px #FFFFFF, 0 0 4px #FFFFFF; text-transform: uppercase;font-size: 80px;margin: 0; padding: 0;line-height: 100%;display: block;font-weight:400;" onclick="changeColor('myid1'); return false;" id="myid1" class="centered">89 ROCK</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The margin-left and margin-right in HTML CSS are not cooperating to center a div

I'm currently diving into the world of CSS and HTML, but I've hit a roadblock. The margin-left and margin-right in the ".logo" div class are refusing to center the div as expected. Despite conducting research and meticulously checking over the co ...

Combining MySQL tables for main menu and subcategories

Working on a new project and seeking help with a certain situation. I have a SQL database featuring two tables: menu and submenu. The table structure is as follows: Menu: id | title | order Submenu: id | title | parentId Currently, I'm using two si ...

An error occurred in Node.js while parsing data: Headers cannot be set after they have already been sent to the client

I am currently using a CSV parser to read data from a CSV file. I then pass this data in object format to an EJS template file for printing. However, when I try to stringify the data, I encounter an error. The error message is as follows: Error [ERR_HTTP_ ...

Struggling to establish a connection with AngularJS

I recently got my hands on the AngularJS SPA template for Visual Studio and dove straight into my first application. However, I'm already encountering a multitude of issues! Here's a glimpse of my PersonView : <!DOCTYPE html> <html ng ...

How can I trigger HierarchicalDataSource.read() when the kendoDropDownList's change event is fired?

Currently, I am utilizing the treeview and HierarchicalDataSource features of KendoUI. Additionally, I have included a kendoDropDownList at the top of the page. Each time a user changes the value of the dropdown list, it triggers the 'change' eve ...

Ensure the central alignment of the focused item within the horizontal scroll

I have successfully implemented a horizontal scroll container using <HTML/> and CSS: body { background-color: #212121; } .scroll_container { height: 100px; width: 400px; display: flex; align-items: center; overflow-y: hidden; width: 1 ...

Retrieving chosen items from dynamically generated checkboxes using Angular 2+

I have a piece of HTML code where I am trying to capture the value of all selected checkboxes whenever any checkbox is checked or unchecked, triggering the (change) event. <div class="checkbox checkbox-primary" *ngFor="let category of categories; let i ...

Displaying a pair of values using a single noUISlider

I'm trying to achieve something unique with a noUIslider range by outputting two values. While I've managed to display the same value twice using examples from the noUIslider documentation, my goal is to have one of the outputs show a value that ...

Issue with radio button not updating when selected

I am encountering a strange issue with a form I am currently developing. The code snippet below showcases the problem: <?php $show = ' checked '; $hide = ' '; /* Some logic to swap the checked here */ ?> <label cl ...

Component does not render on router.push('/page') without reloading first

After a successful login, I am storing the user token in browser cookies and using router.push('/dashboard') to redirect the user to their dashboard. However, the '/dashboard' page does not display any components until a manual reload i ...

The textbox is only enabled when a specific selection is made in the dropdown menu

My JavaScript code seems to be malfunctioning: <script language="javascript" type="text/javascript"> function ShowExpenseReport(Expense_Report_ID) { var width = screen.width; var leftpad = (width - 740) / 2; var jsopti ...

Having trouble setting the CSS width to 100%

Is there a way to assert the CSS width of an element in NightwatchJS to be 100% without hard-coding the value? Currently, when I attempt to do so, it fails and reports that the width is 196px. The specific error message is as follows: Testing if element ...

Is Socket.io combined with Redis the best architecture for an interactive gaming experience?

My current setup involves a NodeJS scaling architecture with Redis, specifically designed for a real-time multiplayer game. However, I'm facing challenges in configuring separate lobbies for players, as the load balancer assigns users to different ser ...

variable identifier looping through elements

I'm attempting to assign a dynamic ID to my div using ng-repeat. Here's an example: <div id="$index" ng-repeat="repeat in results.myJsonResults"> <div id="$index" ng-click="saveID($index)" ng-repeat="subRepeat in results.myJsonResul ...

Is it possible to include personalized validations in Formik's YupValidationSchema?

Is it possible to include custom validations in Formik's YupValidationSchema as shown below? YupValidationSchema = () => { return Yup.object({ Email: Yup.string() .max(256, "Length exceed 256 chars") ...

The Power of Jquery's load() Function for Loading Text

Is there a way to show the content of file.txt along with any text on the <title id="mytitle"></title> tag? $(window).on('load', function() { $("#mytitle").load("/file.txt"); }); The content of file ...

What causes my React app menu to unexpectedly open while simply updating the state without any CSS modifications?

In the Header component, there is a button that triggers the toggleNav function in context.js when clicked. This function changes the state of isNavOpen from false to true, resulting in the opening of the navigation. Surprisingly, there doesn't appear ...

Overlaying images on top of text

Is there a way to overlay an image on text, like a pattern image? Similar to applying color with a hex value in CSS: color: #ffffff; Any ideas on how this can be achieved? I want to have a pattern over text. https://i.sstatic.net/JaNZU.jpg Appreciate ...

Is there a foolproof method to verify if a user truly has visibility of a div element?

When I search online, most solutions only consider the viewport and/or the first parent container that is scrollable when trying to determine if a div is visible. However, I am wondering if there is a foolproof method to check if a div is truly visible t ...

Using a CSS hack to create a border cutout

The Dilemma... I am attempting to position div elements over a border, while maintaining space on both sides of each div. Take a look at the scenario: Note the gaps surrounding the black divs. I am struggling to find a solution for this issue, aside fro ...