Modify the color of a particular character in a CSS text box when hovered over

I am facing a challenge in changing the color of two specific characters when hovering over them inside a textbox that is styled purely with CSS.

As the content is created using only CSS, I am unable to use style tags to accomplish this and feel stuck.

My goal is to have the "<" and ">" characters turn red when hovered over within the #CODESTUDENT:hover:after code.

#CODESTUDENT:after {
  content: "CODE STUDENT";
}

#CODESTUDENT:hover:after {
  content: "<CODE STUDENT>";
}
<li id="CODESTUDENT"></li>

I have attempted to add color properties but have not been successful in resolving this issue.

Answer №1

If you need to customize HTML text, consider adding the phrase "CODER AT WORK" within the tag and apply the following CSS styles:

#CODERATWORK:hover::before {
  content: "<";
  color: red;
}

#CODERATWORK {
  opacity: 0;
}

#CODERATWORK:hover {
  opacity: 1;
}

#CODERATWORK:hover:after {
  content: ">";
  color: red;
}

You have the flexibility to remove these styles for #CODERATWORK while still maintaining its presence on the webpage. Check out this example on CodePen for reference.

Answer №2

Are you familiar with using Javascript? If so, this method could be a suitable approach:

for (const div of document.querySelectorAll('div')) {
  div.textContent = div.id;
}
div::before,
div::after {
  color: red;
}
div:hover::before {
  content: "<";
}
div:hover::after {
  content: ">";
}
<div id="CODESTUDENT"></div>
<div id="FOO"></div>
<div id="BAR"></div>
<div id="BAZ"></div>

Answer №3

Check out this innovative approach to approximate the desired effect by utilizing a different pseudo element and adjusting margin/letter-spacing.

#CODESTUDENT:after {
  content: "CODE STUDENT";
}

#CODESTUDENT:hover:after {
  content: "CODE STUDENT";
  margin-left:-190px;
}
#CODESTUDENT:hover:before {
  content: "< >";
  letter-spacing:60px;
  color:red;
}
<li id="CODESTUDENT"></li>

Another suggestion involves incorporating background coloration in the after element for a similar effect. This method may also require knowledge of the width occupied by < and >.

#CODESTUDENT:after {
  content: "CODE STUDENT";
}

#CODESTUDENT:hover:after {
  content: "<CODE STUDENT>";
  background:
    linear-gradient(red,red) left /10px 100% no-repeat,
    linear-gradient(red,red) right/10px 100% no-repeat,
    #000;
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
}
<li id="CODESTUDENT"></li>

Answer №4

Check out this unique approach that utilizes pure CSS: https://jsfiddle.net/wb34yzLh/1/

The technique involves creating the word with the before pseudo element

 #CODESTUDENT::before {
   content: "CODE STUDENT";
   color: black;
 }

Subsequently, the word is reassembled on hover using both before and after to allow for individual letter coloring.

 #CODESTUDENT:hover::before {
   content: "<CODE STUDENT";
 }

 #CODESTUDENT:hover::after {
   content: ">";
 }

To finish off, we designate the single letter in after as red and change the color of the first letter to red upon hovering.

 #CODESTUDENT::after {
   color: red;
 }

#CODESTUDENT:hover::first-letter {
   color: red;
 }

Answer №5

I completely agree with the sentiments expressed in other responses regarding the proper use of pseudo elements. However, here's a quirky method to achieve a similar effect without modifying your HTML:

#CODESTUDENT:after {
      content: url("data:image/svg+xml,%3Csvg height='30' width='200' xmlns='http://www.w3.org/2000/svg'%3E%3Ctext x='0' y='15' fill='none'%3E&lt;%3C/text%3E%3Ctext x='15' y='15' fill='black'%3ECODE STUDENT%3C/text%3E%3Ctext x='140' y='15' fill='none'%3E&gt;%3C/text%3E%3C/svg%3E");
    }
    
  
#CODESTUDENT:hover:after {
    content: url("data:image/svg+xml,%3Csvg height='30' width='200' xmlns='http://www.w3.org/2000/svg'%3E%3Ctext x='0' y='15' fill='red'%3E&lt;%3C/text%3E%3Ctext x='15' y='15' fill='black'%3ECODE STUDENT%3C/text%3E%3Ctext x='140' y='15' fill='red'%3E&gt;%3C/text%3E%3C/svg%3E");
      
    }
<li id="CODESTUDENT"></li>

This technique involves swapping an SVG data URL on the hover state, which includes the following SVGs:

<svg height="30" width="200" xmlns="http://www.w3.org/2000/svg">
  <text x="0" y="15" fill="none">&lt;</text>
  <text x="15" y="15" fill="black">CODE STUDENT</text>
  <text x="140" y="15" fill="none">&gt;</text>
</svg>

<svg height="30" width="200" xmlns="http://www.w3.org/2000/svg">
  <text x="0" y="15" fill="red">&lt;</text>
  <text x="15" y="15" fill="black">CODE STUDENT</text>
  <text x="140" y="15" fill="red">&gt;</text>
</svg>

With some additional CSS tweaks, this approach might even appear somewhat presentable :).

Answer №6

Follow these steps to achieve your desired outcome

HTML:

<li id="CODESTUDENT">CODESTUDENT</li>

CSS:

#CODESTUDENT:hover:before {
  content:"<";
  color:red;
}

