Using a variable in a CSS selector with less

As a newcomer to LESS, I am starting by using it to streamline my stylesheets and reduce duplication. In an attempt to incorporate variables into a somewhat complex selector in less 1.2.21, I have written the following code:

@current_house_id: 97;
ul#house-family-menu li#menu-item-@current_house_id {
    background: #8cb4e8 url(images/family-current.png) no-repeat 97% 50%;
}

To clarify, the original CSS would have looked like this:

ul#house-family-menu li#menu-item-97 {
    background: #8cb4e8 url(images/family-current.png) no-repeat 97% 50%;
}

Upon compiling with lessc, I encounter the following error:

Syntax Error: on line 268: expected one of [ ( . # * - :: : @media @font-face , { ; got @ after: ul#house-family-menu li#menu-item-

I have attempted various escape methods as suggested in different sources, but none seem to resolve the issue. Could it be that this feature is not supported in LESS? If not, perhaps I could consider an alternative approach, such as applying the styles individually.

Any assistance would be greatly appreciated.

Answer №1

Here is some code:

@num: 10;

(~'.count{num}') {
    color: green;
}

Generates the following output:

.count10 {
    color: green;
}

Try this out:

@current_count: 25;
(~'span#item-count-@{current_count}') {    }

This will result in:

span#item-count-25 {    }

Note:

Version 2.0.0-beta has made a change ():

(~".newclass_@{index}") { ... 

Instead of using selector interpolation, do this instead

.newclass_@{index} { .... 

Also:

You are unable to perform selector interpolation with pseudo elements (as of v1.5.0). See open issue: https://github.com/less/less.js/issues/1294

Solution:

.myStyle(@whichPseudo) {
  @pseudo-element: ~":@{whichPseudo}";
    &@{pseudo-element} {
       border: 2px solid blue;
    }
}

#elementOne {
    .myStyle(before);
}

#elementTwo {
   .myStyle(after);
}

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

Aligning text to the right within a div that is placed inside an image element

I have a simple structure that looks like this: <div class="inline"> <div class="photo-group thumb_wrapper"> <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQYZ35nsT0CRTlGBtNmPHikH74wXBldsPe8LQYfhXnzADYo-xSU" ...

How can you quickly check an element's visual properties following a CSS modification?

I am facing an issue where I want to modify the appearance of an element in an HTML page using CSS and then immediately check its visual properties. However, the changes specified in the CSS are not applied instantly, but rather after a delay. CSS: .node ...

When Css is incorporated within a text, it prevents the use of " " from functioning properly

I am currently developing a GUI application using Qt 4.8. I have a label that displays text in white (the stylesheet has already been set up for this) and I want to change the color of the word "UPDATE" to red. Here is my initial code: //all text in whit ...

Issues with navigation menus in Bootstrap/Wordpress

My mobile drop-down menu is giving me some strange issues. When you toggle the button, the menu briefly appears just below the button before moving to its correct position. Sometimes it doesn't work at all, and clicking has no effect. You can witnes ...

Troubleshooting Vue 3 Options API custom element with non-functional Bootstrap js files

Having trouble incorporating Bootstrap 5 into a Vue 3 Options Api custom element? Check out my setup below: import { defineCustomElement } from 'vue' import 'bootstrap/dist/js/bootstrap.bundle' import App from './App.ce.vue' ...

Exploring the techniques for displaying and concealing div elements with pure JavaScript

Here's an example of code I wrote to show and hide div elements using pure JavaScript. I noticed that it takes three clicks to initially hide the div elements. After that, it works smoothly. I was attempting to figure out how to display the elements ...

Let the numerical tally commence once we arrive at said juncture

Our script is designed to count numbers, but the issue arises when the page is refreshed and the number count starts over. We want the count to start only when a user reaches that specific number or point. Css:- .office{padding-right: 5px; padding-left: ...

Adjusting image size to accommodate responsive column cell using CSS

I've been working on optimizing my mobile website using a responsive column layout. The challenge I'm facing is getting the images within each column to stretch to 100% width while automatically adjusting their height. I experimented with settin ...

Add aesthetic flair to scrollable containers

I am currently working on displaying data in columns using ngFor. However, I've run into an issue where the styles I apply to the column div are only visible on the top part of the column. As I scroll down to the bottom, the styles disappear. I believ ...

Is there a way to relocate the play again button below the box?

How can I ensure that my "Play Again" button moves up to align perfectly under the black border box? The black border box is styled with a class named .foot .button { background: rgb(247, 150, 192); background: radial-gradient(circle, rgba(247, 1 ...

The Bootstrap 4.5 code is not functioning as anticipated

I recently started learning Bootstrap and have been referring to the official documentation. However, I am facing issues with my code even though my folder only contains an index.html file. Link to Visual Studio Code screenshot. <!doctype HTML> < ...

Having trouble with the page background image link not functioning properly because of the wrapper

I am facing an issue with my HTML layout. I have a full background image set within the body tag, followed by a wrapper element: <body> <a href="#" id="bkimage">Background Image</a> <wrapper> ................. Here are the styles ...

Positioning of the header that adapts and adjusts based

I created a header layout using bootstrap 4.5 https://i.sstatic.net/RhWrs.png <nav class="navbar navbar-expand-sm navbar-light"> <!-- Brand --> <a class="navbar-brand" href="#"> <img src="logo.pn ...

What is the best solution for setting up a div containing a table with images inside?

I'm looking to achieve the following: <div id="display"> <div id="slideshow1"> <table cellspacing=0><tr><td style="height:200px;padding:0;vertical-align:middle"> <img ... /> </td></tr> ...

What is the best way to incorporate annotations onto a website without impacting the overall design and layout?

I'm currently working on a personal project that is similar to markup.io where users can load any website and add annotations. I am struggling to implement an annotation feature that functions like the one in markup.io: Does not disrupt the styling o ...

Chrome failing to import images, but Firefox has no issues with image importing

I'm not very familiar with JavaScript or jQuery, but I was attempting to troubleshoot a functionality that was previously created. The issue is that it works fine on Firefox, but it fails on Chrome. I am importing an image and then cropping the necess ...

Tips on increasing video size upon visiting a website. HTML, JavaScript, and potentially jQuery can be used for video animation

Is there a way to create a video pop-up in the same window when visiting a website? I want the video to increase in height from 0 to a specific height and move slightly upwards. Are there JavaScript or jQuery functions that can achieve this effect? I wou ...

The CSS for selecting the input[type="submit"] element is not functioning as expected

I apologize but I am encountering an issue with my .css stylesheet that covers styles for my input type buttons as shown below. input[type="submit"] input[type="submit"]:hover input[type="submit"]:focus The first three selectors are functioning well, how ...

Get rid of the lower padding on a handsontable

I am currently using either Chrome Version 61.0.3163.100 (Official Build) (64-bit) or Safari Version 11.0 (12604.1.38.1.7) on Mac OS Sierra 10.12.6. I am looking to create a handsontable that may exceed the screen height: Click here for an example As I ...

Navigation bar transforms color once a specific scroll position is reached

My website features a sleek navbar fixed to the top of the window. As users scroll past a specific div, the background color of the navbar changes seamlessly. This functionality has been working flawlessly for me thus far. However, upon adding anchor link ...