Create Star Rating using HTML and Bootstrap (Source Code) - Easy Guide

Faraz

By Faraz -

Learn how to create a star rating system using HTML and Bootstrap. Follow our step-by-step guide with source code examples for easy integration into your website.


create-star-rating-using-html-and-bootstrap-source-code-easy-guide.webp

Table of Contents

  1. Project Introduction
  2. HTML Code
  3. CSS Code
  4. Preview
  5. Conclusion

Introduction to Star Ratings

Star ratings are a simple yet effective way to allow users to rate content, products, or services. They provide a quick visual representation of user satisfaction, making them a popular feature on review sites, e-commerce platforms, and more. Implementing a star rating system can enhance user engagement and provide valuable feedback. In this guide, we will show you how to create a star rating system using HTML and Bootstrap, complete with source code for easy integration into your website.

Benefits of Using Star Ratings

Star ratings offer numerous advantages for both website owners and users. They allow for quick and easy feedback without the need for lengthy comments. This encourages more users to leave reviews, leading to a more comprehensive overview of user satisfaction. For website owners, star ratings can increase credibility and trustworthiness, as they provide potential customers with insights into the quality of products or services. Additionally, they can improve user experience by simplifying the feedback process and making it visually appealing.

Setting Up Your Environment

Installing Bootstrap

Before we start coding, we need to set up our environment by installing Bootstrap. Bootstrap is a powerful front-end framework that makes it easy to design responsive web pages. To include Bootstrap in your project, you can either download it from the official website or use a Content Delivery Network (CDN). For simplicity, we will use the CDN method. Add the following lines to the <head> section of your HTML file:

<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-star-rating/4.0.3/css/star-rating.min.css'>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>

Source Code

Step 1 (HTML Code):

Now that Bootstrap is set up, we can create the basic HTML structure for our star rating system. Open your HTML file and paste the below codes into your file. Make sure to save your HTML document with a .html extension, so that it can be properly viewed in a web browser.

Here’s a detailed explanation of each part of the code:

Document Type and Head Section

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Bootstrap Star Rating</title>
    <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-star-rating/4.0.3/css/star-rating.min.css'>
    <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>
  </head>
  1. <!DOCTYPE html>: Specifies that the document is an HTML5 document.
  2. <html lang="en">: Defines the language of the document as English.
  3. <head>: Contains meta-information and links to external resources.
  4. <meta charset="UTF-8" />: Sets the character encoding to UTF-8.
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />: Ensures compatibility with Internet Explorer.
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />: Makes the webpage responsive to different screen sizes.
  7. <title>Bootstrap Star Rating</title>: Sets the title of the webpage.
  8. <link rel='stylesheet' href='...'>: Links to external CSS files for the Bootstrap Star Rating plugin and Bootstrap framework.

Body Section

<body>
    <div class="container">
      <h2>Bootstrap Star Rating</h2>
      <input class="rating" value="1" data-size="xs">
      <input class="rating" value="2" data-size="sm">
      <input class="rating" value="3" data-size="md">
      <input class="rating" value="4" data-size="lg">
      <input class="rating" value="5" data-size="xl">
      <br />
    </div>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-star-rating/4.0.3/js/star-rating.min.js'></script>
  </body>
</html>
  1. <body>: Contains the content of the HTML document.
  2. <div class="container">: A Bootstrap class that provides a responsive fixed-width container.
  3. <h2>Bootstrap Star Rating</h2>: A heading for the section.
  4. <input class="rating" value="1" data-size="xs">: An input field with a class of "rating" and a value of 1. The data-size attribute specifies the size of the stars (xs for extra small).
  5. Additional input fields with data-size values of sm (small), md (medium), lg (large), and xl (extra large) for different sizes of star ratings.
  6. <br />: A line break to add some space after the star rating inputs.
  7. <script src='...'></script>: Links to external JavaScript files for jQuery and the Bootstrap Star Rating plugin.

Explanation

1. CSS and JS Includes: The document includes external CSS and JavaScript files necessary for the Bootstrap framework and the star rating functionality. The Bootstrap CSS provides the styling for the document, while the star rating CSS and JavaScript provide the functionality and styles for the star rating inputs.

2. Container and Ratings: Within a Bootstrap container, there are multiple input fields with the class rating, each having a different initial value and size. These inputs are transformed into star rating widgets by the Bootstrap Star Rating plugin.

3. jQuery: The Bootstrap Star Rating plugin relies on jQuery, which is included in the document.

4. Star Rating Plugin: The star rating functionality is activated by including the star rating plugin's JavaScript file.

Step 2 (CSS Code):

No custom CSS thanks to Bootstrap!

/*

 No custom CSS thanks to Bootstrap!

*/ 

Final Output:

create-star-rating-using-html-and-bootstrap-source-code-easy-guide.gif

See the Pen Untitled by Faraz (@codewithfaraz) on CodePen.

Conclusion:

Creating a star rating system using HTML and Bootstrap is a straightforward process that can greatly enhance user interaction on your website. By following this guide, you can implement a star rating feature that is both functional and visually appealing. With the provided source code, you can easily integrate this system into your web projects and customize it to meet your specific requirements. Star ratings are a valuable tool for collecting user feedback and improving the overall user experience on your site.

That’s a wrap!

I hope you enjoyed this post. Now, with these examples, you can create your own amazing page.

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post