Difference between paddingLeft and marginLeft - Javascript CSS Style Property

Javascript examples for CSS Style Property:marginLeft

Description

Difference between paddingLeft and marginLeft

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {//from   ww  w  .  ja  v  a2  s  .co  m
    border: 1px solid #FF0000;
}
</style>
</head>
<body>

<div id="myDiv">This is some text.</div>
<br>
<button type="button" onclick="changeMargin()">Change left margin of the div element</button>
<br><br>

<div id="myDiv2">This is some text.</div>
<br>
<button type="button" onclick="changePadding()">Change left padding of the div element</button>

<script>
function changeMargin() {
    document.getElementById("myDiv").style.marginLeft = "100px";
}

function changePadding() {
    document.getElementById("myDiv2").style.paddingLeft = "100px";
}
</script>

</body>
</html>

Related Tutorials