How to set the CSS margin properties in one declaration?


The margin property defines the space around an HTML element. It is possible to use negative values to overlap content. It specifies a shorthand property for setting the margin properties in one declaration.

Example

You can try to run the following code to set margins

<html>
   <head>
   </head>
   <body>
      <p style = "margin: 20px; border:2px solid yellow;">
         All four margins will be 20px
      </p>
      <p style = "margin: 15px 4% -10px; border:2px solid red;">
         Top margin will be 15px, left and right margin will be 4% of the total width of the document, bottom margin will be -10px
      </p>
   </body>
</html>

Updated on: 03-Feb-2020

207 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements