Update the LESS code by modifying the styles to specifically target color classes

I have defined several color variables:

@green: #00a75c;
@darkgreen: #118335;
@blue: #0099ff;
@orange: #f7931e;
@sapphire: #303a96;
@gray: #4d4d4d;

The content management system I am using allows for selecting a color as an option. As a result, the generated HTML will include one of these color names as a sibling element. Here is an example of the generated code:

<div class="thing orange">
</div>

Presently, I am styling the elements in my Less code in the following manner:

.thing{
  &.orange{
    color: @orange;
    border: @orange solid 1px;
  }
  &.blue{
    color: @blue;
    border: @blue solid 1px;
  }
  &.sapphire{
    color: @sapphire;
    border: @sapphire solid 1px;
  }
  &.green{
    color: @green;
    border: @green solid 1px;
  }

  &:hover{
    text-decoration: none;
    color: white;
    &.orange{background-color: @orange;}
    &.blue{background-color: @blue;}
    &.sapphire{background-color: @sapphire;}
    &.green{background-color: @green;}
  }
}

I am looking to refactor this code. Should I create individual mixins, a single large mixin, or is there a way to loop over the colors programmatically?

Answer №1

After reviewing the feedback provided by code-helper, a sample implementation has been created.

@themes:
    'light' #f0f0f0,
    'dark' #333333,
    'blue' #66b3ff,
    'green' #00cc66,
    'purple' #9933cc;

.generate-theme-classes(@index: length(@themes)) when (@index >0) {
    @theme: extract(@themes, @index);
    @property-name: e(extract(@theme, 1));
    @property-value: extract(@theme, 2);
    .generate-theme-class(@property-name; @property-value);
    .generate-theme-classes(@index - 1);
}

.generate-theme-class(@name; @value){
    &.@{name} {
        background-color: @value;
        color: white;
    }
}

.container {
    .generate-theme-classes();    
}

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

The loading speed is being impacted by the jQuery in the <head> section and <scripts> in my code

My website includes Slick JS on the index page and Colcade on another page. Before launching my site, I want to ensure that the loading times are optimized. Can someone review the order of appearance for these scripts and confirm if it's correct? Than ...

Containers do not line up properly with each other. Adjusting the width leads to unexpected consequences

As someone who is new to HTML and CSS, I am facing a challenge with aligning the items within a navigation bar on my website. The list serves as a navigation bar and is contained inside the "inner header" div. While I was able to align the entire "nav" con ...

Library for creating animated effects on HTML5 canvas elements in mobile phone applications using Javascript

Which JavaScript Animation Library is recommended for optimal performance on mobile devices using the HTML5 Canvas Tag for game development? I have found that many of the currently available libraries do not meet the necessary performance requirements ...

Is there an HTML5 input option specifically for timecodes?

Looking to implement an HTML5 input field for editing video and audio timecode, but struggling to find a suitable solution. The goal is to display and edit the following time components: Hours, minutes, seconds, and milliseconds. After researching, the ...

Is there a way to adjust the height pixel value in my code so it can be dynamic?

I have created a simple script that allows selected objects to fade in as the user scrolls down. However, my issue is that this script is quite rigid. If I were to apply it to 20 different objects, for example, I would need to manually adjust the height ea ...

Split-button dropdowns within tabs implemented with Bootstrap

I'm looking to create a navigation menu that combines both split buttons and dropdown tabs. I've found examples of each individually, but can't figure out how to merge the two: "Tabs with dropdowns" Is it possible to have a tab with a drop ...

Imitate a style using jQuery without deleting other elements

This is an example of HTML code: <div id="id1" style="width:100px;background: rgb(196, 14, 14);">1</div> <div id="id2" style="margin-top:10px">2</div> to <div id="id1" style=&qu ...

Using Javascript to open a new page and execute a script

I need to be able to launch a new window window.open(url,'_blank'); After that, I want to execute a JavaScript script like this: window.open(url,'_blank').ready("javascript code here"); However, I'm unsure how to accomplish thi ...

Exploring Options for Enabling HTML in AngularUI Accordion-Group Content

I want to showcase content in an accordion-group that might contain HTML markup. This content is fetched from external sources. How can I achieve this? You can check out an example at Plnkr (content hard-coded for testing) Currently, the items are displa ...

"Is there a way to dynamically add an input value as a class to the currently selected element using jQuery

Hey everyone, I'm currently working on some jQuery code to change the class of an icon by selecting a radio input with the corresponding class value. However, I'm encountering an issue where the class value is being added to all other icons as we ...

CSS:Tips for removing borders with border-radius in CSS

I have a div that has been styled with a left-hand border, and it's causing me some trouble. You can view the styling here on Jsfiddle. Is there a way to remove just the left hand side border without affecting the rest of the design or creating an un ...

Apply a chosen style to the element that was clicked on, while removing the style from the rest

Here is an example of my ul li structure: <div id="categoryTree"> <ul> <li id="cat_15"> <a class="hasSubCat" href="javascript:void(0);"><img src="images/icons/folder.gif" border="0" alt="Folder" title=" Folder ">N ...

Tips on using php within <<<DELIMETER:

Here is the code snippet I'm working with: $footer_rotem = <<<DELIMETER <div class="tile red"><img src="http://localhost/be/wp-content/plugins/Rotem%20Gallery/img/4.png"></div> <div class="tile red"><i ...

What is the best way to use shortcodes to display custom fields for post objects, specifically products, within WordPress posts

I'm in the process of displaying 'featured products' within my blog posts. These specific products need to be chosen using custom post objects on the backend for each individual post. I've outlined what I believe the PHP code should lo ...

Tips on playing HTML video only when it is in the user's view

Currently, I am working on a TikTok-inspired UI project. However, I am facing an issue where videos do not autoplay when they come into view after scrolling, similar to how it works on TikTok. Here is a demo for reference. Below is a snippet of my code: ...

Trouble with dropdown menu not sending selected option - Python/Django

I need assistance with creating a single dropdown menu that allows the user to select an item and add it to the cart. Currently, my code is printing out multiple dropdowns instead of just one. Any help or suggestions are appreciated. Thank you. {% for p ...

What is the best way to trigger content loading on touch or mouseover events?

Is there a way to dynamically load HTML content using JavaScript when a user hovers over or touches an element? For instance, I'm looking to load the entire <div id="mydiv"> content only when the user interacts with it. <div id="mydiv"> ...

Tips for identifying the clicked location inside an element using JavaScript?

Is there a way in JavaScript to find out the exact position of a click within an element, like its distance from the left, right, or center? I'm struggling to determine whether the clicked area is on the left, center, or right side. https://i.stack.i ...

Steps to deactivate a particular date on the Jquery UI datepicker:

I am struggling to disable specific dates (20, 21, 25 OCT 2015) using the JQuery UI datepicker. Despite my efforts, I have not been able to achieve the desired output. Below is a snippet of my code for reference. <!doctype html> <html lang="en" ...

Is requesting transclusion in an Angular directive necessary?

An issue has cropped up below and I'm struggling to figure out the reason behind it. Any suggestions? html, <button ng-click="loadForm()">Load Directive Form</button> <div data-my-form></div> angular, app.directive(&apos ...