The Bootstrap grid is not aligning to the center when dynamically adjusting sizes

I am currently in the process of developing a store website using bootstrap. One issue I am facing involves creating a grid for my product display. The problem is that the number of grid cells remains constant even when the screen size is reduced. I would like it to have 1 row when the screen is very small and 2 rows otherwise. Another challenge I am encountering is that the grid does not align centrally on the screen. Does anyone know how to resolve these issues? Thank you!

The content in each column refers to the figures.

Below is the code I am working with:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Ryan's Cookies</title>
        <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2d0ddddc6c1c6c0d3c2f2879c829c83">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
        <link href="/static/css/styles.css" rel="stylesheet">
        
    </head>
    <body class="bg-light">
        <!--Shopping cart-->
        <img src="/static/images/cart.png" class="top-right" alt="cart">

        <!--Main Title-->
        <div class="container text-center title-container">
            <h1 class="display-1">Ryan's Cookie Store</h1>
            <h1 class="display-6"><em>Fresh. Delicious. Delivered</em></h1>
        </div>

        <!--Cookie Grid-->

        <div class="container">
            <div class="row justify-content-center">
                <div class="col-4">
                    <figure class="figure">
                        <img src="/static/images/chocolate-chip.jpg" class="figure-img img-fluid rounded" alt="A generic square placeholder image with rounded corners in a figure.">
                        <figcaption class="figure-caption text-right display-8 mt-1">$7.50 <button class="btn btn-dark btn-sm sub-margin">Add To Cart</button></figcaption>
                    </figure>
                </div>
                <div class="col-4">
                    <figure class="figure">
                        <img src="/static/images/peanut-butter.jpg" class="figure-img img-fluid rounded" alt="A generic square placeholder image with rounded corners in a figure.">
                        <figcaption class="figure-caption text-right display-8 mt-1">$7.50 <button class="btn btn-dark btn-sm sub-margin">Add To Cart</button></figcaption>
                    </figure>
                </div>
            </div>
            <div class="row justify-content-center">
                <div class="col-4">
                    <figure class="figure text-center">
                        <img src="/static/images/nieman-marcus.jpg" class="figure-img img-fluid rounded" alt="A generic square placeholder image with rounded corners in a figure.">
                        <figcaption class="figure-caption text-right display-8 mt-1">$7.50$ <button class="btn btn-dark btn-sm sub-margin">Add To Cart</button></figcaption>
                    </figure>
                </div>
                <div class="col-4">
                    <figure class="figure text-center">
                        <img src="/static/images/snickerdoodle.jpg" class="figure-img img-fluid rounded" alt="A generic square placeholder image with rounded corners in a figure.">
                        <figcaption class="figure-caption text-right display-8 mt-1">$7.50 <button class="btn btn-dark btn-sm sub-margin">Add To Cart</button></figcaption>
                    </figure>
                </div>
            </div>
            <div class="row justify-content-center">
                <div class="col-4">
                    <figure class="figure text-center">
                        <img src="/static/images/tiger.jpg" class="figure-img img-fluid rounded" alt="A generic square placeholder image with rounded corners in a figure.">
                        <figcaption class="figure-caption text-right display-8 mt-1">$7.50 <button class="btn btn-dark btn-sm sub-margin">Add To Cart</button></figcaption>
                    </figure>
                </div>
            </div>
        </div>

        <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="14767b7b60676066756454213a243a25">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
    </body>
</html>

Answer №1

Upon closer inspection, a few details that may have been overlooked have come to light. I believe these additions will enhance the original answer and provide value:

Check out this codepen sample

By assigning the col elements an initial class (col-12), you can control the number of columns displayed on screens up to max-width: 575.98px, classified as extra-small.

The subsequent class with the breakpoint class included (col-sm-6) determines the column layout at min-width: 576px, categorized as small, and so forth.

Here is a breakdown of the bootstrap breakpoints:

    /* Extra small devices col-* (portrait phones, less than 576px) */
    @media (max-width: 575.98px) { ... }
    
    /* Small devices col-sm-* (landscape phones, 576px and up) */
    @media (min-width: 576px) and (max-width: 767.98px) { ... }
    
    /* Medium devices col-md-* (tablets, 768px and up) */
    @media (min-width: 768px) and (max-width: 991.98px) { ... }
    
    /* Large devices col-lg-* (desktops, 992px and up) */
    @media (min-width: 992px) and (max-width: 1199.98px) { ... }
    
    /* Extra large devices col-xl-* (large desktops, 1200px and up) */
    @media (min-width: 1200px) { ... }

To align with my understanding of bootstrap, here are the modifications I made:

PLEASE NOTE: A single row can accommodate up to 12 columns (hence, I removed the extra rows). The "justify-content-center" class pertains to flex containers and may not be necessary when the text-center class is present in the parent container. Additionally, I added container-fluid and px-0 to allow the columns to expand full width. Moreover, applying the no-gutters class on the row allows specifying padding without gutters, which could lead to overflow issues if misunderstood.

Take a look at the modified code snippet below:

Please feel free to reach out if you find this information helpful.

Thank you!

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

