Bootstrap 5 margin and padding

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 the margin
  • p – used to set the padding

sides Primary direction:

  • t – used to set margin-top or padding-top
  • b – used to set margin-bottom or padding-bottom
  • l – used to set margin-left or padding-left
  • r – used to set margin-right or padding-right
  • x – used to set *-left and *-right
  • y – used to set *-top and *-bottom
  • blank – used to set the margin or padding of the element in all four directions

size refers to the size of the margins:

  • 0 – set margin or padding to 0
  • 1 – set margin or padding to $spacer * .25
  • 2 – set margin or padding to $spacer * .5
  • 3 – set margin or padding to $spacer
  • 4 – set margin or padding to $spacer * 1.5
  • 5 – set margin or padding to $spacer * 3
  • auto – set margin 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:

Bootstrap 5 margin and padding

Like(1)