Creating a visually appealing 5-star rating system using CSS with a mix of full and half stars

I am looking to visually display the average rating of an entity using a 5-star marker that is customizable via JavaScript for setting or updating the current average rating. The marker should also allow for gradations such as full and half stars.

Previous solutions I have come across use intricate fonts, images, and involve complex HTML elements. I am seeking a more minimalistic code solution that is simple to integrate.

Answer №1

Presenting my unique solution. To enhance your HTML, you simply need to insert two nested divs styled using the provided stylesheet:

<div class="stars"><div class="percent"></div></div>

You can adjust the star rating in percentage (20% for each complete star; 10% for a half star) by setting the width attribute of the .percent element in CSS. The percent value can be easily modified later via JavaScript.

.stars {
    position: relative;
    float: left;
    font-size: 50pt;
    height: 1em;
    line-height: 1em;
}

.stars:before {
    content: "\2606\2606\2606\2606\2606";
    float: left;
    z-index: 1;
}

.stars .percent {
    position: absolute;
    left: 0;
    float: left;
    overflow: hidden;
    z-index: -1;
}

.stars .percent:after {
    content: "\2605\2605\2605\2605\2605";
    color: rgb(255, 200, 0);
}

li {
  display: table;
}
<ul>
<li><div class="stars"><div class="percent" style="width:  0%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 10%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 20%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 30%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 40%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 50%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 60%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 70%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 80%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 90%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 100%;"></div></div></li>
</ul>

The snippet above demonstrates ratings from 0% to 100% in increments of 10%. Please note that finer adjustments may not distribute evenly within the stars due to space considerations, but 10% intervals function well.

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 information about the property is not appearing on the screen

I included the following CSS: nav label:AFTER { content: " ▾"; } However, when viewed in Chrome, it appears like this: content: " â–¾"; I have already added <meta charset="utf-8"/> to my JSP page and @CHARSET "utf-8"; in my CSS file. ...

Eliminating divs and preserving content

Is there a way to remove certain DIV elements using jQuery or pure JavaScript without affecting the content within them? Here is an example structure: <div class="whatever_name"> <div class="whatever_name"> <h2>subtitle</h2&g ...

creating a Vuejs button function that will add together two given numbers

Help needed with VueJs code to display the sum of two numbers. Seeking assistance in developing a feature that calculates the sum only when the user clicks a button. Any guidance would be greatly appreciated! <!DOCTYPE html> <html lang="en"> ...

Manually controlling line breaks in HTML text

When collaborating with designers, they often have strong opinions about word wrapping in the final HTML page. If I am working on a fixed layout (non-responsive) and the designer is not satisfied with how the text wraps, there are several options available ...

Utilizing Objects as Properties in Phaser.Scene in Phaser 3

I've just started working with Phaser using TypeScript and I'm facing an issue. I attempted to move my main objects out of the create and preload methods by loading them as Phaser.Scene class properties. However, after making this change, my game ...

Strategies for integrating user data into Vue components within Laravel

I've successfully logged my user data in the console, but when I try to display the data on Contalist page, nothing is returned. I'm new to using Vue and need help implementing it into my projects. Below are my PHP controller and Vue component fi ...

"Encountering a 400 Error While Attempting to Edit Requests in NodeJS/A

Currently, I am building an application using Ionic/Angular with a NodeJS backend. Within this project, I have created an update form that allows users to modify or delete a specific row. While the deletion function is working fine, I am facing some challe ...

Manipulating arrays and troubleshooting Typescript errors in Vue JS

I am attempting to compare the elements in one list (list A) with another list (list B), and if there is a match, I want to change a property/field of the corresponding items in list B to a boolean value. Below is the code snippet: export default defineCo ...

Managing Cache Expirations

When I checked my website's performance using Google Webmaster Tools' PageSpeed analyzer, it highlighted that none of my resources were being cached. Here is the code snippet from my .htaccess file sourced directly from H5BP. I'm wondering i ...

JavaScript encountered an issue when trying to display HTML content

Through my PHP code, I am attempting to create a popup window that displays the content of an HTML file. However, after adding script tags, no HTML content is displayed. When I tried echoing out $row2, only the word 'array' appeared on the screen ...

What could be the reason for the navigation bar menu items not showing up in the sample application of the twitter.bootstrap.mvc4 package?

After setting up a new MVC4 web application project in VS2012, I decided to add the http://www.nuget.org/packages/twitter.bootstrap.mvc4.sample/ nuget package. However, upon running the sample application, I encountered an issue where the navigation menu i ...

How to maintain a child component's state without resetting it when the parent component's state changes and also retrieve the values of all child components in ReactJS with Typescript

Recently, I've been diving into React and encountered a roadblock while working on a custom dropdown filter for a table in my React application. Each column in the table has a set of dropdown values and an Apply button. To implement this, I created a ...

Generating interactive buttons that delete a row in a data table

I am facing an issue with creating delete buttons for each row in my database. Despite the data being displayed correctly and the table functioning properly, when I click on the button to delete a row, it disappears temporarily but reappears upon reloadi ...

The anchor tag's href attribute isn't working properly when using a single slash, but it works

I'm encountering an issue where my absolute URL is not clickable in an anchor tag as shown below: <a href="/account/login"><Login</a> However, it works with double slashes: <a href="//account/login"><Login</a> B ...

Tips for creating a loop that maintains the integrity of the data

I am facing a challenge with an exercise block containing a word of 4 syllables. This is how it appears in my JSON data: { "main_object": { "id": "new", "getExerciseTitle": "TestFrontEnd", "language": "nl_NL", "application": "lettergrepen", "main_object" ...

The FormData() object in Django backend is consistently found to be void of any data

I am currently experimenting with uploading an HTML form through AJAX using pure JavaScript, without jQuery. The form is put together in my template by combining three components: the CSRF token, a ModelForm, and a regular Django form (forms.Form). The vis ...

Deleting the initial pseudo element: A step-by-step guide

Among my collection of three interconnected divs, there is a continuous gray line. The following shows the HTML code: <div class="col-xs-3"> <div class="middle-blue-bg"> <!--Content--> </div> ... </div> ...

Withdrawal of answer from AJAX request

Is there a way to create a function that specifically removes the response from an AJAX call that is added to the inner HTML of an ID? function remove_chat_response(name){ var name = name; $.ajax({ type: 'post', url: 'removechat.php ...

Hidden beneath the content div lies the secretive footer div

I am experiencing an issue with a webpage that is working in IE7 but not in IE8; Here is the HTML code: <div class="content"> <div class="inner_content"> <div class="column"> <div class="widget"> ...

Looking for a JavaScript bookmarklet that can transform a CSS-styled table into an HTML <table>?

I am in need of a solution to convert a CSS-styled data display into a table-like format that can be used in a word processor. The issue is that the word processor does not recognize the structure created using CSS, resulting in a distorted layout. My goal ...