MK-DK
MK-DK

Reputation: 1085

Set padding on all columns in grid

I am making a grid with bootstrap 3 there is looking like the below image. It is not possible for me to use Bootstrap 4, since it is a webshop with over 100.000 products, so the backend developers did not change it yet.

enter image description here

My problem is regarding the padding. All around the columns I would like to have a padding:15px;. At the moment I am giving each column the class="top-pad-d". But I am getting problems when I look at a mobile:

enter image description here

So to make my padding work, I now have to make a padding inside a media query, fx with the class="top-pad-m". But it is getting very complicated to control and weird.

For a single column the code will be:

<div class="col-sm-2">
     <a href="#"><img src="http://placehold.it/196x210" class="img-responsive top-pad-d top-pad-m"></a>
</div> 

Is there a way I can write some code there is saying - around all columns should there be 15px padding?

Please notice that I cannot set padding:15px; on all small columns, since they then will have a padding:30px;, because there is 2 rows on top of each other.

.row [class*="col-"] {
    padding-right: 7.5px;
    padding-left: 7.5px;
}
.padding-y {
    padding: 15px;
    background-color: #fff;
    margin-bottom:30px;
    border: 1px solid #ebecf0;
}
.top-pad-d {
    padding-top: 15px;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script
  src="https://code.jquery.com/jquery-3.5.1.js"
  integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
  crossorigin="anonymous"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Section 4 -->
<div class="section padding-y">
    <div class="row">
        <div class="col-sm-4">
            <a href="#"><img src="http://placehold.it/400x427" class="img-responsive"></a>
        </div>
        <div class="col-sm-2">
            <a href="#"><img src="http://placehold.it/196x210" class="img-responsive"></a>
        </div>
        <div class="col-sm-2">
            <a href="#"><img src="http://placehold.it/196x210" class="img-responsive"></a>
        </div>
        <div class="col-sm-2">
            <a href="#"><img src="http://placehold.it/196x210" class="img-responsive"></a>
        </div>
        <div class="col-sm-2">
            <a href="#"><img src="http://placehold.it/196x210" class="img-responsive"></a>
        </div>
        <div class="col-sm-2">
            <a href="#"><img src="http://placehold.it/196x210" class="img-responsive column-top-pad"></a>
        </div>
        <div class="col-sm-2">
            <a href="#"><img src="http://placehold.it/196x210" class="img-responsive column-top-pad"></a>
        </div>
        <div class="col-sm-2">
            <a href="#"><img src="http://placehold.it/196x210" class="img-responsive column-top-pad"></a>
        </div>
        <div class="col-sm-2">
            <a href="#"><img src="http://placehold.it/196x210" class="img-responsive column-top-pad"></a>
        </div>
    </div>
</div>

Upvotes: 3

Views: 652

Answers (1)

Chiel
Chiel

Reputation: 1442

I have come up with a full css solution: you will not have to alter the DOM anymore. To get to the result you want I've added padding: 7.5px to every column. Now in-between every column there is a distance of 7.5px + 7.5px = 15px. However, the outer edges still have a padding of 7.5px. That is why I've added a padding of 7.5px to the .row class. So now, there's a distance of 15px around every column.

.row [class*="col-"] {
  padding: 7.5px;
}

.row {
  background-color: #fff;
  padding: 7.5px !important;
}

.padding-y {
  margin-left: 15px !important;
  background-color: #fff;
  margin-bottom: 30px;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Section 4 -->
<div class="section padding-y">
  <div class="row">
    <div class="col-sm-4">
      <a href="#"><img src="http://placehold.it/400x427" class="img-responsive"></a>
    </div>
    <div class="col-sm-2">
      <a href="#"><img src="http://placehold.it/196x210" class="img-responsive"></a>
    </div>
    <div class="col-sm-2">
      <a href="#"><img src="http://placehold.it/196x210" class="img-responsive"></a>
    </div>
    <div class="col-sm-2">
      <a href="#"><img src="http://placehold.it/196x210" class="img-responsive"></a>
    </div>
    <div class="col-sm-2">
      <a href="#"><img src="http://placehold.it/196x210" class="img-responsive"></a>
    </div>
    <div class="col-sm-2">
      <a href="#"><img src="http://placehold.it/196x210" class="img-responsive"></a>
    </div>
    <div class="col-sm-2">
      <a href="#"><img src="http://placehold.it/196x210" class="img-responsive"></a>
    </div>
    <div class="col-sm-2">
      <a href="#"><img src="http://placehold.it/196x210" class="img-responsive"></a>
    </div>
    <div class="col-sm-2">
      <a href="#"><img src="http://placehold.it/196x210" class="img-responsive"></a>
    </div>
  </div>
</div>

Hope this helps!

Upvotes: 1

Related Questions