Propriété marginRight de style

Définition et utilisation

marginRight set or return the right margin of the element.

Property margin and Property padding All insert spaces around the element. However, the difference is that margin inserts space around the border, while padding inserts space inside the element's border.

See also :

CSS Tutorial :Marges CSS

CSS Reference Manual :margin-right attribute

HTML DOM Reference Manual :Property margin

Example

Example 1

Sets the right margin of the <div> element :

document.getElementById("myDiv").style.marginRight = "50px";

Essayez-le vous-même

Example 2

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

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

Essayez-le vous-même

Example 3

Returns the right margin of the <div> element :

alert(document.getElementById("myDiv").style.marginRight);

Essayez-le vous-même

Example 4

Difference between marginRight and paddingRight :

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

Essayez-le vous-même

Syntaxe

Returns the marginRight property :

object.style.marginRight

Sets the marginRight property :

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

Valeur de l'attribut

Valeur Description
% Defines the right margin using a percentage of the parent element's width.
length Defines the right margin using a length unit.
auto The browser sets the right margin.
initial Sets this property to its default value. See also initial.
inherit Inherits this property from its parent element. See also inherit.

Détails techniques

Valeur par défaut : 0
Valeur de retour : Chaîne, représentant la marge extérieure droite de l'élément.
Version CSS : CSS1

Navigateur pris en charge

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support