skip to Main Content

I’m trying to center a 2×3 grid of images & text and although it works on desktop, it has a seemingly weird offset on mobile. For example, here’s how it looks on desktop:

Desktop View

and here’s the layout on mobile:

Mobile View

The HTML section is defined as follows:

<div class="row" id="selected-works-section">
    <h1>Selected Works</h1>

    <div class="col-lg-12">
      <div id="selected-works-grid" class="container-fluid">
        <div class="row">
          <div
            class="selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12"
          >
            <img
              src="/assets/images/belden_suites.jpg"
              alt="belden suites"
              class="img-fluid mx-auto d-block"
            />
            <div class="selected-works-grid-item-text col-lg-8 offset-xl-2">
              <h6>BELDEN SUITES</h6>
              <p>Creative direction and execution, 2015</p>
            </div>
          </div>

          <div
            class="selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12"
          >
            <img
              src="/assets/images/camies_kitchen.jpg"
              alt="camie's kitchen"
              class="img-fluid mx-auto d-block"
            />
            <div class="selected-works-grid-item-text col-lg-8 offset-xl-2">
              <h6>CAMIE'S KITCHEN</h6>
              <p>Concept and design, 2018</p>
            </div>
          </div>

          <div
            class="selected-works-grid-item col-lg-4 col-md-4 col-sm-12 col-xs-12"
          >
            <img
              src="/assets/images/fairhill_hall.jpg"
              alt="fairhill hall"
              class="img-fluid mx-auto d-block"
            />
            <div class="selected-works-grid-item-text col-lg-8 offset-xl-2">
              <h6>FAIRHILL HALL</h6>
              <p>Concept and rendering, 2013</p>
            </div>
          </div>
        </div>

        <div class="row">
          <div
            class="selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12"
          >
            <img
              src="/assets/images/gaels_restaurant.jpg"
              alt="gael's restaurant"
              class="img-fluid mx-auto d-block"
            />
            <div class="selected-works-grid-item-text col-lg-8 offset-xl-2">
              <h6>GAEL'S RESTAURANT</h6>
              <p>Concept, rendering and execution, 2015</p>
            </div>
          </div>

          <div
            class="selected-works-grid-item col-lg-4 col-md-4 col-sm-12 col-xs-12"
          >
            <img
              src="/assets/images/guajardo_residence.jpg"
              alt="guajardo residence"
              class="img-fluid mx-auto d-block"
            />
            <div class="selected-works-grid-item-text col-lg-8 offset-xl-2">
              <h6>GUAJARDO RESIDENCE</h6>
              <p>Concept, rendering and execution, 2025</p>
            </div>
          </div>

          <div
            class="selected-works-grid-item col-lg-4 col-md-4 col-sm-12 col-xs-12"
          >
            <img
              src="/assets/images/newark_residence.jpg"
              alt="newark residence"
              class="img-fluid mx-auto d-block"
            />
            <div class="selected-works-grid-item-text col-lg-8 offset-xl-2">
              <h6>NEWARK RESIDENCE</h6>
              <p>Concept, rendering and execution, 2020</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

and here’s the custom css code:

/* selected works section */

.selected-works-grid-item > img {
  max-width: 350px;
  max-height: 200px;
}

.selected-works-grid-item-text > h6 {
  margin-top: 20px;
  font-weight: bolder;
}

What could be causing this offset on mobile? I tried various combinations of col-md-X values and using the mx-auto & d-block classes without img-fluid but to no avail.

2

