search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
Use semantic HTML with hidden inputs and labels
Style the stars with CSS
Make it accessible
Optional: Add half-star ratings
Home Web Front-end H5 Tutorial How to create a star rating with HTML5

How to create a star rating with HTML5

Aug 14, 2025 pm 04:09 PM

It is feasible to create an HTML5 star rating system without JavaScript. By using semantic radio input boxes and label tags, combined with CSS style, the specific method is to hide the radio button, use label to display star symbols, and use the :hover and :checked status to match the brother selector to make the current and previous star turn golden when the mouse is hovered or selected. At the same time, the direction: rtl ensures that the score is displayed correctly from right to left. This solution has good accessibility, supports screen readers, keyboard navigation, and can be further optimized through title, aria-label or fieldset. If a half-star rating is required, more complex CSS or JavaScript extensions are required, and finally realizes a lightweight, customizable and beautiful star rating component.

How to create a star rating with HTML5

Creating a star rating system with HTML5 doesn't require complex JavaScript or external libraries—though you can enhance it later. With just HTML and a bit of CSS, you can build a clean, semantic, and interactive star rating that works well for user feedback, product reviews, or content ratings.

Here's how to build a basic star rating using HTML5 and CSS.


Use semantic HTML with hidden inputs and labels

HTML5 doesn't have a built-in star rating element, but you can use radio inputs and labels to create a clickable, accessible rating system.

 <div class="star-rating">
  <input type="radio" id="star5" name="rating" value="5">
  <label for="star5" title="5 stars">★</label>

  <input type="radio" id="star4" name="rating" value="4">
  <label for="star4" title="4 stars">★</label>

  <input type="radio" id="star3" name="rating" value="3">
  <label for="star3" title="3 stars">★</label>

  <input type="radio" id="star2" name="rating" value="2">
  <label for="star2" title="2 stars">★</label>

  <input type="radio" id="star1" name="rating" value="1">
  <label for="star1" title="1 star">★</label>
</div>

This structure uses radio buttons so only one star can be selected at a time. Each label displays a star symbol and is tied to its corresponding input via the for attribute.


Style the stars with CSS

Now make it looks like a real star rating. Hide the radio buttons and style the labels to show stars.

 .star-rating {
  direction: rtl;
  font-size: 32px;
  display: flex;
  gap: 2px;
}

.star-rating input {
  display: none;
}

.star-rating label {
  color: #ccc;
  cursor: pointer;
  user-select: none;
  transition: color 0.2s;
}

/* Hover and selection effect */
.star-rating label:hover,
.star-rating label:hover ~ label,
.star-rating input:checked ~ label {
  color: #ffcc00;
}

Key points:

  • direction: rtl reverses the order so that hovering over a star fills all stars to the left.
  • The ~ (general sibling selector) apply styles to stars that come after the hovered or selected one.
  • Stars turn gold ( #ffcc00 ) on hover or when selected.

You can use actual star icons (like from Font Awesome) instead of Unicode ★ if you want better visuals.


Make it accessible

This method is already fairly accessible:

  • Screen readers can interpret the radio group.
  • Each label has a title attribute describing the rating.
  • Keyboard navigation works naturally.

For even better accessibility, add aria-label or wrap the whole thing in a fieldset with a legend.


Optional: Add half-star ratings

For more precision, you can use two sets of radio inputs (odd and even) or use a pseudo-element trick with background gradients. But that requires more advanced CSS or JavaScript.


That's it. You now have a functional, accessible, and visually appealing star rating using only HTML5 and CSS. It's lightweight, easy to customize, and doesn't need JavaScript for basic functionality.

Basically just structure the inputs and labels right, hide the inputs, and use CSS to make the stars respond on hover and selection.

The above is the detailed content of How to create a star rating with HTML5. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to center an image vertically in HTML5? (Layout techniques) How to center an image vertically in HTML5? (Layout techniques) Mar 07, 2026 am 02:05 AM

Flexbox is the most stable for centered images. The key is to set display:flex and align-items:center in the parent container and specify the height; using place-items:center for Grid is more concise; absolute positioning requires top:50% with transform:translateY(-50%); vertical-align is invalid for block-level centering.

How to use SVG graphics directly in HTML5? (Inline SVG) How to use SVG graphics directly in HTML5? (Inline SVG) Mar 07, 2026 am 01:40 AM

SVG tags can be written directly into HTML without any external reference. The core of InlineSVG is to use it as an ordinary HTML element. The browser supports it natively. It does not require additional loading, does not trigger HTTP requests, and can be directly controlled by CSS and JS. A common mistake is to insert it as an image - this way you lose the advantage of inlining, the style cannot penetrate, and JS cannot get inside. Directly copy the SVG source code (exported from Figma, or handwritten), and paste it into an HTML file or any container. Make sure the beginning is, the end is, and there is no DOCTYPE in the middle. Delete useless attributes such as xmlns="http://www.

Related articles