Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8028465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:08:37+00:00 2026-06-05T00:08:37+00:00

I have a LESS file that has some variables: @font-size: 100%; @label-align: left; @field-width:

  • 0

I have a LESS file that has some variables:

@font-size: 100%;
@label-align: left;
@field-width: 230px;
@icon-size: 16px;
@icon-padding: 8px;

In my JS script I need to know the sum of some initial values the user has set in the LESS file. The computed css values might change according to parent container size. Those values also change on first load, depending on the window size.

Some elements are also dynamically created so it makes it really hard to grab the correct initial values in JS because I would have to declare variables at different points in the code and in different scopes.

One idea I had is to declare a “high scope” object with some dummy variables and assign the value to the variable as soon as I append to element to the DOM but it turned out to be messy and redundant.

After many hours I came up with this idea that currently works, kinda hacky but works.

LESS:

.less-vars {
    width: @field-width + @error-width + @icon-size + (@icon-padding * 2);
}

JS:

var less_vars = $('<div class="less-vars" />').hide().appendTo('body').width();
$('.less-vars').remove();

Is there any other way I can do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:08:38+00:00Added an answer on June 5, 2026 at 12:08 am

    I just wrote a small piece of Javascript that reads this directly from document.styleSheets so it doesn’t require any extra HTML. I’ve only tested it in Chrome.

    In my less I have:

    #less {
        .thumbsWidth { width: @thumbsWidth; }
        .thumbsTop { width: @thumbsTop; }
    }
    

    My Javascript reads:

    var oLess = {};
    $.each(document.styleSheets,function(i,sheet){
        $.each(sheet.cssRules,function(i,rule){
            var sRule = rule.cssText;
            if (sRule.substr(0,5)=="#less") {
                var aKey = sRule.match(/\.(\w+)/);
                var aVal = sRule.match(/(\d+)/);
                if (aKey&&aVal) oLess[aKey[1]] = aVal[0]<<0;
            }
        });
    });
    console.log(oLess);
    

    Which makes oLess:

    {
        thumbsWidth: 123,
        thumbsTop: 456
    }
    

    Currently this only reads the pixel values but you can easily modify it to read out anything.

    -update-

    Here’s a fiddle: http://jsfiddle.net/Sjeiti/VHQ8x/ (with a gist in the resources https://gist.github.com/2948738)

    -a very late update-

    The above was nice at the time. Now you are better off using CSS variables and element.style.getPropertyValue("--my-var");. See here: https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties#Values_in_JavaScript

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large text file (~10mb) that has more or less every dictionary
I have a .less stylesheet that has a mixin for custom gradients. When I
I have the following less.css ( http://lesscss.org/ ): #content { overflow: hidden; width: 100%;
I have a client who got a zipped file that has all the database
I have some code that looks more or less like this: while(scanner.hasNext()) { if(scanner.findInLine(Test)
I have a verry simple style.less file in my project, and i'm using bootstrap
I have been looking at LESS ( http://lesscss.org/ ) which adds variables and macro
I have some more or less linear data of the form: x = [0.1,
I have a txt file that I’m trying to import as flat file into
I have a web application that will display logos. Some of the logos will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.