/

HTML / CSS / JavaScript Tutorial

CSS Property: margin-right

[this page | pdf | back links]

The CSS (CSS1) margin-right property sets the width of the right margin of an element.

 

Valid property values (other than inherit and initial) are:

 

Value

Description

length

Any specified margin size as a CSS length

%

Margin specified as percentage of width of container

auto

Browser calculates margin

 

Default Value:

0

JavaScript syntax:

e.g. object.style.marginRight="30px"

Inherited:

No

Animatable:

Yes

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
p {border: 2px solid red; width: 400px}
span {border: 2px solid green;}
.x1 {margin-right: 50px;}
</style>
</head>
<body>
Element with default property<br>
<p><span>1</span><span>2</span><span>3</span></p>
Property set using in-file HTML style<br>
<p><span>1</span><span class="x1">2</span><span>3</span></p><br><br>
Property set using JavaScript<br>
<p><span>1</span><span id="x2">2</span><span>3</span></p>

<script>
document.getElementById("x2").style.marginRight = "50px";
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedCSSPropertyMarginRight() {
  var x = document.createElement("DIV"); x.style.marginRight = "50px"; return (window.getComputedStyle(x, null).marginRight == "50px");
}


NAVIGATION LINKS
Contents | Prev | Next | CSS Properties


Desktop view | Switch to Mobile