Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
CSS varibale in own style

I'am not profficient in html and css, so i dont even know how to search for a post here in StackOverflow I excuse my selfe beforehand if this is an duplicate.

i want to do something like this:

margin-horizontal
{
   margin-left: var;
   margin-right: var;
}

<div style="margin-horizontal: 50px">

Is there a way to accomplish this behavior?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

CSS variables are possibly what you are looking for. Your example would look as follows.

CSS:

:root {
 --space: 50px;
}

.margin-horizontal
{
   margin-left: var(--space);
   margin-right: var(--space);
}

HTML:

<div class="margin-horizontal">...</div>
about 4 years ago · Juan Pablo Isaza Report

0

To add to Chris05's answer: you can also programmatically set the CSS variables from JavaScript.

document.documentElement.style.setProperty('--space', '150px');
document.documentElement.style.setProperty('--space2', '20px');
:root {
  --space: 50px;
  --space2: 50px;
}

div {
  color: white;
  text-align: center;
  background-color: #454545;
  margin-top: 10px;
}

.margin-horizontal {
  margin-left: var(--space);
  margin-right: var(--space);
}

.margin-horizontal2 {
  margin-left: var(--space2);
  margin-right: var(--space2);
}
<div class="margin-horizontal">150px margins</div>
<div class="margin-horizontal2">20px margins</div>

about 4 years ago · Juan Pablo Isaza Report

0

Expanding on the answer by @chris05, if you want to have different values of margins for different divs, you can add a class that sets the CSS variable to a different value, for instance:

  .m-25 {
    --space: 25px;
  }

  .m-75 {
    --space: 75px;
  }

Then your default will be 50px, and if you want a different one you just add a modifier class:

<div class="margin-horizontal">Foo</div>
<div class="m-25 margin-horizontal">Bar</div>

and you'll see:

HTML rendering

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!