CSS margin-top property

CSS Margin margin-top Property

Description

The margin-top property sets the width of an element’s top margin.

Possible Values

  • length – Any length value.
  • percentage – The width of the margin is calculated relative to the width of the element’s containing block.


  • auto – The default value allows the browser to automatically set margins.

Applies to

All HTML elements.

DOM syntax

object.style.marginTop = "5px" 

Example

Here are examples:

<html> 
<head> 
</head> 

<body> 
<p style = "margin-top: 15px; border:1px solid black;"> 
This is a paragraph with a specified top margin
</p>

<p style = "margin-top: 5%; border:1px solid black;">
This is another paragraph with a specified top margin in percent
</p>
</body>

</html>

This will produce the following result –

CSS margin-top property

Leave a Reply

Your email address will not be published. Required fields are marked *