#CODESTUDENT:hover:after{
  content:">";
  color:red;
}

Thank you

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

Obtaining the header with jQuery leads to receiving no content

Forgive me for the odd request, but I am currently utilizing the jQuery Cycle plugin on a WordPress platform to incorporate a custom post type as a banner image with accompanying text. Rather than the typical pagination format of "1 2 3 4..." etc., I aim t ...

End the div element upon completion of the Vimeo video

I am in need of a website that includes an intro video displayed as a full-width div over the background content. To achieve this, I created a Div containing an iframe video from Vimeo along with a button to skip the intro (which closes the div upon clicki ...

What methods do web browsers use to detect when a user has viewed a custom image on a webpage?

As I work on creating a website to sell handmade crafts, it is essential that high-quality images are displayed for each product. In order to address SEO issues, I am considering loading the images asynchronously only when the user reaches the thumbnail ...

reasons why my custom attribute directive isn't functioning properly with data binding

Here is a snippet of the code I am working on, with some parts omitted for brevity: template.html ... <tr *ngFor="let item of getProducts(); let i = index" [pa-attr]="getProducts().length < 6 ? 'bg-success' : 'bg-warning'" ...

Creating a personalized notification box that is compatible with various screen sizes and web browsers on android devices with the help of

After successfully implementing a custom alert box to hide the header with JavaScript, I encountered an issue when trying to use it on different browsers and Android devices. The alert box appeared misplaced on the page and was too big for certain Android ...

Halt the countdown of a Jquery or Javascript timer

Searching for a straightforward solution to stopping a JavaScript/jQuery timer function has proven to be more challenging than I expected. The function in question initiates a timer, as shown below: function startTimer() { (function ($) { //timer for ...

Tips on showcasing Java map information obtained from the backend on an Angular/Typescript interface

I have a 'detailsMap : any' variable from the backend that contains multiple rows in the format (key1,key2). I need to display this data in the UI using TypeScript/Angular2. Please advise on how I can achieve this. key1 : { Name:'ABC' , ...

Strategies for resolving a mix of different data types within a single parameter

Here, I am setting up the options params to accept a value that can either be a single string or another object like options?: string[] | IServiceDetail[] | IServiceAccordion[]; However, when attempting to map these objects, an error is encountered: Prope ...

Django fails to display the initial image on the template

I'm a beginner in Django and I'm trying to display all images from different folders in an HTML file. In my views.py, I have the following code snippet: python template = loader.get_template('index.html') context = { 'images&apos ...

Retrieving a targeted data point from a JSON object

I am working with a json data that contains various properties, but I am only interested in extracting the uniqueIDs. Is there a way to retrieve ONLY the uniqueID values and have them returned to me as a comma separated list, for example: 11111, 22222? (I ...

Discovering user activity through rooms within SocketIO

My goal is to trigger an event when a user switches between online and offline status. The challenge arises from the fact that a user may have multiple tabs open, making it ineffective to track their connection status using on('connect') and on(& ...

Should I be concerned about including a non-existent CSS class?

When it comes to using jQuery and finding the values of text fields, adding classes seems like a common practice. However, I am concerned about how this may impact page loading. Is it efficient for the browser to search for all these classes? In the past, ...

How can I maintain the hover state even when the mouse is not hovering over it in CSS?

I have a lineup of 5 div boxes, and whenever you hover over one, it expands to fill the width of the screen. The default width of the blue div is set to cover the whole screen, allowing for the application of CSS to shrink it when hovering over the other ...

How come the transition does not take effect when removing and adding the class to the same element with the removeClass() and addClass() methods?

Two images are present, with the first one having the class "opacityOne". When a button is clicked, based on the variable index, I want the current image to fade in while the other fades out. It works well when I remove the "opacityOne" class from one ima ...

Step-by-step guide for importing a JSON file in React typescript using Template literal

I am facing an error while using a Template literal in React TypeScript to import a JSON file. export interface IData { BASE_PRICE: number; TIER: string; LIST_PRICE_MIN: number; LIST_PRICE_MAX: number; DISCOUNT_PART_NUM: Discout; } type Discoun ...

div with a fixed element inside that is scrollable

Check out this jsfiddle: http://jsfiddle.net/devboell/kjFps/ I am trying to create a div that overflows both horizontally and vertically. Additionally, I need an element that scrolls only horizontally while remaining fixed vertically. Here is an explanat ...

Using HapiJs in Node.js to communicate data back and forth with the client

I have client.js and server.js files that are used to send data to my server using ajax. I am able to successfully send the searched username, but on the server side, the domain is being received as undefined. I'm unsure if the issue lies on the clien ...

What is the best way to display photos horizontally instead of vertically on a webpage?

Is there a way to display these photos (found at ) in a row instead of a column? Can someone provide instructions on how to achieve this? Additionally, should I use one div for all the images or create a separate div for each image? Furthermore, it would ...

The issue of Next.JS fetch not caching data within the same request

I am faced with a straightforward setup where a Next.JS server-side component is responsible for fetching and displaying a post. The challenge lies in setting the page title to reflect the title of the post, requiring me to call my posts API endpoint twice ...

Could Angular be automatically applying margins or padding to my component?

I'm encountering a minor issue.. I'm attempting to create a "Matrix" to construct a snake game in angular, but there seems to be an unremovable margin/padding. Below is my code: <!-- snake.component.html --> <div id="ng-snake-main&q ...