How To Set Margin In Html
In this article, we volition learn about the CSS Margin & Padding properties of the Box Model & understand their implementation through the instance.
CSS Margins: CSS margins are used to create space around the chemical element. We tin can gear up the different sizes of margins for individual sides(top, correct, bottom, left).
Margin properties tin have the post-obit values:
- Length in cm, px, pt, etc.
- Width % of the element.
- Margin calculated by the browser: machine.
Syntax:
body { margin: size; }
The margin holding is a autograph holding having the following individual margin properties:
- margin-top: It is used to set the top margin of an element.
- margin-right: It is used to fix the right margin of an element.
- margin-bottom: It is used to specify the amount of margin to be used on the bottom of an element.
- margin-left: It is used to set the width of the margin on the left of the desired element.
Annotation: The margin holding allows the negative values.
We will discuss all iv backdrop sequentially.
If the margin holding has 4 values:
margin: 40px 100px 120px 80px;
- height = 40px
- correct = 100px
- bottom = 120px
- left = 80px
Example: This example describes the margin belongings past specifying the 4 values.
HTML
<
html
>
<
head
>
<
style
>
p {
margin: 80px 100px 50px 80px;
}
</
way
>
</
head
>
<
body
>
<
h1
>
GeekforGeeks
</
h1
>
<
p
> Margin properties </
p
>
</
body
>
</
html
>
Output:
If the margin property has 3 values:
margin: 40px 100px 120px;
- elevation = 40px
- right and left = 100px
- lesser = 120px
Case: This instance describes the margin holding past specifying the three values.
HTML
<
html
>
<
head
>
<
style
>
p {
margin: 80px 50px 100px;
}
</
style
>
</
head
>
<
body
>
<
h1
>
GeeksforGeeks
</
h1
>
<
p
> Margin backdrop </
p
>
</
torso
>
</
html
>
Output:
If the margin property has 2 values:
margin: 40px 100px;
- top and bottom = 40px;
- left and right = 100px;
Example: This example describes the margin property by specifying the double value.
HTML
<
html
>
<
caput
>
<
fashion
>
p {
margin: 100px 150px;
}
</
way
>
</
caput
>
<
torso
>
<
h1
>
GeeksforGeeks
</
h1
>
<
p
> Margin properties </
p
>
</
trunk
>
</
html
>
Output:
If the margin holding has 1 value:
margin: 40px;
- top, right, bottom and left = 40px
Example: This example describes the margin property by specifying the single value.
HTML
<
html
>
<
head
>
<
style
>
p {
margin: 100px;
}
</
style
>
</
caput
>
<
body
>
<
h1
>
GeeksforGeeks
</
h1
>
<
p
> Margin properties </
p
>
</
body
>
</
html
>
Output:
CSS Padding: CSS paddings are used to create space around the element, within any divers edge. We can set up dissimilar paddings for individual sides(height, right, lesser, left). It is important to add border properties to implement padding properties.
Padding backdrop can have the following values:
- Length in cm, px, pt, etc.
- Width % of the element.
Syntax:
body { padding: size; }
The padding CSS shorthand property can be used to specify the padding for each side of an chemical element in the following society:
- padding-top: Information technology is used to fix the width of the padding expanse on the superlative of an element.
- padding-correct: It is used to set the width of the padding area on the correct of an element.
- padding-bottom: It is used to set the height of the padding area on the bottom of an element.
- padding-left: It is used to fix the width of the padding area on the left of an chemical element.
Note: The padding holding allows the negative values.
We volition discuss all these 4 properties sequentially.
If the padding property has 4 values:
padding: 40px 100px 120px 80px;
- top = 40px
- right = 100px
- bottom = 120px
- left = 80px
Example: This example describes the padding belongings past specifying the 4 values.
HTML
<
html
>
<
caput
>
<
style
>
p {
padding: 80px 100px 50px 80px;
border: 1px solid black;
}
</
way
>
</
head
>
<
body
>
<
h1
>GeeksforGeeks</
h1
>
<
p
>Padding backdrop</
p
>
</
body
>
</
html
>
Output:
If the padding property has 3 values:
padding: 40px 100px 120px;
- top = 40px
- right and left = 100px
- bottom = 120px
Example: This example describes the padding property by specifying the 3 values.
HTML
<
html
>
<
head
>
<
style
>
p {
padding: 80px 50px 100px;
edge: 1px solid black;
}
</
way
>
</
caput
>
<
body
>
<
h1
>GeeksforGeeks</
h1
>
<
p
>Padding properties</
p
>
</
body
>
</
html
>
Output:
If the padding property has 2 values:
padding: 100px 150px;
- top and bottom = 100px;
- left and right = 150px;
Case: This example describes the padding belongings using a double value.
HTML
<
html
>
<
head
>
<
style
>
p {
padding: 100px 150px;
border: 1px solid blackness;
}
</
way
>
</
head
>
<
torso
>
<
h1
>GeeksforGeeks</
h1
>
<
p
>Padding properties</
p
>
</
body
>
</
html
>
Output:
If the padding property has i value:
padding: 100px;
- top, correct, bottom and left = 100px
Instance: This example describes the padding property using a single value.
HTML
<
html
>
<
head
>
<
manner
>
p {
padding: 100px;
border: 1px solid black;
}
</
style
>
</
head
>
<
trunk
>
<
h1
>GeeksforGeeks</
h1
>
<
p
>Padding backdrop</
p
>
</
torso
>
</
html
>
Output:
Difference between Margin and Padding:
- Margin is used to create infinite around elements and padding is used to create space around elements inside the border.
- We tin set the margin property to car just we cannot set up the padding belongings to machine.
- In Margin property we can allow negative or bladder number but in padding we cannot permit negative values.
- Margin and padding target all 4 sides of the element. Margin and padding volition piece of work without the border property also. The difference will be more clear with the following case.
Case: This instance describes the margin & padding properties around the content.
HTML
<!DOCTYPE html>
<
html
>
<
head
>
<
style
>
h2 {
margin: 50px;
border: 70px solid dark-green;
padding: 80px;
}
</
fashion
>
</
caput
>
<
body
>
<
h1
>GeeksforGeeks</
h1
>
<
h2
>
Padding properties
</
h2
>
</
body
>
</
html
>
Output:
Supported Browser:
- Google Chrome 1.0
- Net Explorer 3.0
- Microsoft Edge 12.0
- Firefox i.0
- Opera 3.5
- Safari i.0
Source: https://www.geeksforgeeks.org/css-margins-padding/
0 Response to "How To Set Margin In Html"
Post a Comment