Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

JavaScript - element padding size in Vanilla JS

0 contributions
8 points

Quick solution:

var element = document.querySelector('#my-element');
var style = element.currentStyle || window.getComputedStyle(element);

console.log(parseInt(style.paddingLeft));
console.log(parseInt(style.paddingTop));
console.log(parseInt(style.paddingRight));
console.log(parseInt(style.paddingBottom));

1. Current style property example

This approach allows to get current style value (even was assigned with selector).

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    body {
        margin: 0;
        height: 140px;
    }

    div {
        padding: 20px 30px 40px 10px; /* [top] [right] [bottom] [left] */
        background: gray;
        width: 100px; height: 100px;
    }

  </style>
</head>
<body>
  <div id="my-element">Text...</div>
  <script>
    
    var element = document.querySelector('#my-element');
    var style = element.currentStyle || window.getComputedStyle(element);

    console.log('padding-left:' + parseInt(style.paddingLeft));
    console.log('padding-top:' + parseInt(style.paddingTop));
    console.log('padding-right:' + parseInt(style.paddingRight));
    console.log('padding-bottom:' + parseInt(style.paddingBottom));

  </script>
</body>
</html>

2. Reusable code example

This approach organized above code in reusable function.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    body {
        margin: 0;
        height: 140px;
    }

    div {
        padding: 20px 30px 40px 10px; /* [top] [right] [bottom] [left] */
        background: gray;
        width: 100px; height: 100px;
    }

  </style>
  <script type="text/javascript">

    function getPadding(element) {
		var style = element.currentStyle || window.getComputedStyle(element);

        var result = {
            getLeft: function() {
                return parseInt(style.paddingLeft);
            },
            getTop: function() {
                return parseInt(style.paddingTop);
            },
            getRight: function() {
                return parseInt(style.paddingRight);
            },
            getBottom: function() {
                return parseInt(style.paddingBottom);
            }
        };
		
      return result;
    }

  </script>
</head>
<body>
  <div id="my-element">Text...</div>
  <script>
    
    var element = document.querySelector('#my-element');
    var padding = getPadding(element);

    console.log('padding-left:' + padding.getLeft());
    console.log('padding-top:' + padding.getTop());
    console.log('padding-right:' + padding.getRight());
    console.log('padding-bottom:' + padding.getBottom());

  </script>
</body>
</html>

 

0 contributions

Suggested wiki articles:

Suggested findings

Let's do the work and have some fun
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way. ❤ 💻 🙂
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    we will help you. 👍 ✔ 🦄
Read more