Ways to change the CSS styling of the placeholder attribute within a textarea or input field tag

I am currently working on adjusting the font size of the text within the placeholder attribute of a textarea element. While I have been successful in achieving this using vanilla CSS, I have encountered difficulties when trying to implement the same concep ...

Setting a default currency value (either in dollars or euros) in an input field

Is there a way to automatically insert a currency symbol (€, $) in a text field by default when entering a number? Are there any default values for this feature? Thank you Here is an example ...

I tried moving the onchange(this) function from HTML to JavaScript, but I seem to have missed a parameter. The code ends

I'm currently building a website for a fictional ice cream shop to enhance my JavaScript skills. function hideAAndB() { var pickupDiv = document.getElementById("pickupDiv"); var deliveryDiv = document.getElementById("deliveryDiv"); pickupDi ...

Boosting Your SCSS (SASS) Productivity

I'm curious if there are any ways to make the code below more efficient or dynamic. I've already achieved the desired result, but I'm interested in optimizing it further. The Script Any suggestions would be highly appreciated, Cheers, ...

Issue with CSS positioning: Struggling to place images outside a div with floating properties

Here is what I need to design along with the code I have written, HTML: <div className='relative mt-64 mb-40 md:h-56 h-36 w-full gradient z-10'> <img className='bg-transparent w-20 h-28 absolute md:top-40 md:lef ...

Is there a way to switch out the navigation icons on the react-material-ui datepicker?

Can the navigation icons on the react-material-ui datepicker be customized? I've attempted various solutions without any success. <button class="MuiButtonBase-root MuiIconButton-root MuiPickersCalendarHeader-iconButton" tabindex="0&q ...

Find the IDs of all input boxes that have a specific class attached

Is there a way to retrieve the ID of an input field that has a specific class attribute? For example: <input id="first" class="failed" /> <input id="last" class="failed" /> <input id="city" class="failed" /> <input id="state" class=" ...

Make sure to deselect all other radio buttons when selecting one, running into difficulties

Having an issue with my UI when selecting radio buttons by clicking on a div: A loop that displays different radio buttons: {% for product in collections.regalos.products %} <li class="grid__item large--one-third gift-card"> <div class="gift-s ...

Tips for shifting icons from the left to the right when collapsing a sidebar menu

Trying to solve a challenge with the sidebar width transition! How can I move icons to the right only when the sidebar is at a width of 50px, within the specified area? The current look: https://i.sstatic.net/GIc4n.png Desired outcome: https://i.sstati ...

Jquery failing to function properly when scrolling

I have been attempting to implement a feature where an element changes its position to fixed after scrolling past a certain point. However, no change occurs when I scroll. In the <head> section of my code, I have included <script src="stiler/jquer ...

Navigating within fixed-positioned divs

I'm attempting to create something similar to the layout seen on: The desired effect is to have fixed content on the right side, with only the left side scrolling up to a certain point before the entire page scrolls normally. However, in my implement ...

Steps for developing a web-based interface for my software

I could use some guidance on how and where to get started. Currently, I have a program written in vb.net that performs basic functions by executing bat files, accessing specific folders, and carrying out command line tasks. My plan is to convert this progr ...

Shopping Cart implemented in PHP

Looking for help in PHP to create a shopping cart? If you're experiencing products disappearing once the user leaves the cart area, here's what you can do: <?php foreach($almofadas as $item):?> <form action="cart.php&quo ...

``Only Firefox supports jQuery animations, all other browsers fail to render them properly

This particular issue is a bit complex to articulate, so I'll begin by providing the code and then proceed to elaborate on the problem as best as I can. I'm assuming that you've already compared the results in Firefox and other browsers. He ...

Footer not disappearing

Need assistance! My footer isn't showing up properly at the bottom even after applying clear:both. Can someone help please? If anyone can provide guidance, it would be greatly appreciated. Thanks for all your help in resolving the issue. ...

Capture a snapshot of a webpage that includes an embedded iframe

Currently, we have a nodeJS/angular 4 website that contains an iframe from a third party (powerBI Emebdded). Our goal is to develop a feature that allows the end user to capture a screenshot of the entire page, including the content within the iframe. We ...

Establishing the default scroll position in tables using React.js

How can I set the initial scroll in ReactJS Material-UI tables? I'm working with a table that has numerous columns and I would like to have specific columns in view automatically without having to scroll, essentially establishing an initial scroll. I ...

A step-by-step guide on showcasing the content from a textfield onto a dynamic column chart

How can I show the value from a text field in a column chart? I found the code for the chart on this website(). I tried using the code below, but nothing happens. Can someone please assist me? <script> window.onload = function () { ...

When a div is floated to the left and another div is floated to the right, the

I would like the footer to display an image aligned to the left and some additional text (an inline ul with links) aligned to the right within the footer. Here is the CSS & HTML: footer { clear: both; background: #113551; width: 90%; ma ...

Decoding HTML with PHP

I'm facing an issue with creating a Web page Parser. The structure of the page is as follows: <TABLE WIDTH=80%> <tr><td colspan=7><BR><BR></td></tr> <TR> <Td colspan=7><FONT FACE="arial" align ...