Develop a LESS mixin for relative paths

Is there a way to create a partial path with a variable and pass only the file name into a mixin for background images in SVG format? Below is an example of what I'm trying to achieve:

@url: "url('../images/icons/_mm/";
.bg(@fileName){
background-image:@url @fileName;
background-size: contain;
background-repeat: no-repeat;
background-position: center center;
}

However, it seems like the concatenation is not working correctly. For instance, when using the following code:

.mmWrap{
.bg('swoosh.svg');
}

The generated CSS turns out messy as shown below:

.mmWrap {
  background-image: "url('../images/icons/_mm/" 'swoosh.svg';
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center center;
}

It becomes problematic when trying to pass arguments without quotes as it throws an error in the LESS compiler. I have tried using the escape ~ string without success. Any ideas or solutions would be greatly appreciated. Thank you!

Answer №1

If you want to achieve that, you'll need to utilize string interpolation.

@picture_directory: '../pictures/';

.bg(@filename) {
    background-image: url('@{picture_directory}@{filename}');
}

As far as I'm aware, this is the sole method of accomplishing this in LESS. Trust this information proves useful!

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

How can I activate page indicator and additional features in a TIZEN web app for wearables?

Switching 'data-enable-page-scroll' to true or false triggers different features and disrupts the section positions. Setting it to 'false' enables 'pageindicator' and the page displays perfectly, but 'moreoptions' fa ...

Utilizing Dojo to apply additional styles to an already existing class when a customized sorting feature is implemented on an improved

How can I dynamically add sort arrows (up/down) to a dojo enhanced Grid without using CSS? var mygrid = new EnhancedGrid({ id: "grid", store: gridStore, structure: gridStructure, canSort : function(index){ alert(index); } }, ...

Add CSS styling to a section of a canvas

I want to use a specific cursor png for just a portion of a canvas. Currently, I have code that applies the cursor to the entire canvas like so: .myClass { cursor: url('../img/myCursor.png') 7 33, auto; /* img hotspot centred*/ } However, I ...

The IntroJs step is only partially visible on the screen

Currently, I am incorporating introJS into my application and encountering an issue where one of the steps is only partially visible on the screen. Despite trying various position settings such as auto, left, right, etc., this particular item consistentl ...

What is the best way to incorporate Font Awesome icons into a UTF-16 encoded HTML document?

Trying to incorporate Font Awesome icons in UTF-16 encoded HTML pages can be a bit tricky. Following these steps should help you achieve the desired results: <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://use.fontawe ...

What's the best way to adjust the height of a textarea to completely fill the column?

Currently, I am in the process of creating a simple contact form using Bootstrap-4. The layout consists of 1 row with 2 columns: the left column contains input text fields and selection menus, while the right column includes a textarea. The challenge I&apo ...

I'm looking to create a Slider component using @material-ui/core that has a track divided into two sections with distinct colors for the left and right sides. How

When the slider ranges from -10 to 0, I want it to display in red. For values between 0 and +10, I would like it to be shown in green. Is there a way to implement this color change using the <Slider /> component from @material-ui/core? ...

Designing a dynamic 1-3 column arrangement with Angular for animated content

Let's discuss the current setup: I am in the process of creating a webpage for an application that includes a navigation bar, a footer, and a 3-column body layout. Initially, only one column is visible. This primary column will contain interactive d ...

Change the color of specific elements in an SVG using React

Can I change the dynamic primary color from ReactJS to a specific class in an SVG file? If yes, how can it be done? Error.svg <!-- Generator: Adobe Illustrator 26.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <svg version="1.1&qu ...

Accordion menu with smooth CSS3 transitions

I created a unique accordion menu to replace the select form control, and I am interested in using CSS3 transitions to give it a smooth expand and contract effect. Feel free to check out my work on jsfiddle: http://jsfiddle.net/hKsCD/4/ In order to achi ...

Achieving Perfect Alignment in Dreamweaver: Crafting a Stylish Header Container

Hello, I am currently working on a Dreamweaver site and have created a CSS style sheet within my template that includes a div tag called #HeaderBox. My goal is to make this box 40% of the screen's size, with specific pixel dimensions if necessary. I a ...

Tips on organizing media queries for multiple components styled in React

Struggling to avoid repeating media queries for all styled components? Have a grid layout that needs rearranging when the display screen changes? It can be tedious reusing the same media query for each styled component. import React from "react"; ...

Having trouble understanding why <p> and <h3> elements are displaying as links?

Starting my journey in front-end development with my first website project. Using HTML, CSS, Bootstrap, and Flask. Encountering an issue where my code is styled like a link instead of plain text. It appears blue and becomes underlined when hovered over. & ...

Mobile devices are experiencing an issue with extra white space appearing on the right side of websites

Having trouble with my website's mobile view... The issue is that on every mobile device, the site looks exactly like it does on desktop (which is good), but I can't seem to debug and find the CSS error causing a white space on the right side of ...

Store the input or response from users in local storage using an HTML table

After trying various methods, I have managed to implement a feature where users can input data into an HTML Table and save it using local storage. The goal is to allow users to easily retrieve their saved inputs when they revisit the page. In addition, I ...

What is the method for making the input field border turn red in Vuejs when data is not entered?

.input-section { border-radius: 4px; width: 359px; height: 42px; line-height: 42px; padding: 0 45px; border-radius: 4px; border: solid 1px #b1b8c9; background-color: #ffffff; } <input type="text" name="fullname" id="fullname" v-model="f ...

The image in the row wrap container spills over slightly

Even though all the items within that container are wrapped properly as article, there is an issue with the right border of the image overflowing the container's article border. My temporary solution involves adding a mediaquery @1041px and a small a ...

What is the best way to customize the default button style for buttons in Angular Datables?

After integrating buttons into my Angular Datatables, I noticed that they have default styling, which makes them stand out from the rest of my web page (refer to the Column Visibility button in the screenshot below): https://i.sstatic.net/w7Y8g.png I att ...

Incorporating Angular module into the mean.io bundle

Need help with adding the angular-ui-grid module to a mean.io package: $ cd packages/custom/mypackage $ npm install angular-ui-grid --save To import the JS, add this line to packages/custom/mypackage/public/index.js: import 'angular-ui-grid'; ...

Enlarge an image when hovering over it

I am currently in the process of creating a website and I have encountered an issue. When I hover over an image, I want it to zoom in, but unfortunately it does not zoom within the designated div area. How can I fix this problem? <div class="container" ...