This code will create a div element with a red background, a white border, and a 10px margin and padding. The margin will push the div element 10px away from its surroundings, while the padding will push the div element's content 10px away from its border.
You can adjust the margin and padding values to suit your needs. You can also use margin and padding to create different effects, such as creating space between elements or adding a border around an element.
Margins and Padding are two important properties of CSS that help in creating a space between the elements of the latter. Margins are used to create a space outside the element.
e.g. if you create a div and you want a space from the outside of the div of 10px then you should apply margin property.
Whereas, if you want a space inside an element you should use padding.
As in the above example, you have created a div and on that div you have added some paragraph and you want 10px space between the border of the div and the paragraph then you should go through the padding.
Let us know this with the help of an example:
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta charset='utf-8' />
<title>Margin and Padding Example | Mindstick</title>
<style>
body {
margin-left: 30%;
margin-right: 30%
}
.bor1 {
border: 1px solid black;
}
.bor2 {
border: 1px solid red;
}
.d-flex {
display: flex;
}
.justify-content-center {
justify-content: center;
}
.m-1 {
margin: 10px;
}
.p-1 {
padding: 10px;
}
</style>
</head>
<body class='bor2'>
<div class='d-flex justify-content-center bor1 m-1' >
<p class='p-1'>Here is the example of Margin and padding</p>
</div>
</body>
</html>
Here, you can understand all the logics behind Margin and padding
I think now your confusion got solved.
Note : Margin here is shown with red color and Padding with black.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Margin and padding are two CSS properties that are used to control the space around an element. However, they do so in different ways.
Margin is used to create space outside of an element, while padding is used to create space inside of an element.
In other words, margin pushes an element away from its surroundings, while padding pushes the element's content away from its border.
Here is a table that summarizes the key differences between margin and padding:
drive_spreadsheetExport to Sheets
Here is an example of how to use margin and padding to create a div element with a red background and a white border:
Code snippet
This code will create a div element with a red background, a white border, and a 10px margin and padding. The margin will push the div element 10px away from its surroundings, while the padding will push the div element's content 10px away from its border.
You can adjust the margin and padding values to suit your needs. You can also use margin and padding to create different effects, such as creating space between elements or adding a border around an element.
Margins and Padding are two important properties of CSS that help in creating a space between the elements of the latter. Margins are used to create a space outside the element.
e.g. if you create a div and you want a space from the outside of the div of 10px then you should apply margin property.
Whereas, if you want a space inside an element you should use padding.
As in the above example, you have created a div and on that div you have added some paragraph and you want 10px space between the border of the div and the paragraph then you should go through the padding.
Let us know this with the help of an example:
Here, you can understand all the logics behind Margin and padding
I think now your confusion got solved.
Note : Margin here is shown with red color and Padding with black.