js element padding size in vanilla js
HTML[Edit]
+
0
-
0
js element padding size in Vanilla JS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34<!doctype html> <html> <head> <style> body { margin: 0; height: 140px; } div { /* 2em=32px */ padding: 2em 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>
[Edit]
+
0
-
0
js element padding size in Vanilla JS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57<!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>