Bootstrap 5 margin and padding
The syntax for setting margin and padding is as follows:
{property}{sides}-{size}
property stands for property and contains:
m
– used to set themargin
p
– used to set thepadding
sides Primary direction:
t
– used to setmargin-top
orpadding-top
b
– used to setmargin-bottom
orpadding-bottom
l
– used to setmargin-left
orpadding-left
r
– used to setmargin-right
orpadding-right
x
– used to set*-left
and*-right
y
– used to set*-top
and*-bottom
- blank – used to set the
margin
orpadding
of the element in all four directions
size refers to the size of the margins:
0
– setmargin
orpadding
to0
1
– setmargin
orpadding
to$spacer * .25
2
– setmargin
orpadding
to$spacer * .5
3
– setmargin
orpadding
to$spacer
4
– setmargin
orpadding
to$spacer * 1.5
5
– setmargin
orpadding
to$spacer * 3
auto
– setmargin
to auto
Look at the following source code settings for partial margins.
.mt-0 {
margin-top: 0 !important;
}
.ml-1 {
margin-left: (spacer * .25) !important;
}
.px-2 {
padding-left: (spacer * .5) !important;
padding-right: (spacer * .5) !important;
}
.p-3 {
padding:spacer !important;
}
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap demos</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<div class="mx-auto" style="width: 200px;">
Element setting centering
</div>
<form>
<div class="form-row">
<div class="form-group">
<label for="text" class="mt-2">set margin-top:</label>
<input type="text" class="form-control" id="text" placeholder="email">
<label for="color" class="mt-2">Color:</label>
<input type="color" id="color" class="form-control" style="width: 60px;padding: 4px;" autocomplete="off" value="#656565">
</div>
</div>
</form>
</div>
</body>
</html>
Output: