What methods can be utilized to create a versatile ratings widget using LESS instead of traditional CSS?

Recently, I stumbled upon a versatile one-image rating widget created with SASS by the team at AirBnB. I am interested in replicating something similar using LESS. While I could manually write out the CSS, I prefer to generate it dynamically with LESS. My main hurdle lies in dynamically appending the numbers from 1 to 10 to the class (for example, .filled_1 up to .filled_10). Is this achievable in LESS?

Below is the functional SASS code:

$starWidth: 44px;
$starOffset: 0 -43px;
$numStars: 5;
$steps: 2;
$total: $numStars * $steps;

@mixin filled($n: 0) {
  width: ($starWidth / $steps) * $n;
}

.stars {
  background: url(/images/sprite.png) repeat-x top left;
  height: 43px;

  &.empty {
    background-position: $starOffset;
    width: $numStars * $starWidth;
  }

  @for $i from 0 through ($total) {
    &.filled_#{$i} { @include filled($i) }
  }
}

This results in the following CSS output:

.stars {
  background: url(/images/sprite.png) repeat-x top left;
  height: 43px; }
  .stars.empty {
    background-position: 0 -43px;
    width: 220px; }
  .stars.filled_0 {
    width: 0px; }
  .stars.filled_1 {
    width: 22px; }
  ...
  .stars.filled_9 {
    width: 198px; }
  .stars.filled_10 {
    width: 220px; }

I am seeking guidance on how to achieve a similar loop and inclusion in LESS as opposed to CSS.

The resulting HTML structure will resemble the following (where 9 indicates 4.5 stars):

<div class="stars empty">
  <div class="stars filled_9">4.5</div>
</div>

Answer №1

Here's a straightforward way to link to a helpful resource:

Furthermore, take a look at this code snippet from the same source:

The following is LESS code:

@iterations: 30;

.loopingClass (@index) when (@index > 0) {

    (~".myclass_@{index}") {
        my-property: -@index px;
    }

    .loopingClass(@index - 1);
}

.loopingClass (0) {}

.loopingClass (@iterations);

Resulting CSS:

.myclass_30 {
  my-property: -30 px;
}
.myclass_29 {
  my-property: -29 px;
}

.......
.......
.......

.myclass_1 {
  my-property: .1 px;
}

Answer №2

A user on a programming forum shared their unique strategy for implementing loops in LESS, which I found to be more complex than anticipated. After some experimentation, I have come up with an elegant solution that is suitable for situations where the number of elements is fixed.

@itemWidth: 33px;
@itemOffset: 0 -32px;
@numItems: 4;

.itemCount(@itemSpan: 1) {
    width: (@itemWidth / 2) * @itemSpan;
}

