I have a basic CSS problem, that I don't know is really a basic or is it something else.
Here is the HTML.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class='test1'>Test1</div>
</body>
</html>
Here is the CSS.
.test1 {
width: 50px;
height: 50px;
border: 1px solid red;
margin: 5px;
}
As we can see I have an div
element having a class test1
on it with margin property as 5px
.
Now if I want to get the margin value on the div
element how will I get it?
And just a tip, writing below JS won't do anything, as there is no style attribute in the div
, margin is added to the div by css class test1.
const test1 = document.getElementsByClassName("test1")[0];
console.log(test1.style.margin);
Here is the JSBIN for the above scenerio.