Latest web development tutorials
 

Style marginRight Property

< Style Object

Example

Set the right margin of a <div> element:

document.getElementById("myDiv").style.marginRight = "50px";
Try it Yourself »

Definition and Usage

The marginRight property sets or returns the right margin of an element.

Both the margin property and the padding property insert space around an element. However, the difference is that margin inserts the space around the border, while padding inserts the space within the border of an element.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The marginRight property is supported in all major browsers.


Syntax

Return the marginRight property:

object.style.marginRight

Set the marginRight property:

object.style.marginRight="%|length|auto|initial|inherit"

Property Values

Value Description
% Defines the right margin in % of the width of the parent element
length Defines the right margin in length units
auto The browser sets the right margin
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

Technical Details

Default Value: 0
Return Value: A String, representing the right margin of an element
CSS Version CSS1

More Examples

Example

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

document.getElementById("myDiv").style.marginRight = "0px";
Try it Yourself »

Example

Return the right margin of a <div> element:

alert(document.getElementById("myDiv").style.marginRight);
Try it Yourself »

Example

Difference between marginRight and paddingRight:

function changeMargin() {
    document.getElementById("myDiv").style.marginRight = "200px";
}

function changePadding() {
    document.getElementById("myDiv2").style.paddingRight = "200px";
}
Try it Yourself »

Related Pages

CSS tutorial: CSS Margin

CSS reference: margin-right property

HTML DOM reference: margin property


< Style Object