Is it possible to use Sass properties as arguments for my function?

My attempt to use SASS properties as arguments for my function seems to be failing miserably.

Is there a way I can achieve this, or is there a better solution to what I'm trying to do?

I am looking to dynamically change the values of variables based on screen width media queries within my function.

Currently:

@function myFunc($arg1, $arg2) {
    @debug "arg1 = #{$arg1} / arg2 = #{$arg2}";

    @return #{$arg1} * #{$arg2};
}

.my-class {
    // Initial values for mobile
    --var1: 10px;
    --var2: 20px;

    @media (--from-tablet-size) {
        --var1: 20px;
        --var2: 40px;
    }

    width: myFunc(--var1, ---var2);
}

The debug output shows that the function receives the strings --var1 and --var2 instead of their actual values...

DEBUG: arg1 = --var1 / arg2 = --var2

Is there a solution to this issue? Either by using properties as arguments like I am trying here, or through a different approach?

Answer №1

There seems to be some issues in your code:

  1. To read CSS variables, you need to use var(), like this: var(--var1). For more information, check out: https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties
  2. You cannot use CSS variables with SASS calculations or functions.
  3. In SASS, you cannot multiply two numbers with units, for example, what do you expect from 10px * 20px? It should be something like 10 * 20px.

First recommendation: Use SASS variables

@function myFunc($arg1, $arg2) {
    @debug "arg1 = #{$arg1} / arg2 = #{$arg2}";

    @return $arg1 * $arg2;
}

.my-class {
    // Initial values for mobile
    $var1: 10;
    $var2: 20px;
    width: myFunc($var1, -$var2);

    @media (--from-tablet-size) {
        $var1: 20;
        $var2: 40px;
        width: myFunc($var1, -$var2);    
    }
}

Second recommendation: Use CSS variables

.my-class {
    // Initial values for mobile
    --var1: 10;
    --var2: 20px;

    @media (--from-tablet-size) {
        --var1: 20;
        --var2: 40px;
    }

    width: calc(var(--var1) * -1 * var(--var2));
}

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 gem 'remotipart' does not display any server errors in Rails 4

Currently, I have included the 'remotipart' gem in my Gemfile like this: gem 'remotipart' This is defined in my form view new.html.erb <%=form_for @user, remote: true, html: { multipart: true, class: 'user-form' } do |f| ...

Storing various data in separate tables using MySQL and Node.js

Currently, I am working with three tables - user, modules, and an intermediate table called user_module. To perform an inner join between the user and module tables, I need to access the user_module table. "SELECT user.uName, module.moduleName FROM user_m ...

The dojo array implemented a new element, pushing out the old one

The JavaScript code below is supposed to populate the array personNames with objects containing names from an array of persons. However, it incorrectly repeats the same name for each object instead of assigning different names: [{"name":"smith"},{"name":" ...

Difficulty arises when attempting to locate particular information within a Vue component using a method that is contained within the component

Currently, I am in the process of developing a request management system for the organization. The key requirements for this project include: Ability to add a new row for each new request. Dynamic generation of parameters based on the selected descriptio ...

Removing files from the S3 bucket and database simultaneously. Execute each function sequentially

Struggling with an API call that should delete an image from both an S3 bucket and a MySQL database. However, encountering an issue where the S3 image cannot be found because it is being deleted from the database first. // Router code: //req.body is an ar ...

the overflow issue with Summernote's dropdown menu

I am currently working on a 2 columns layout within 2 divs that have different margins set. In the left column, I have integrated a bootstrap datetimepicker and summernote. While the datetimepicker dialog overflows outside the internal div, I am wondering ...

Establishing a simple server with express

I am facing an issue with my node.js application where I keep getting an error message when trying to load the homepage. Below is the architecture of my application: index.js --> server.js --> router.js --> requestHandlers.js For this project, I ...

Utilizing the CSS REM unit to define element dimensions

I recently discovered the versatility of using the REM unit for sizing elements, not just font sizes. It works really well in conjunction with the HTML font-size property. html { font-size:1vw } @media all and (max-width:720px) { html { font-size:10px ...

What is the method to access an HTML page div using JavaScript?

I'm currently developing a login page using phonegap with android. Upon successful login validation, I need the user to be directed to the welcome section if they are authorized. Here is my javascript code from the file called myjavascrpt.js: var ...

Style your content using PHP and CSS to format text with multiple selectors

My HTML code looks like this: <div id="main"> <aside class="readableCenterAside left"> ## Content 1 ## </aside> <aside class="readableCenterAside right"> ## Content 2 ## </aside> </div> Here is my CSS: .readableCe ...

Sort function now accepts a limitless amount of arguments for sorting

We are tasked with creating a function that can take an unlimited number of arguments to sort an array by. For example: var data = [['John','London',35], ['Ricky','Kuala Lumpur',38], [' ...

Eliminate Bootstrap's validation glyphs

Issue Description I am attempting to eliminate the validation icons from Bootstrap, such as the "x" and "check" symbols. Despite my efforts to search thoroughly, I have been unable to locate where these icons are located in the code. Code Sample You can ...

Different ways to notify a React/Next.js page that Dark Mode has been switched?

I am in the process of creating my first basic Next.js and Tailwind app. The app includes a fixed header and a sidebar with a button to toggle between dark and light modes. To achieve this, I'm utilizing next-theme along with Tailwind, which has been ...

Executing a function that includes showModalDialog outside of an iframe might display the incorrect page

This website structure looks like: `--main.html `--dialog.html `--folder `--iframe.html The code for each page is as follows: main.html: <html> <head> <script type="text/javascript"> function t ...

Tips for breaking apart elements of a JavaScript array when a specific delimiter is present in an object property

I am facing a challenge with an array of objects where some properties contain commas. My goal is to split these properties into new objects and recursively copy the rest of the properties into new array elements. For example, consider this original array ...

Utilize Express to deliver dynamically generated or stored images

My latest project is an innovative image delivery application that can serve images and dynamically resize/change their extension as needed. For the routing, I utilized Express and Sharp for processing the images. One challenge I encountered is high CPU ...

Vue Error: "the main template must have one and only one root element"

Exploring My Component Setup components exposed: <template> <!--<post-form/>--> <div class="wrapper"> <b-table :data="posts"> <template slot-scope="props"> <b-table-column fie ...

Javascript error: The variable calculator_test has not been defined

Why am I receiving an error message: Uncaught ReferenceError: calculator_test is not defined index.html: <!DOCTYPE html> <html> <body> <p>Click the button</p> <button onclick="calculator_test()">test</button> &l ...

Utilizing JQuery to modify CSS in a partial view

My layout includes a button and a dynamically filled div below it using ajax and a partial view. Here is the code related to the button: $(document).on('click', '.btn-release', function () { StartReleaseVersionEdit(); ReleaseV ...

Encountered an error while trying to access an undefined property during an Angular Karma

I'm currently working on testing a service function that involves multiple $http.get() calls. The function being tested returns a promise but the test is failing with an error stating response is undefined. Below is the test script: it('should ...