Working Selectable “Rating Stars” Without JavaScript FIRST

Working Selectable “Rating Stars” Without JavaScript FIRST

In Hans McMurdy’s article on using just css to make rating stars, he presents and excellent use case for the general sibling combinator. The problem is that it doesn’t actually do anything other than hovers.

In Hans McMurdy’s article on using just css to make rating stars, he presents and excellent use case for the general sibling combinator. The problem is that it doesn’t actually do anything other than hovers.

This is because:

  1. There’s no semantics, it’s just a bunch of DIV with pointless classes.
  2. Because it’s just a bunch of DIV any click to select them would be pointless.
  3. Since they’re just DIV, any hook to make them permanent would have to be scripting based.

The solution to this is easy, write working practical markup for it FIRST. Always try to make markup that works BEFORE you try and throw CSS or JavaScript at it.

It’s a subject I’ve meant to cover for a while now, how you do NOT have to sacrifice progressive enhancement / graceful degradation just because you want some fancy visual effect or to use scripting to avoid a page-load. Write it for both, it’s often easier!

The Markup

Our best answer would be to make a form filled with radio buttons. This provides scripting disabled functionality, keyboard navigation hooks, and a host of other things needed for accessibility and, well… just sane and rational web development.

<form action="#" method="get" class="ratingStars">
  <fieldset>
    <legend>Rate This:</legend>
    <input type="radio" name="rating" id="toggle_rating_5" value="5">
    <label for="toggle_rating_5">
      <span>5 Stars</span>
    </label><br>
    <input type="radio" name="rating" id="toggle_rating_4" value="4">
    <label for="toggle_rating_4">
      <span>4 Stars</span>
    </label><br>
    <input type="radio" name="rating" id="toggle_rating_3" value="3">
    <label for="toggle_rating_3">
      <span>3 Stars</span>
    </label><br>
    <input type="radio" name="rating" id="toggle_rating_2" value="2">
    <label for="toggle_rating_2">
      <span>2 Stars</span>
    </label><br>
    <input type="radio" name="rating" id="toggle_rating_1" value="1">
    <label for="toggle_rating_1">
      <span>1 Stars</span>
    </label><br>
    <input type="radio" name="rating" id="toggle_rating_0" value="0">
    <label for="toggle_rating_0">
      <span>0 Stars</span>
    </label>
  <!-- .ratingStars --></fieldset>
  <div class="submitsAndHiddens">
    <button>Submit</button>
  <!-- .submitsAndHiddens --></div>
</form>

Naturally, it has to be a form so users can submit the change. As a radio button set only one can be selected. All forms should have <fieldset>around any inputs the user can change, and this actually gives us an excellent parent element we can use to quickly querySelectorAll our input for scripting hooks when enhancing, and for selector driven style. The latter being so we don’t have to piss all over the markup with endless pointless classes for nothing.

I always put my submit/reset/hidden buttons in a DIV after any user interactable <fieldset>. Makes it easier to style and makes more sense semantically.

The <span> are in there so that we can use absolute positioning to fake them behaving like tooltips. They behave as normal text when CSS is disabled, blocked, or outright irrelevant.

They rank from high to low so that we can use the “any sibling combinator” to select later elements for style.

javascript css html web-design web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire CSS Developer

Want to develop a website or re-design using CSS Development? We build a website and we implemented CSS successfully if you are planning to **[Hire CSS Developer](https://hourlydeveloper.io/hire-dedicated-css-developer/ "Hire CSS Developer")**...

Pure CSS Responsive Image Slider For Web Design in HTML 5 CSS 3 - Web Design

Pure CSS Responsive Image Slider For Web Design in HTML 5 CSS 3 - Web Design

Latest Web Design and Development Trends To Follow In 2020

Web development is a dynamic field. With the emergence of new technologies each year, we can observe web development trends changing to a greater or lesser extent. In 2020 too, new technologies have emerged and

Top Neumorphism UI Design Using HTML, CSS & JavaScript

Top Neumorphism UI Design Using Html CSS & Javascript - Neumorphism Effect. Neumorphism Element Using HTML, CSS & JavaScript

Custom Website Design | Web Design | Local Website Development | Utah

We are Your Design Guys... But we do much more than just web design: Custom Website Design, Website Redesign, Website Development, Online Marketing, PPC, SEO, SEM, Hosting & Webmaster Services. Get a custom web design that's not just beautiful, it's written and designed to bring in more business.