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

202
Views
Why can't I assign all padding values?

I tried to change all paddings of the html element with js, like:

const pd = 1
document.getElementsByClassName('board')[0].style.paddingRight = pd + 'px'
document.getElementsByClassName('board')[0].style.paddingLeft = pd + 'px'
document.getElementsByClassName('board')[0].style.paddingTop = pd + 'px'
document.getElementsByClassName('board')[0].style.paddingBottom = pd + 'px'

but it sets the padding to 1px.

If one of the lines is removed eg.:

const pd = 1
document.getElementsByClassName('board')[0].style.paddingRight = pd + 'px'
document.getElementsByClassName('board')[0].style.paddingTop = pd + 'px'
document.getElementsByClassName('board')[0].style.paddingBottom = pd + 'px'

other values are set properly.

Can anyone explain why is this happening, and how can I fix it?

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

0

When you set the same value (in your example 1px) for the following CSS properties: padding-bottom,padding-left,padding-right,padding-top (all sides) - it's the same as padding: 1px;.

Moreover, if you set the same value (for example 2px) for padding-right and padding-left, and the same value for padding-top and padding-bottom (for example 1px) - you will see padding: 1px 2px;

Snippet:

var pd = 1;
var pd2 = 2;
document.getElementsByClassName('board')[0].style.paddingTop = pd + 'px'
document.getElementsByClassName('board')[0].style.paddingRight = pd2 + 'px'
document.getElementsByClassName('board')[0].style.paddingBottom = pd + 'px'
document.getElementsByClassName('board')[0].style.paddingLeft = pd2 + 'px'
  <div class="board">board</div>

Check this MDN's chart to see all combination.

about 4 years ago · Juan Pablo Isaza Report

0

That's because you are passing an Integer instead a String, in order to change the padding property you should use a string.

There are many ways to solve it but if all the paddings are the same then just modify padding property.

Change your const to string:

const pd = '1';
document.getElementsByClassName('board')[0].style.padding = pd;

'1' is taken as 1px by default, if you want to specify other metrics then write it, like '1em', '1%', etc.

Also, you may use

document.querySelector('.board')

to select the first element with board class instead select all and modify by index

about 4 years ago · Juan Pablo Isaza Report

0

Because I can't comment so i trying to answer

Can you show me your test results and what do you want

There's nothing wrong with your javascript script

If you want to change all the padding why not

 selector.style.padding = "1px"

Or

 Selector.style.padding = "1px 1px 1px 1px"

What is the reason you type that much? 🤔

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!