xFactorSchool.com(Beta)
How to Do Examples
Python Examples
HTML Examples
Code Examples
C Code Examples
C++ Code Examples
C# Code Examples
Java Code Examples
Python Code Examples
HTML Code Examples
CSS Code Examples
JavaScript Code Examples
Online Editor
C Code Editor
C++ Code Editor
C# Code Editor
Java Code Editor
Python Code Editor
HTML Code Editor
CSS Code Editor
JavaScript Code Editor
Practice how to set margin to an HTML element using CSS
CSS Online Editor
Run Code
More CSS Sample Codes
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Set margin of an HTML element using CSS</title> <!--Internal CSS added--> <style> /* CSS id name used to apply css style to the divs*/ #div-1 { margin: 30px; /*30px margin at all sides of the div*/ background-color: lightpink; /*background color of the div*/ } #div-2 { margin: 20px; /*20px margin at all sides of the div*/ background-color: lightblue; /*background color of the div*/ } #div-3 { margin: 10px; /*10px margin at all sides of the div*/ background-color: lightgreen; /*background color of the div*/ } #div-4 { background-color: green; /*background color of the div*/ } #p-1 { margin: 40px; /*30px margin at all sides of the paragraph*/ position:absolute; } div{ min-height: 100px; } </style> </head> <body> <h1>How to set margin of an HTML element using CSS?</h1> <div id="div-1">First div with 30px margin at all sides</div> <div id="div-2">Second div with 20px margin at all sides</div> <div id="div-3">Third div with 10px margin at all sides</div> <div id="div-4"> <p id="p-1">Paragraph with 40px margin at all sides with absolute position.</p> </div> </body> </html>