I have a grid of items and I’m looking to add two Titles (ie class=”work-category”), first at the beginning of the grid, and second, in the middle of the grid.

I’d like to add margin-top and margin-bottom to both Titles. However, while I can apply margin-bottom to both Titles, I CANNOT add margin-top to the second Title. I CAN add margin-top to the first Title.

See the image for clear demonstrations of the issue:
enter image description here

I’d like to add margin-top to the second Title, as well. How to fix this problem?

.portfolio {
  list-style: none;
  margin-left: auto;
  margin-right: auto;
}

.container {
  width: 1170px;
}

.row {
  margin-left: -15px;
  margin-right: -15px;
}

.work-category {
  padding: 0 30px;
  margin-top: 50px;
  margin-bottom: 50px;
}

.portfolio .one-col {
  padding: 0px 30px;
}

.col-md-4 {
  width: 50px;
  float: left;
}

.footer {
  clear: both;
  position: relative;
  bottom: 0px;
  width: 100%;
  padding-top: 104px;
  margin-top: -27px;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<div class="portfolio">
  <div class="container">
    <div class="row">
      <div class="work-category">Title 1</div>
      <div class="col-md-4 one-col">...</div>
      <div class="col-md-4 one-col">...</div>
      <div class="col-md-4 one-col">...</div>
      <div class="col-md-4 one-col">...</div>
      <div class="work-category">Title 2</div>
      <div class="col-md-4 one-col">...</div>
      <div class="col-md-4 one-col">...</div>
      <div class="col-md-4 one-col">...</div>
      <div class="col-md-4 one-col">...</div>
      <div class="footer">...</div>
    </div>
  </div>
</div>

From your snippet, there is no margin-top on any of the titles. The reason why your margin-top is not being applied is simply because Bootstrap uses a more specific selector:

.row > * {
    margin-top: var(--bs-gutter-y);
}

Where the value is a Bootstrap CSS-variable. This selector means that any direct child of .row will have this margin-top-value applied from the CSS-variable. So all you need to do is use a more specific selector, such as:

.row .work-category {
  padding: 0 30px;
  margin-top: 50px;
  margin-bottom: 50px;
}

More on CSS-specificity from MDN here

Full snippet:

.portfolio {
  list-style: none;
  margin-left: auto;
  margin-right: auto;
}

.container {
  width: 1170px;
}

.row {
  margin-left: -15px;
  margin-right: -15px;
}

.row .work-category {
  padding: 0 30px;
  margin-top: 50px;
  margin-bottom: 50px;
}

.portfolio .one-col {
  padding: 0px 30px;
}

.col-md-4 {
  width: 50px;
  float: left;
}

.footer {
  clear: both;
  position: relative;
  bottom: 0px;
  width: 100%;
  padding-top: 104px;
  margin-top: -27px;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<div class="portfolio">
  <div class="container">
    <div class="row">
      <div class="work-category">Title 1</div>
      <div class="col-md-4 one-col">column</div>
      <div class="col-md-4 one-col">column</div>
      <div class="col-md-4 one-col">column</div>
      <div class="col-md-4 one-col">column</div>
      <div class="work-category">Title 2</div>
      <div class="col-md-4 one-col">column</div>
      <div class="col-md-4 one-col">column</div>
      <div class="col-md-4 one-col">column</div>
      <div class="col-md-4 one-col">column</div>
      <div class="footer">footer</div>
    </div>
  </div>
</div>

3

Not the answer you’re looking for? Browse other questions tagged html css or ask your own question.