admin管理员组文章数量:1186252
Is there a way to detect the true border, padding and margin of elements from Javascript code? If you look at the following code:
<html>
<head>
<style>
<!--
.some_class {
padding-left: 2px;
border: 2px solid green;
}
-->
</style>
<script>
<!--
function showDetails()
{
var elem = document.getElementById("my_div");
alert("elem.className=" + elem.className);
alert("elem.style.padding=" + elem.style.padding);
alert("elem.style.paddingLeft=" + elem.style.paddingLeft);
alert("elem.style.margin=" + elem.style.margin);
alert("elem.style.marginLeft=" + elem.style.marginLeft);
alert("elem.style.border=" + elem.style.border);
alert("elem.style.borderLeft=" + elem.style.borderLeft);
}
-->
</script>
</head>
<body>
<div id="my_div" class="some_class" style="width: 300px; height: 300px; margin-left: 4px;">
some text here
</div>
<button onclick="showDetails();">show details</button>
</body>
</html>
you can see, if you click the button, that the padding is not reported right. Only the properties defined directly through "style" are reported back, those defined through a CSS class are not reported.
Is there a way to get back the final values of these properties? I mean the values obtained after all CSS settings are computed and applied by the browser.
Is there a way to detect the true border, padding and margin of elements from Javascript code? If you look at the following code:
<html>
<head>
<style>
<!--
.some_class {
padding-left: 2px;
border: 2px solid green;
}
-->
</style>
<script>
<!--
function showDetails()
{
var elem = document.getElementById("my_div");
alert("elem.className=" + elem.className);
alert("elem.style.padding=" + elem.style.padding);
alert("elem.style.paddingLeft=" + elem.style.paddingLeft);
alert("elem.style.margin=" + elem.style.margin);
alert("elem.style.marginLeft=" + elem.style.marginLeft);
alert("elem.style.border=" + elem.style.border);
alert("elem.style.borderLeft=" + elem.style.borderLeft);
}
-->
</script>
</head>
<body>
<div id="my_div" class="some_class" style="width: 300px; height: 300px; margin-left: 4px;">
some text here
</div>
<button onclick="showDetails();">show details</button>
</body>
</html>
you can see, if you click the button, that the padding is not reported right. Only the properties defined directly through "style" are reported back, those defined through a CSS class are not reported.
Is there a way to get back the final values of these properties? I mean the values obtained after all CSS settings are computed and applied by the browser.
Share Improve this question edited Jun 12, 2015 at 2:45 L84 46.3k59 gold badges181 silver badges259 bronze badges asked Dec 8, 2008 at 11:34 GabrielGabriel 2,3639 gold badges29 silver badges41 bronze badges3 Answers
Reset to default 12the style
property can get the styles that are assigned inline like
<div id="target" style="color:#ddd;margin:10px">test</div>
if you want to get the styles assigned in outer css file or <style/>
element, try this:
var div = document.getElementById("target");
var style = div.currentStyle || window.getComputedStyle(div);
display("Current marginTop: " + style.marginTop);
if you are using jQuery, @vsync 's solution is fine.
With jQuery:
var $elm = $('.box');
var hPadding = $elm.outerWidth() - $elm.width();
var vPadding = $elm.outerHeight() - $elm.height();
var hBorder = $elm.outerWidth() - $elm.innerWidth();
var vBorder = $elm.outerHeight() - $elm.innerHeight();
console.log("Horizontal padding & border: ", hPadding);
console.log("Vertical padding & border: ", vPadding);
console.log("Horizontal border: ", hBorder);
console.log("Vertical border: ", vBorder);
.box{
width: 50%;
padding:10vw;
border:10px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='box'></div>
Your way (but you have to state paddingLeft, paddingRight..not just 'padding' ):
var elm = $('.box');
console.log("padding left (PX): ", elm.css("paddingLeft"));
.box {
padding:0 50px 0 5vw;
border: 2px solid green;
width: 300px;
height: 300px;
margin-left: 4px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">some text here</div>
It's possible, but of course, every browser has its own implementation. Luckily, PPK has done all the hard work for us:
http://www.quirksmode.org/dom/getstyles.html
本文标签: cssdetecting true borderpadding and margin from JavascriptStack Overflow
版权声明:本文标题:css - detecting true border, padding and margin from Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738344696a2077846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论