Change all four margins of a <div> element to "25px": - Javascript CSS Style Property

Javascript examples for CSS Style Property:margin

Description

Change all four margins of a <div> element to "25px":

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDiv {// ww w.j a v  a 2s  .  c o m
    border: 1px solid #FF0000;
    margin: 2cm 4cm 3cm 2cm;
}
</style>
</head>
<body>

<div id="myDiv">This is a div.</div>
<br>
<button type="button" onclick="myFunction()">Change margin</button>

<script>
function myFunction() {
    document.getElementById("myDiv").style.margin = "25px";
}
</script>

</body>
</html>

Related Tutorials