Answers


  1. d-flex flex-column align-items-center justify-content-center is used to center elements.

     /* selected works section */
    
      .selected-works-grid-item > img {
        max-width: 350px;
        max-height: 200px;
      }
    
      .selected-works-grid-item-text > h6 {
        margin-top: 20px;
        font-weight: bolder;
      }
    <link
      href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD"
      crossorigin="anonymous"
    />
    <script
      src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN"
      crossorigin="anonymous"
    ></script>
    
    <div class="container-fluid" id="selected-works-section">
      <h1>Selected Works</h1>
    
      <div class="col-lg-12">
        <div id="selected-works-grid" class="container-fluid">
          <div class="row">
            <div
              class="d-flex flex-column align-items-center justify-content-center selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12"
            >
              <img
                src="/assets/images/belden_suites.jpg"
                alt="belden suites"
                class="img-fluid mx-auto d-block"
              />
              <div
                class="d-flex flex-column align-items-center justify-content-center selected-works-grid-item-text col-lg-8 offset-xl-2"
              >
                <h6>BELDEN SUITES</h6>
                <p>Creative direction and execution, 2015</p>
              </div>
            </div>
    
            <div
              class="d-flex flex-column align-items-center justify-content-center selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12"
            >
              <img
                src="/assets/images/camies_kitchen.jpg"
                alt="camies kitchen"
                class="img-fluid mx-auto d-block"
              />
              <div
                class="d-flex flex-column align-items-center justify-content-center selected-works-grid-item-text col-lg-8 offset-xl-2"
              >
                <h6>CAMIE'S KITCHEN</h6>
                <p>Concept and design, 2018</p>
              </div>
            </div>
    
            <div
              class="d-flex flex-column align-items-center justify-content-center selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12"
            >
              <img
                src="/assets/images/fairhill_hall.jpg"
                alt="fairhill hall"
                class="img-fluid mx-auto d-block"
              />
              <div
                class="d-flex flex-column align-items-center justify-content-center selected-works-grid-item-text col-lg-8 offset-xl-2"
              >
                <h6>FAIRHILL HALL</h6>
                <p>Concept and rendering, 2013</p>
              </div>
            </div>
          </div>
    
          <div class="row">
            <div
              class="d-flex flex-column align-items-center justify-content-center selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12"
            >
              <img
                src="/assets/images/gaels_restaurant.jpg"
                alt="gael's restaurant"
                class="img-fluid mx-auto d-block"
              />
              <div
                class="d-flex flex-column align-items-center justify-content-center selected-works-grid-item-text col-lg-8 offset-xl-2"
              >
                <h6>GAEL'S RESTAURANT</h6>
                <p>Concept, rendering and execution, 2015</p>
              </div>
            </div>
    
            <div
              class="d-flex flex-column align-items-center justify-content-center selected-works-grid-item col-lg-4 col-md-4 col-sm-12 col-xs-12"
            >
              <img
                src="/assets/images/guajardo_residence.jpg"
                alt="guajardo residence"
                class="img-fluid mx-auto d-block"
              />
              <div
                class="d-flex flex-column align-items-center justify-content-center selected-works-grid-item-text col-lg-8 offset-xl-2"
              >
                <h6>GUAJARDO RESIDENCE</h6>
                <p>Concept, rendering and execution, 2025</p>
              </div>
            </div>
    
            <div
              class="d-flex flex-column align-items-center justify-content-center selected-works-grid-item col-lg-4 col-md-4 col-sm-12 col-xs-12"
            >
              <img
                src="/assets/images/newark_residence.jpg"
                alt="newark residence"
                class="img-fluid mx-auto d-block"
              />
              <div
                class="d-flex flex-column align-items-center justify-content-center selected-works-grid-item-text col-lg-8 offset-xl-2"
              >
                <h6>NEWARK RESIDENCE</h6>
                <p>Concept, rendering and execution, 2020</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    Login or Signup to reply.
  2. Like the other answers and comments, I was not able to duplicate the offset on mobile. It’s best to use StackOverflow’s code snippet function to include a working example of your code with the problem, rather than using a screen capture.

    Your screen capture seems to show a horizontal scrollbar, indicating your page is wider than the viewport. That may be because you don’t have your first row inside a container.

    You’re defining your image sizes using pixels, but Bootstrap uses percentages. It may be better to use the img-fluid class, rather than overriding those values.

    For your item text, you’re defining a column inside of a column with no containing row div. I don’t think that’s causing the mobile offset problem, but it might be good to add a row div.

    As @tdy pointed out, having all six columns in one row would work nicely.

    .selected-works-grid-item-text>h6 {
        margin-top: 20px;
        font-weight: bolder;
    }
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
    
    <div class="container">
        <div class="row" id="selected-works-section">
            <h1>Selected Works</h1>
    
            <div class="col-lg-12">
                <div id="selected-works-grid" class="container-fluid">
                    <div class="row">
                        <div class="selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12">
                            <img src="https://dummyimage.com/700x400/870000/fff.png&text=Belden+Suites" alt="Belden Suites" class="img-fluid mx-auto d-block" width="350" height="200">
                            <div class="selected-works-grid-item-text col-10 offset-1 col-sm-8 offset-sm-2 col-md-10 offset-md-1 col-lg-8 offset-lg-2">
                                <h6>BELDEN SUITES</h6>
                                <p>Creative direction and execution, 2015</p>
                            </div>
                        </div>
    
                        <div class="selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12">
                            <img src="https://dummyimage.com/700x400/877100/fff.png&text=Camie's+Kitchen" alt="Camie's Kitchen" class="img-fluid mx-auto d-block" width="350" height="200">
                            <div class="row">
                                <div class="selected-works-grid-item-text col-10 offset-1 col-sm-8 offset-sm-2 col-md-10 offset-md-1 col-lg-8 offset-lg-2">
                                    <h6>CAMIE'S KITCHEN</h6>
                                    <p>Concept and design, 2018</p>
                                </div>
                            </div>
                        </div>
    
                        <div class="selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12">
                            <img src="https://dummyimage.com/700x400/2D8700/fff.png&text=Fairhill+Hall" alt="Fairhill Hall" class="img-fluid mx-auto d-block" width="350" height="200">
                            <div class="row">
                                <div class="selected-works-grid-item-text col-10 offset-1 col-sm-8 offset-sm-2 col-md-10 offset-md-1 col-lg-8 offset-lg-2">
                                    <h6>FAIRHILL HALL</h6>
                                    <p>Concept and rendering, 2013</p>
                                </div>
                            </div>
                        </div>
    
                        <div class="selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12">
                            <img src="https://dummyimage.com/700x400/008744/fff.png&text=Gael's+Restaurant" alt="Gael's Restaurant" class="img-fluid mx-auto d-block" width="350" height="200">
                            <div class="row">
                                <div class="selected-works-grid-item-text col-10 offset-1 col-sm-8 offset-sm-2 col-md-10 offset-md-1 col-lg-8 offset-lg-2">
                                    <h6>GAEL'S RESTAURANT</h6>
                                    <p>Concept, rendering and execution, 2015</p>
                                </div>
                            </div>
                        </div>
    
                        <div class="selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12">
                            <img src="https://dummyimage.com/700x400/005A87/fff.png&text=Guarjardo+Residence" alt="Guajardo Residence" class="img-fluid mx-auto d-block" width="350" height="200">
                            <div class="row">
                                <div class="selected-works-grid-item-text col-10 offset-1 col-sm-8 offset-sm-2 col-md-10 offset-md-1 col-lg-8 offset-lg-2">
                                    <h6>GUAJARDO RESIDENCE</h6>
                                    <p>Concept, rendering and execution, 2025</p>
                                </div>
                            </div>
                        </div>
    
                        <div class="selected-works-grid-item col-lg-4 col-md-6 col-sm-12 col-xs-12">
                            <img src="https://dummyimage.com/700x400/170087/fff.png&text=Newark+Residence" alt="Newark Residence" class="img-fluid mx-auto d-block" width="350" height="200">
                            <div class="row">
                                <div class="selected-works-grid-item-text col-10 offset-1 col-sm-8 offset-sm-2 col-md-10 offset-md-1 col-lg-8 offset-lg-2">
                                    <h6>NEWARK RESIDENCE</h6>
                                    <p>Concept, rendering and execution, 2020</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    I stayed with your 350px wide image size, so the images won’t fill the columns. You could use a larger image (e.g. 700px wide), which would fill the column widths. If you do that, you might want to change the offset value for the item-text.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search