.items {

    background: url('/images/sprites/items.png') repeat-x top left;
    height: 32px;
    display:block;

    &.empty {
        background-position: @itemOffset;
        width: (@itemWidth * @numItems);
    }

    &.filled_0     { .itemCount(0); }
    &.filled_1     { .itemCount(1); }
    &.filled_2     { .itemCount(2); }
    &.filled_3     { .itemCount(3); }
    &.filled_4     { .itemCount(4); }
    &.filled_5     { .itemCount(5); }
    &.filled_6     { .itemCount(6); }
    &.filled_7     { .itemCount(7); }
    &.filled_8     { .itemCount(8); }
    &.filled_9     { .itemCount(9); }
    &.filled_10    { .itemCount(10); }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sync Data Automatically from SQL Database

For the past two months, I've been researching how to achieve an effect similar to the auto-updating sales on the page. So far, I haven't had any luck. I do have a PHP file that counts the number of results in a database and displays them as a n ...

Is it possible to incorporate pre-written HTML code into ASP.NET?

Is there a method to transform an HTML template into ASP.NET? I am attempting to convert a website template, which includes Javascript and CSS files, from HTML to ASP.NET in order to establish a connection with a SQL Server database. I have minimal coding ...

Switching my Bootstrap navbar to a Right-to-Left layout

Currently working on creating a navbar that needs to go from Right to Left for Hebrew language compatibility. Experimenting with a responsive navbar layout where the linked buttons align on the Left and move towards the Right. Want to have the brand logo ...

Creating uniform table column widths within a flex container with dynamic sizing

I am working on designing a navigation system for my website using a table inside a flexbox. I want to ensure that the table columns have equal width and can scale dynamically. Additionally, I aim to wrap the text in the data cells without changing the cel ...

Changing images dynamically in tinymce using JavaScript

When using the tinymce editor, I attempt to modify my images. I currently have an image within it and I am trying to dynamically change the path of this image with: tinymce.activeEditor.selection.getNode().src = '/my/path/' Surprisingly, this m ...

Top tips for seamless image transitions

Currently working on a slideshow project and aiming to incorporate smooth subpixel image transitions that involve sliding and resizing, similar to the Ken Burns effect. After researching various techniques used by others, I am curious to learn which ones ...

Carousel is error-free, but the Left/Right Buttons are malfunctioning

My Bootstrap Carousel Buttons are not working correctly. Although the first image is displayed along with the buttons, the image does not change on its own or in response to button clicks. This issue pertains to a web application that I am developing usin ...

What methods can I use to accomplish the transformation of superimposed images?

Struggling with transforming a content box on my webpage. I have a div containing three overlapping images positioned using Grid, and I want to scale one of them to fill the entire div when scrolled over. While I managed to achieve this effect, I'm un ...

The image does not resize vertically to fit the browser window

How can I make a large background image stretch to the size of the browser window when a user first visits the site, similar to how it is done on ? The current code I am using stretches horizontally but won't stretch vertically past a certain min-heig ...

Stop Gulp Inline CSS from enclosing HTML content within html and body elements

I need to work on a piece of HTML that I plan to copy and paste directly into an existing document. Here is the code snippet: <div id="someDiv"> </div> <script src="js/someScript.js" inline></script> <style> div#someDiv { ...

Instructions on creating an "already clicked" style for an <a> href

Is there a solution to display my href as already clicked? I'm looking for a property or method that does not involve css or javascript. ...

What jQuery method can be used to target the parent table containing a nested table opposite of the :has() selector?

I am facing an issue with a nested table structure in my HTML code. Below is the representation of the table: <table class="table parent"> <tbody> <tr> <td>TEXTA</td> <td>TEXTB</td> </t ...

What is the method for aligning images next to each other and on top of one another

Being new to html and css, I am currently working with 4 images that need to be arranged a certain way. My issue is that the pictures are stacking on top of each other instead of displaying as desired. Check out this image for reference To solve this pro ...

Align the reposition button with the pagination in the datatables

I am looking to adjust the placement of my buttons. The buttons in question are for comparison, saving layout, and printing. I would like them to be inline with the pagination, as I am using datatables here. <table id="sparepart_id" cellspacing="0" ...

The @Html.Label feature fails to appear when the content includes a period (.) - Utilizing Bootstrap with MVC Razor

I am dynamically populating label text from a database source <div class="col-11 col-sm-11 col-md-11 col-lg-11 col-xl-11"> @Html.Label(@Model.Questions[i].DataText + ": ", new { @for = "dd" + @Model.Questions[i].Data ...

Elevate when the mouse hovers over the button

My current task involves increasing the bottom-padding of a span when the mouse hovers over a button. Here is the button code: <button class="btn btn-success btn-circle" id="myBtn" title="Go to top"> <span id="move" class="fa fa-chevron-up"&g ...

Animations in Angular fail to operate within mat-tabs after switching back when custom components are being utilized

I am facing a similar issue as Olafvv with my angular 16 project. The problem occurs in a mat-tab-group where the :enter animations do not work when navigating away from a tab and then returning to it. However, in my case, the issue lies within a custom su ...

Create a visually stunning CSS3 animation within a specified container, mimicking the style shown in the

I'm currently working on creating the animation depicted in the image below: https://i.stack.imgur.com/W69EQ.jpg My goal is to develop a video slider where, upon clicking a button, the video will seamlessly shift to the right within its parent conta ...

Is there a way to alter visibility once a radio button has been selected?

I have a shape resembling a cube with 4 faces, all of which are currently visible. However, I would like to hide the other 3 faces when one face is showing. For example: I attempted setting the opacity of the other cube sides to 0 when the first radio but ...

Is the JavaScript click event ineffective for <select> <option>s?

It seems like there's a problem with the code below that is causing it to not work as expected. I'm trying to figure out why the onclick events on the <option>s are not triggering, but everything else seems fine. function displayInput() ...