What is the best way to style an icon in CSS?

I am facing an issue with the following code snippet:

<span class="stars">★★☆☆☆   </span>

My objective is to style the empty stars differently, but I am unsure how to specifically target them.

The following CSS rule does not produce the desired effect:

.glyphicon-glyphicon-star-empty{
    color: gray !important;
}

Similarly, this rule changes the color of all stars, not just the empty ones:

.entry-content span .stars{
    color:gray;
}

Is there a way to only change the color of the borders of the empty stars (the last 3)? I prefer a solution using CSS only, but I am open to using JQuery or JavaScript if necessary. Please note that there will be additional classes involved. The goal is to target all empty stars specifically.

Answer №1

To change the color of stars, you can use an absolute positioned pseudo-element (::before). Modify the number of stars by using the data-stars attribute. To color the desired number of stars, utilize jQuery's _.attr() or Element.setAttribute():

$('span').attr('data-stars', '☆☆☆');
.stars {
  position: relative;
}

.stars::before {
  position: absolute;
  right: 0;
  height: 100%;
  color: red;
  content: attr(data-stars);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="stars">★★☆☆☆</span>

Answer №2

It appears that additional classes will be necessary in this situation. Consider utilizing the following:

<span class="icons"><span class=“icon-blue”>★</span>☆</span>

Afterwards, apply the icon-blue class to your CSS file.

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

Achieve seamless integration of PHP function execution with Javascript onClick without the need for page

After delving into my research, it seems like Ajax and/or JQuery may be the way to go for what I'm trying to achieve. However, I wanted to double-check before moving forward in that direction. The task at hand involves using a javascript onclick func ...

Struggling to figure out the ::before border trim issue

As I work on designing a banner for a webpage, I have added a sliced bottom right border using the ::before pseudo class. The code snippet I used includes: &::before { content: ""; position: absolute; bottom: 0; right: 0; ...

The element type provided is not valid: it should be a string for built-in components or a class/function for composite components. However, an object was received instead. - React Native

After conducting extensive research, I have been unable to find a solution as to why this issue persists. Can anyone shed some light on what the error might be referring to? Error: Element type is invalid: expected a string (for built-in components) or a c ...

Looking for visible elements in WebDriverIO?

Currently, I am developing a test suite using WebDriverIO for a website with multiple duplicate elements that are selectively displayed based on user interaction... For example, the site may contain five buttons that each open a tooltip. These tooltips ar ...

PHP ajax handling multiple requests

I've searched high and low on the internet, but couldn't find a solution to my problem. I am making multiple jQuery AJAX calls to a PHP script. Initially, I noticed that each call was being executed only after the previous one had finished. To a ...

Comparing ASP.NET Core and Node.js: A Closer Look at Their Similar

After working with Node.js for some time, I have gained a deep understanding of how it operates internally, including the event loop and other aspects. However, I can't help but notice that ASP.NET Core bears a striking resemblance to Node.js. ASP.NE ...

Execute a function upon pressing the enter key

Currently, I am working on coding a webpage with a form that includes one field where users input a set of numbers. After typing in the numbers, they should then press a button labeled 'Run' to execute the code. However, the issue arises when use ...

Using a jQuery animation to trigger a dialog opening from one element and closing it back into that same element

Is it possible to display my jQuery dialog outside of its original element and then hide it back inside that element? What is the best way to achieve this functionality? Are there any specific effects that can be used for this purpose? ...

The transparency level of materials in THREE.js

When using the lambert shader, I encountered an issue with setting the material. In the example provided, it sets the material as follows: this.material.uniforms.emissive.value = new THREE.Color( Math.random(), Math.random(), Math.random()); Prior ...

Identifying duplicate values in an array of objects using JavaScript

I am facing an issue with my array that collects data from a spreadsheet into studentCCAList. Sometimes, a student may have multiple subjects. For example, Name: Joseph Subject: English Name: Peter Math Name: Joseph Subject: Science My concern i ...

The placement of the Vuetify tooltip is incorrectly aligned when located in the footer section

Having trouble fixing the issue with the Vuetify tooltip. After scrolling on the page, the tooltip moves up despite using fixed="true". Here is the code snippet causing the problem: <v-footer app inset fixed> <v-row align="center ...

Identifying the server control ID on the master page with the help of jQuery

My Asp.net Master Page includes an adrotator element. <asp:AdRotator ID="adr" AdvertisementFile="~/Adrotator.xml" Width="180px" Height="200px" runat="server" Target="_self" /> I have implemented a jquery script to rotate the ADS, b ...

Synchronizing timers among different elements

Just a little context: I'm diving into the world of React and currently working on a small app using next.js (with a template from a tutorial I followed some time ago). Lately, I've encountered a challenge where I need to synchronize a timer betw ...

Update the class information using jQuery after performing an action and serialize the button's ID for AJAX requests

I'm facing an issue with two buttons on my page: <td> <button id="1" class="green-button">Publish</button> <button id="1" class="yellow-button">Delete</button> </td> The green-button and red-button have different ...

Aligning two lines next to a radio button using HTML and CSS

I'm facing a challenge where I want to align two lines in the middle of a radio button. Despite my efforts, I am able to line up the top line but struggling with the bottom one. I prefer not floating the radio button as it's being styled using a ...

Exploring Attachments in ASP.NET Core MVC Views

I have placed attachments in a Shared Folder on a server. I attempted to access them using the <a> tag <a href="file:///C:">Open Attachments</a> Unfortunately, this method did not work in ASP.NET Core MVC for unknown reasons. I ...

Items in Bootstrap row are not aligning in the center

Utilizing the Bootstrap 5 row property for responsiveness on my website, I encountered an issue where two divs placed next to each other did not align at the center of the webpage as expected. There was an unusual gap on the right that I couldn't elim ...

Retrieving chosen row data in Angular 6 Material Table

I am attempting to send the value of a selected row from one component to another upon clicking a button. However, in this particular example, I'm unsure where to obtain the selected row values and how to pass them on button click. After that, routing ...

Sending information from a controller to a view in Ruby: a how-to guide

Currently, I am utilizing Ruby and facing the need to provide users with a constant status update message while a lengthy task is executed in the controller. During this method, various rows are being inserted into the database. My goal is to display a me ...

Tips for Avoiding Line Breaks in CSS Divs

I am currently designing a website menu with items such as Home, Contact us, and About us. Each item is styled with a background color and text size of 125X30. I initially used the float property in CSS to align them correctly in a single line from left ...