Propriété marginTop du style
Exemple
Définissez la marge supérieure d'un élément <div> :
document.getElementById("myDiv").style.marginTop = "50px";
Définition et utilisation
La propriété marginTop définit ou renvoie la marge supérieure d'un élément.
La propriété margin et la propriété padding insèrent toutes deux un espace autour d'un élément. Cependant, la différence est que margin insère l'espace autour de la bordure, tandis que padding insère l'espace dans la bordure d'un élément.
Prise en charge du navigateur
Property | |||||
---|---|---|---|---|---|
marginTop | Yes | Yes | Yes | Yes | Yes |
Syntaxe
Renvoyez la propriété marginTop :
object.style.marginTop
Définissez la propriété marginTop :
object.style.marginTop = "%|length|auto|initial|inherit"
Valeurs de propriété
Value | Description |
---|---|
% | Defines the top margin in % of the width of the parent element |
length | Defines the top margin in length units |
auto | The browser sets the top margin |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
Détails techniques
Valeur par défaut: | 0 |
---|---|
Valeur de retour : | Une chaîne, représentant la marge supérieure d'un élément |
Version CSS | CSS1 |
Plus d'exemples
Exemple
Changez la marge supérieure d'un élément <div> en "normal":
document.getElementById("myDiv").style.marginTop = "0px";
Exemple
Renvoie la marge supérieure d'un élément <div> :
alert(document.getElementById("myDiv").style.marginTop);
Exemple
Différence entre marginTop et paddingTop :
function changeMargin() {
document.getElementById("myDiv").style.marginTop = "100px";
}
function changePadding() {
document.getElementById("myDiv2").style.paddingTop = "100px";
}
Pages connexes
Tutoriel CSS : Marge CSS
Référence CSS : propriété margin-top
Référence HTML DOM : propriété margin
❮ Objet de style