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

  • Home
  • SEARCH
  • 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 7635533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:27:02+00:00 2026-05-31T07:27:02+00:00

I want to add a variable to a css class. Is it possile? I

  • 0

I want to add a variable to a css class. Is it possile? I mean I want to do something like the following:

#box[i]
{
   padding: [i]px;
}

(for example: div which is name “box10” is supposed to get padding of 10px.)

I understand this may not be the right syntax, but I hope you can help me achieve the concept setting my class’ properties up to a variable value.

  • 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-05-31T07:27:04+00:00Added an answer on May 31, 2026 at 7:27 am

    It’s currently not possible to use variables within CSS, however there are a number of other options available.

    • The simplest option would be to create a CSS style for each of your box IDs.
    • You could use JavaScript to add padding to the box, but it is not sensible to include presentation within logic. In jQuery, a loop to do this would look like (assuming your boxes are ):

      $('div[id^=box]').each(function() {
          $(this).css('padding',this.id.substr(3)+'px');
      });
      
    • You could use a pre-processor tool, such as LESS to set variables in your CSS; but you would still need to specify each selector.

    Setting padding based on different box ID values seems like an odd problem to have. It may be worth taking a look at whether your approach to building this page is correct. Don’t forget every ID on the page should be unique. If you wish to use the same ID on multiple elements, you should use CSS classes.

    If you are trying to create a box sized based on a number of results (such as poll results), then it would be easier to use the style attribute and set a width/padding on each element rather than create an ID for every possible outcome. For example:

    <div style="width:10px"></div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add a variable place holder in my textField and when something
I want to add a bool variable and property to the base Entity class
I want to add a variable ${date} in the following bash script: ffmpeg -i
I want to add a variable number of records in a table (days) And
I want to add a string to a variable name that represents an integer.
I have dbml with single table users i want add partial class for User
I want to add a variable (column) to a dataframe ( df ), containing
I want to add a variable list of parameters to a Struts2 URL tag.
Is this possible to add variable to callback scope? What I want to achieve
In my asp.net MVC application, I want to add a variable (a flag for

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.