Rating
Bootstrap 5 Star Rating component
Responsive star rating built with the latest Bootstrap 5. Rating provides insight into others opinions and experiences with a product. Use stars or other custom symbols (i.e. smiling faces}
Rating component can be used to allow the users to share their opinion about the product, documentation page, photo and more.
Note: Read the API tab to find all available options and advanced customization
Basic example
You can automatically initialize the rating component using
data-mdb-toggle="rating"
Read only
If you want to use a rating to show the score you can use the
readonly
option
Events
Rating emits events after click and hover element. Check the browser console to test it.
Custom text
You can add extra text before or after the icon using
data-mdb-after
and data-mdb-before
Custom icons
You can use other icons easily. Just change the fa
class to whatever you want.
Number of icons
If you want to display more or less icons in your rating, all you have to do is add as many
icons as you like inside the <ul>
tag.
Icons custom color
If you want to set your own icon color you can use the
data-mdb-color
tag
Dynamic icons
You can make your rating more dynamic by adding
data-mdb-dynamic="true"
Styling active elements
You can use active
class to set different styles for selected elements
Popover implementation example
Rating allows you to easily add popover functionality
Get selected value
To get the value selected by the user just listen for the onSelect.mdb.rating event that returns a value. Open the browser console to test how it's work
Rating - API
Usage
Via data attributes
Via JavaScript
Via jQuery
Note: By default, MDB does not include jQuery and you have to add it to the project on your own.
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the
option name to data-mdb-
, as in data-mdb-value="1"
.
Name | Type | Default | Description |
---|---|---|---|
value |
Number | '' |
Set default rating value |
readonly
|
Boolean | false |
Disable hover, click and keypress events |
after |
String | '' |
Set a custom text after the icon |
before |
String | '' |
et a custom text before the icon |
dynamic
|
Boolean | false |
Dynamically change previous icons to the currently selected/hovered icon |
tooltip
|
String | 'top' |
set tooltip position |
title |
String | '' |
Set the text displayed in the tooltip (for use only on the icons element) |
color |
String | '' |
Set icon color (for use only on the icons element) |
Methods
Name | Description | Example |
---|---|---|
dispose
|
Removes a mdb.Rating instance |
myRating.dispose()
|
getInstance
|
Static method which allows you to get the rating instance associated to a DOM element. |
Rating.getInstance(myRatingEl)
|
getOrCreateInstance
|
Static method which returns the rating instance associated to a DOM element or create a new one in case it wasn't initialized. |
Rating.getOrCreateInstance(myRatingEl)
|
Events
Name | Description |
---|---|
onSelect.mdb.rating
|
This event fires immediately when you clicked on icon. The clicked element is
available as the target property of the event, and selected value as the
value property.
|
onHover.mdb.rating
|
This event fires immediately when you hovered on icon. The hovered element is
available as the target property of the event, and hovered value as the
value property.
|
Import
MDB UI KIT also works with module bundlers. Use the following code to import this component:
CSS variables
As part of MDB’s evolving CSS variables approach, rating now use local CSS variables on
.rating
for enhanced real-time customization. Values for the CSS variables are
set via Sass, so Sass customization is still supported, too.