Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4920

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T04:26:06+00:00 2024-11-27T04:26:06+00:00

Scalable “star rating” without JS (and no SVG or image for the star)

  • 61k

Here I am with my “Star rating” system:

✔️ No JavaScript.
✔️ No complex HTML code, only the needed <input> elements for the user interaction and one extra element.
✔️ No hacky CSS code.
✔️ Easily scalable. Simply add more <input> to get more stars. No need to change any CSS code.
✔️ Works with keyboard navigation.
✔️ No SVG, No images. The star shape is built using pure CSS.
✔️ You can easily adjust the size and coloration of the stars.
✔️ Support both ltr and rtl direction

See it in play:

As said, the HTML code is pretty simple. A div that contains our inputs and an extra <i> element. Nothing more!

All you have to do is to add as many inputs as stars you want.

For the CSS part, we have the trivial one as follow:

.stars {   --s:50px;   position:relative;   display:inline-flex; } .stars input {   width:var(--s);   height:var(--s);   margin:0;   opacity:0;   cursor:pointer; } 
Enter fullscreen mode Exit fullscreen mode

The variable --s will define the size of our inputs that we make invisible using opacity:0. Everything is within a flexbox container (an inline one to easily integrate the star rating like an image or a simple text).

The real trick relies on i

.stars i {   position:absolute;   inset:0 0 calc(var(--s)*0.1);   pointer-events:none;   /* the star */   --v1:transparent,#000 0.5deg 108deg,#0000 109deg;   --v2:transparent,#000 0.5deg  36deg,#0000  37deg;   -webkit-mask:     conic-gradient(from 54deg  at calc(var(--s)*0.68) calc(var(--s)*0.57),var(--v1)),     conic-gradient(from 90deg  at calc(var(--s)*0.02) calc(var(--s)*0.35),var(--v2)),     conic-gradient(from 126deg at calc(var(--s)*0.5)  calc(var(--s)*0.7) ,var(--v1)),     conic-gradient(from 162deg at calc(var(--s)*0.5)  0                  ,var(--v2));   -webkit-mask-size: var(--s) var(--s);   -webkit-mask-composite: xor,destination-over;   mask-composite: exclude,add;   /**/   background:     linear-gradient(rgba(255,0,0,var(--o,0.3)) 0 0),       linear-gradient(gold                       0 0)     #ccc;   background-size:      calc(var(--l,0)*var(--s)) 100%,      calc(var(--p,0)*var(--s)) 100%;   background-repeat:no-repeat; } 
Enter fullscreen mode Exit fullscreen mode

First, we make it an absolute element that will cover all the div and logically all the inputs. We use pointer-events:none; to be able to interact with the inputs but still have the mouse cursor on the <i>.

Second, we apply 3 background layers as follows:

  1. The bottom layer is a grey coloration (#ccc) to indicate the number of stars and the non-selected ones
  2. The middle layer is the gold coloration. Here we use a gradient having a variable size based on the selected stars (controlled with the variable --p)
  3. The top layer is similar to (2) and will respond to the :hover effect (controlled with the variable --l). I will be using a semi-transparent color so we can still see the selected stars.

What about all those strange gradients and mask??

This is my personal touch and the crazy part of the work. I have built the star shape using multiple gradients applied to the mask property so all the background layers are seen through that shape.

Finally, the interactive part done using the following code:

.stars:focus-within {   outline:1px solid; } input:active ~ i{--o:1} input:nth-of-type(N):checked ~ i {--p:N}  input:nth-of-type(N):hover ~ i {--l:N} 
Enter fullscreen mode Exit fullscreen mode

:focus-within will allow me to style the whole div when interacting with the inputs (good for accessibility)

When an input is active (clicked on) I change the semi-transparent color to an opaque one to highlight the click action.

On :checked I update the variable --p based on the input index. We can easily generate the code using SASS/LESS or by doing some copy/paste (it only takes a few seconds to write the code that can cover up to 20 inputs)

On :hover we do the same logic but with the variable --l.

What about the rtl support?

Either we update the background-position based on the direction attribute and we simply add:

[dir="rtl"] .stars i {     background-position: right; } 
Enter fullscreen mode Exit fullscreen mode

Or I update the code and instead of multiple backgrounds, I rely on pseudo-elements that I can easily place using margin-inline-end

The trick is to have both pseudo-elements above each other (thanks to grid-area:1/1) with the adequate color. Their width will be controlled with the same variables used to control the gradient. Finally by using margin-inline-end:auto; they will get placed either at the left or the right based on the direction.


That's it

A simple non-hacky code and we have a fully interactive “Star rating” that you can easily embed anywhere.


Bonus

If you don't need the interactive part, here is a one-div version that you can control using CSS variables:

beginnerscsshtmlwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.