Programming

Speech Bubble in CSS

In today’s digital age, star ratings have evolved into universal symbols for conveying feedback and assessments. They are prevalent in various contexts, from rating movies and TV shows to appraising restaurants and hotels. Star ratings offer a straightforward and widely understood means of expressing the quality and appeal of products or services. Implementing star ratings on a website is an excellent way to empower users to provide feedback and engage with your content effectively. In this tutorial, we will explore how to create a star rating system using a combination of CSS and JavaScript to enhance user experience.

Designing Star Ratings with CSS:

The appearance and behavior of a star rating system can be defined in the CSS code. The following CSS styles present an elegant representation of star ratings:

.rating {
    float: right;
    margin-top: -35px;
}

.rating:not(:checked) > input {
    position: absolute;
    top: -9999px;
    clip: rect(0, 0, 0, 0);
}

.rating:not(:checked) > label {
    float: right;
    width: 1em;
    padding: 0 .1em;
    overflow: hidden;
    white-space: nowrap;
    cursor: pointer;
    font-size: 120%;
    line-height: 1.2;
    color: #ddd;
    text-shadow: 1px 1px #bbb, 2px 2px #666, .1em .1em .2em rgba(0, 0, 0, 0.5);
}

.rating:not(:checked) > label:before {
    content: '★ ';
}

.rating > input:checked ~ label {
    color: #f70;
    text-shadow: 1px 1px #c60, 2px 2px #940, .1em .1em .2em rgba(0, 0, 0, 0.5);
}

.rating:not(:checked) > label:hover,
.rating:not(:checked) > label:hover ~ label {
    color: gold;
    text-shadow: 1px 1px goldenrod, 2px 2px #B57340, .1em .1em .2em rgba(0, 0, 0, 0.5);
}

.rating > input:checked + label:hover,
.rating > input:checked + label:hover ~ label,
.rating > input:checked ~ label:hover,
.rating > input:checked ~ label:hover ~ label,
.rating > label:hover ~ input:checked ~ label {
    color: #ea0;
    text-shadow: 1px 1px goldenrod, 2px 2px #B57340, .1em .1em .2em rgba(0, 0, 0, 0.5);
}

.rating > label:active {
    position: relative;
    top: 2px;
    left: 2px;
}

Creating the HTML Structure:

To bring your star rating system to life, you’ll need to structure your HTML as shown in the following example. This code snippet showcases how to create a star rating using radio buttons and corresponding labels:

<div class="rating" id="rating">
    <input type="radio" id="star5" name="rating" value="5" /><label for="star5" title="Rocks!">5 stars</label>
    <input type="radio" id="star4" name="rating" value="4" /><label for="star4" title="Pretty good">4 stars</label>
    <input type="radio" id="star3" name="rating" value="3" /><label for="star3" title="Meh">3 stars</label>
    <input type="radio" id="star2" name="rating" value="2" /><label for="star2" title="Kinda bad">2 stars</label>
    <input type="radio" id="star1" name="rating" value="1" /><label for="star1" title="Sucks big time">1 star</label>
</div>

Retrieving the Rating Value with JavaScript:

In JavaScript, you can easily extract the value of the selected star rating using the following code:

var rating = $('input:radio[name=rating]:checked').val();

By following these steps, you can seamlessly implement a visually appealing and fully functional star rating system on your website. Users can effortlessly provide feedback and ratings, while your website gains an interactive and user-friendly interface. Star ratings not only enhance user engagement but also offer valuable insights into the popularity and quality of your content or products.

Image result for what is speech bubble

Category: CSS /

Ali Imran
Over the past 20+ years, I have been working as a software engineer, architect, and programmer, creating, designing, and programming various applications. My main focus has always been to achieve business goals and transform business ideas into digital reality. I have successfully solved numerous business problems and increased productivity for small businesses as well as enterprise corporations through the solutions that I created. My strong technical background and ability to work effectively in team environments make me a valuable asset to any organization.
https://ITsAli.com

Leave a Reply