Javascript DOM CSS Style marginTop Property set

Introduction

Change the top margin of a <div> element back to "normal":

document.getElementById("myDiv").style.marginTop = "0px";

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDiv {/*  w w  w .j av a  2 s  .c o  m*/
  border: 1px solid #FF0000;
  margin-top: 75px;
}
</style>
</head>
<body>

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

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

</body>
</html>



PreviousNext

Related