Aligning a bootstrap grid in the center for mobile devices

I've encountered an issue where my 2×3 grid of images and text is perfectly centered on desktop but has a strange offset on mobile devices. Here's how it appears on the desktop:

https://i.sstatic.net/HXqFq.jpg

and here's the layout on mobile:

https://i.sstatic.net/kKkhs.png

The structure of the HTML section is 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">
         - The rest of the HTML content remains unchanged from the original post -
      </div>
    </div>
  </div>

In addition to that, here's the custom CSS code utilized for styling:

/* 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;
}

Despite my attempts with different column values like col-md-X and playing around with classes such as mx-auto and d-block, I haven't been able to resolve this offset discrepancy on mobile. Any insights into what might be causing this issue?

Answer №1

d-flex flex-column align-items-center justify-content-center is a CSS class that centers elements vertically and horizontally.

 /* Styles for the 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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c1e1313080f080e1d0c3c49524f524c511d100c141d4d">[email protected]</a>/dist/css/bootstrap.min.css"
  rel="stylesheet"
  integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD"
  crossorigin="anonymous"
/>
<script
  src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="74161b1b00070006150434415a475a44591518041c1545">[email protected]</a>/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="Camie's 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>

Answer №2

After reviewing all the responses and comments, it appears that there is no consistent issue with duplicating the offset on mobile devices. To provide a clearer understanding of the problem you are facing, I recommend utilizing StackOverflow's code snippet feature to showcase a functional example of your code rather than relying on screen captures.

Your screen capture suggests that your page exceeds the viewport width, as indicated by the presence of a horizontal scrollbar. This could be attributed to not enclosing the first row within a container element.

The usage of pixel-based image sizes conflicts with Bootstrap's percentage-based approach. It may be more suitable to utilize the img-fluid class instead of manually adjusting these values.

In terms of item text layout, nesting a column inside another without a containing row div might not be directly related to the mobile offset issue, but adding a row div could improve the structure.

As noted by @tdy, consolidating all six columns into a single row would result in a more cohesive layout.

Consider the following CSS snippet for selected-works-grid-item-text:

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

I maintained the 350px width for the images to prevent them from stretching across the columns. Alternatively, using larger images (e.g., 700px wide) would fill the column widths better. In such cases, adjusting the offset value for item-text may be necessary.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps can I take to prevent ng-bootstrap modal from automatically centering the content within the modal?

Looking for a solution to a UX issue with my Angular ng-bootstrap modal. The problem arises when displaying messages of varying lengths - if the message is longer than the screen's vertical resolution, it gets centered on the screen. This causes incon ...

What is the most effective method to extract an ID from a URL that is written in various formats?

Does anyone know of a reliable method to extract an ID from various URL formats using javascript or jquery? https://plus.google.com/115025207826515678661/posts https://plus.google.com/115025207826515678661/ https://plus.google.com/115025207826515678661 ht ...

What steps can I take to prevent the array from constantly updating and instead only show the final task?

<h1>Custom Calendar and Task Scheduler</h1> <div class="navigation-text">Use the arrows to switch between months</div> <div class="navigation-text">Click on a date to create tasks</div> &l ...

Can we use Selenium to extract text from a webpage that is not present in the HTML but is instead fetched from an API response?

There is text in the text field, but the HTML isn't displaying it. Check out the Text field with HTML. Can Selenium extract the text "test" if it is retrieved from an API? ...

steps for including preset text in an input field within a form on WordPress

I am currently utilizing a WordPress plugin that automatically generates a form in the admin area based on user input. The form includes five fields that require URL validation. Users have the option to enter optional link text which will be displayed as t ...

Problem with jQuery offset: SWF position remains unaffected

I attempted to create a code that would position an SWF file on top of an HTML button using jQuery's offset function to determine the coordinates. However, despite my efforts, the SWF does not appear to move as intended. var offset = $("#button").off ...

Step-by-step guide on sorting WordPress posts by a custom field date

I've created an event sidebar section that will only show the next 3 upcoming events. I have successfully set up the custom post type and custom fields, but I am struggling to figure out how to order the posts based on the start date of the events, wh ...

Combining CSS to Align Two Form Fields Side by Side

I recently came across an online form and I'm attempting to customize it by aligning the phone and email text fields next to each other. I have made some progress, but the formatting is not quite right. The fields are too small and adjusting their siz ...

Retrieve the image and insert it using an img tag

Working on a project, I want to include Instagram profile pictures by embedding them. Although I have the image URL, I am struggling to integrate it into my HTML page. Take a look at this sample: This provided link displays the Instagram profile picture. ...

Conceal the navbar during the process of the ajax loader loading

My current issue involves utilizing an AJAX loader to conceal my page until all elements have loaded. Unfortunately, the navbar is not hidden along with the other content as shown in the image below. I need assistance in ensuring that everything, including ...

How can I ensure that the background color of my HTML email blends seamlessly with the color of the email client's background?

As I embark on creating an HTML email template, I am venturing into the realm of developing for emails, which I know can be quite temperamental. The challenge I'm facing is in making sure that the background of my HTML email matches that of the email ...

Issues arise when using Bootstrap-select with multiple options

Currently, I am in the process of developing a web application using Angular 6. In order to implement a customizable combo-box, I integrated the bootstrap-select library developed by Silvio Mureto into my project. However, I have encountered an issue with ...

What is the best way to transform height in pixels to percentage in order to create a responsive design that adjusts smoothly on

Currently, I am in the process of developing a responsive grid system by using a max-width of 980px to convert my fixed PX widths to percentages. However, I am facing some challenges in updating the heights that are also set in PX to %. Could anyone provid ...

Problem arising from implementing background and relative positioning in html and css

Encountering an issue with html and css. Today, attempted to create a page with a large background image for the first time. Utilizing the foundation framework, but facing a challenge. Foundation is an adaptive framework, and when resizing the browser, a ...

Retrieve the present time of an ongoing CSS3 animation

I've been trying to retrieve the current time of a running animation, but I haven't had any luck so far. I can easily grab the start time using: myelement.addEventListener('webkitAnimationStart', function (evt){ console.log(evt.elaps ...

Using Angular to Bind Checkbox Value in Typescript

I have a challenge of creating a quiz where a card is displayed with 4 questions structured like this: <div class="col-md-6"> <div class="option" id="Answer1"> <label class="Answer1"> <input value= "Answer1" type="checkbox ...

The CSS code isn't functioning properly on both desktop and mobile views

Here is what I desire to achieve: https://i.sstatic.net/8H4fv.png The CSS code I have is: And the HTML code looks like this: <body> <div class="contanier"> <div id="rightcol">Right Section</div> <div id="content">Cont ...

Adjust the height of the `<input type="file">` element to match the containing div by utilizing Bootstrap 4.5 styling

Can I update the .html file to give the "Choose File" and "Upload" options the same height as the "Upload" button in the second div, which has the py-4 tag? <div class="input-group"> <div class="custom-file py-4"> < ...

Setting an AngularJS variable as the name attribute in an HTML tag

Important Values {{item.name1}} equals item_name_1 {{item.description}} represents a Band Situation 1 <input name="item_name_1" value={{item.description}}> The input field is filled with Band Situation 2 <input name={{item.name1}} value={{ ...

Transmit data from the Windows Communication Foundation to JavaScript

Looking to invoke the WCF service through JavaScript, utilizing AJAX? Check out this resource: Calling WCF Services using jQuery Here's my query: Is there a method to retain some JavaScript-related data after making a request, and then transmit inf ...