user1234: I'm interested in decreasing the amount of items shown on the screen when it's smaller

Currently working on the design of the Search page for an online store. I aim to adjust the number of items displayed on the screen gradually as the screen size changes, similar to Amazon's layout. Additionally, I'm looking to rectify the excess white space issue that occurs when the first line contains 6 items and the second line only has 4.

Any solutions or suggestions? You can reach me at [email protected].

The objective is as follows

  1. On a full screen (1920px): Display 6 items per row
  2. At 1575px & above: Show 5 items per row and hide 1
  3. For 1270px & above: Present 4 items per row and hide 2
  4. When the width is 970px & above: Exhibit 3 items per row and hide 3
  5. If the width is 670px & above: Showcase 2 items per row and hide 4
  6. When the screen size is less than or equal to 670px: Display one item per row and hide five

I attempted to achieve this using CSS but encountered difficulties.

Here is the CSS Code

@media (min-width: 1250px) {
    .col#card-row {
        flex: 0 0 25%; /* Display 4 items in a row */
        max-width: 25%;
    }
}

@media (max-width: 1249px) {
    .col#card-row {
        display: none; /* Hide all items by default */
    }
}

@media (max-width: 780px) {
    .col#card-row:nth-child(-n+2) {
        display: block; /* Show the first 2 items */
    }

    .col#card-row:nth-child(n+3) {
        display: none; /* Hide the remaining items */
    }
}

@media (max-width: 500px) {
    .col#card-row:first-child {
        display: block; /* Only show the first item */
    }

    .col#card-row:nth-child(n+2) {
        display: none; /* Hide the rest of the items */
    }
}

This is the HTML Code

<div class="container-fluid">
    {{-- item 1  --}}
    <div class="row g-3 mb-5" id="card-row">
        <div class="col">
            <div class="card" style="width: 18rem;">
                <img src="..." class="card-img-top" alt="product image">
                
                <div class="card-body">
                    <h1 class="h3">Product Name</h1>
                    <h2 class="h4">$50</h2>
                    <a href="" class="text-muted text-decoration-none">Shop Name</a>
                </div>
            </div>
        </div>>

        <!-- Repeat above structure for other items -->

    </div>

    {{-- item 2  --}}
    <div class="row g-3 mb-5" id="card-row">
        <div class="col">
            <div class="card" style="width: 18rem;">
                <img src="..." class="card-img-top" alt="product image">
                
                <div class="card-body">
                    <h1 class="h3">Product Name</h1>
                    <h2 class="h4">$50</h2>
                    <a href="" class="text-muted text-decoration-none">Shop Name</a>
                </div>
            </div>
        </div>>

        <!-- Repeat above structure for other items -->

    </div>
</div>

Answer №1

After reviewing the example you provided, it seems that implementing this code will override some of the default Bootstrap features in order to achieve a specific layout. Have you considered utilizing all of the available Bootstrap classes to create the desired layout and ensure responsiveness at various breakpoints?

Here is a suggested code snippet:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="01636e6e75727573607141342f322f32">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f7d70706b6c6b6d7e6f5f2a312c312c">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</head>
<body>

    <div class="container-fluid mt-5">
        {{-- item 1  --}}
        <div class="row g-3 mb-5" id="card-row">
            <div class="col-xxl-2 col-xl-2 col-lg-3 col-md-4 col-sm-6 col-xs-12">
                <div class="card" style="">
                    <img src="..." class="card-img-top" alt="product image">
                    
                    <div class="card-body">
                        <h1 class="h3">Product Name1</h1>
                        <h2 class="h4">$50</h2>
                        <a href="" class="text-muted text-decoration-none">Shop Name</a>
                    </div>
                </div>
            </div>
            
            <div class="d-none col-xxl-2 col-xl-2 col-lg-3 col-md-4  col-sm-6 d-xxl-block d-xl-block d-lg-block d-md-block d-sm-block">
                <div class="card" style="">
                    <img src="..." class="card-img-top" alt="product image">
                    
                    <div class="card-body">
                        <h1 class="h3">Product Name2</h1>
                        <h2 class="h4">$50</h2>
                        <a href="" class="text-muted text-decoration-none">Shop Name</a>
                    </div>
                </div>
            </div>
    
            <div class="d-none col-xxl-2 col-xl-2 col-lg-3 col-md-4 d-xxl-block d-xl-block d-lg-block d-md-block">
                <div class="card" style="">
                    <img src="..." class="card-img-top" alt="product image">
                    
                    <div class="card-body">
                        <h1 class="h3">Product Name3</h1>
                        <h2 class="h4">$50</h2>
                        <a href="" class="text-muted text-decoration-none">Shop Name</a>
                    </div>
                </div>
            </div>
    
            <div class="d-none col-xxl-2 col-xl-2 col-lg-3 d-xxl-block d-xl-block d-lg-block">
                <div class="card" style="">
                    <img src="..." class="card-img-top" alt="product image">
                    
                    <div class="card-body">
                        <h1 class="h3">Product Name4</h1>
                        <h2 class="h4">$50</h2>
                        <a href="" class="text-muted text-decoration-none">Shop Name</a>
                    </div>
                </div>
            </div>
    
            <div class="d-none col-xxl-2 col-xl-2 d-xxl-block d-xl-block">
                <div class="card" style="">
                    <img src="..." class="card-img-top" alt="product image">
                    
                    <div class="card-body">
                        <h1 class="h3">Product Name5</h1>
                        <h2 class="h4">$50</h2>
                        <a href="" class="text-muted text-decoration-none">Shop Name</a>
                    </div>
                </div>
            </div>
    
            <div class="col-xxl-2 d-xxl-block d-none ">
                <div class="card" style="">
                    <img src="..." class="card-img-top" alt="product image">
                    
                    <div class="card-body">
                        <h1 class="h3">Product Name6</h1>
                        <h2 class="h4">$50</h2>
                        <a href="" class="text-muted text-decoration-none">Shop Name</a>
                    </div>
                </div>
            </div>
        </div>
    
        {{-- item 2  --}}
        <div class="row g-3 mb-5" id="card-row">
            <div class="col-xxl-2 col-xl-2 col-lg-3 col-md-4 col-sm-6 col-xs-12">
                <div class="card" style="">
                    <img src="..." class="card-img-top" alt="product image">
                    
                    <div class="card-body">
                        <h1 class="h3">Product Name1</h1>
                        <h2 class="h4">$50</h2>
                        <a href="" class="text-muted text-decoration-none">Shop Name</a>
                    </div>
                </div>
            </div>
            
            <div class="d-none col-xxl-2 col-xl-2 col-lg-3 col-md-4  col-sm-6 d-xxl-block d-xl-block d-lg-block d-md-block d-sm-block">
                <div class="card" style="">
                    <img src="..." class="card-img-top" alt="product image">
                    
                    <div class="card-body">
                        <h1 class="h3">Product Name2</h1>
                        <h2 class="h4">$50</h2>
                        <a href="" class="text-muted text-decoration-none">Shop Name</a>
                    </div>
                </div>
            </div>
    
            <div class="d-none col-xxl-2 col-xl-2 col-lg-3 col-md-4 d-xxl-block d-xl-block d-lg-block d-md-block">
                <div class="card" style="">
                    <img src="..." class="card-img-top" alt="product image">
                    
                    <div class="card-body">
                        <h1 class="h3">Product Name3</h1>
                        <h2 class="h4">$50</h2>
                        <a href="" class="text-muted text-decoration-none">Shop Name</a>
                    </div>
                </div>
            </div>
    
            <div class="d-none col-xxl-2 col-xl-2 col-lg-3 d-xxl-block d-xl-block d-lg-block">
                <div class="card" style="">
                    <img src="..." class="card-img-top" alt="product image">
                    
                    <div class="card-body">
                        <h1 class="h3">Product Name4</h1>
                        <h2 class="h4">$50</h2>
                        <a href="" class="text-muted text-decoration-none">Shop Name</a>
                    </div>
                </div>
            </div>
    
            <div class="d-none col-xxl-2 col-xl-2 d-xxl-block d-xl-block">
                <div class="card" style="">
                    <img src="..." class="card-img-top" alt="product image">
                    
                    <div class="card-body">
                        <h1 class="h3">Product Name5</h1>
                        <h2 class="h4">$50</h2>
                        <a href="" class="text-muted text-decoration-none">Shop Name</a>
                    </div>
                </div>
            </div>
    
            <div class="col-xxl-2 d-xxl-block d-none ">
                <div class="card" style="">
                    <img src="..." class="card-img-top" alt="product image">
                    
                    <div class="card-body">
                        <h1 class="h3">Product Name6</h1>
                        <h2 class="h4">$50</h2>
                        <a href="" class="text-muted text-decoration-none">Shop Name</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

I hope this solution proves useful for your project.

Answer №2

If only I had taken the time to review the bootstrap documentation before posing my question.

Here is the solution I came up with:

<div class="row row-xl-cols-6 row-cols-lg-4 row-cols-md-3 row-cols-xs-1 g-3 mb-5" id="card-row">

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

Hide a div when multiple classes are filtered using jQuery

I have several divs with the class .item. When the user clicks on the Classfilter submit button, all .item elements that also have at least one class from the dateClasses array (for example ['28.09.2015', '29.09.2015']) should be hidden ...

Creating a layout with 2 rows and 5 columns in Bootstrap 5 may not perfectly fit a 100% width and 100% height

Can someone assist in adjusting this Bootstrap 5 code to perfectly fill the entire screen of all 16:9 devices (mobile and desktop/1080p/2160p using Chrome browser) with 100% width and height? The goal is to eliminate horizontal and vertical scrolling. Desp ...

Using HTML, CSS, and jQuery to create a master-detail feature

I'm looking to implement a master-detail layout, so I decided to follow this tutorial for guidance: While trying to replicate the tutorial, I encountered an issue with my code. Here's the code snippet that I am working on: HTML/jQuery <!DO ...

Using a <div> to contain <mat-card> in Angular Material

Can you achieve the following: Show components with specified width in a row instead of the usual column layout Enclose the cards within another defined container I've been attempting to accomplish this but without success so far.... While materia ...

How to ensure scrollbar adjusts to increasing height when using scroll:auto?

My <div> element has the style property scroll:auto. Whenever text is added to the <div> and the content height exceeds the default height, I find myself having to scroll down in order to see the latest lines of text that have been added. What ...

Adding a stylesheet dynamically to the <head> tag using $routeProvider in AngularJS

Is there a way to load a specific CSS file only when a user visits the contact.html view on my AngularJS application or site? I came across this helpful answer that almost made sense to me How to include view/partial specific styling in AngularJS. The acce ...

Looking to include a badge within the nebular menu

Can someone assist me with adding a badge to the Nebular menu to display the inbox count dynamically? Any help would be greatly appreciated. Thanks! import { NbMenuItem } from '@nebular/theme'; export const MENU_ITEMS: NbMenuItem[] = [ { ti ...

Methods for anchoring an element at the bottom of a square container div

* { box-sizing: border-box; } .publication { display: flex; width: 100%; margin-top: 6%; } .bottom1, .bottom2 { display: flex; flex-direction: column; ...

Troubleshooting a unique CSS bug in jQuery mouseover functionality

Check out this pen: https://codepen.io/anon/pen/eKzEVX?editors=1111 I recently created a Form Select in Laravel: {!! Form::select('status_id', $statuses, $post->status_id, ['class' => 'form-control post-sub-items-label &apo ...

Adding a three-dimensional perspective to an HTML5 canvas without utilizing CSS3 or WebGL

Here is a canvas I am working with: http://jsbin.com/soserubafe Here is the JavaScript code associated with it: var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); var w = canvas.width; var h = canvas.height; var cw=canvas. ...

Prevent Overflow with Hidden Setting, Use Cover for Background Image, and Optimize for Anchors and

I'm currently working on a page that is designed not to scroll vertically. If you'd like to see the page with the issue I'm encountering for reference, you can visit Everything has been running smoothly except for two specific issues: W ...

Tips for Building Common Controller Logic in CakePHP

I am in the process of developing a website where I have set up dynamic content for the footer, which is retrieved from a database. I am looking to create a single controller logic code that can be used across all pages on the site. Any suggestions or gu ...

Is there a quicker alternative to html.fromhtml for adding html-styled text to textviews?

Looking for a quicker solution than using Html.fromHtml to set text in multiple TextViews. Open to suggestions of support libraries that may offer a faster method. Spannables have been considered, but they don't allow for line breaks without using Spa ...

Tips for using nested flexbox with images

I am facing a straightforward use case where I have an image and a div containing text. The current setting for the div is flex column. My goal is to change the flex setting to row so that the text and image appear side by side. Despite having the corre ...

Interactive Django table using AJAX

This marks the second question in a series regarding my Django project. The code utilized here contains sections borrowed from this post: Stack Overflow The goal is to implement a dynamic table that cycles through objects in a list (currently set at an in ...

Changing Content Dynamically in Adobe Muse

I'm in the process of creating a website for an internet radio station and I've chosen to use Adobe Muse due to its user-friendly design features. My goal is to have the header and footer sections stay static while only the content on the page ch ...

Are HTML attribute names really case-sensitive? A topic filled with conflicting information

After learning that attribute names are case-sensitive, I found conflicting information. A source mentioned that attribute names are indeed case-sensitive. For example, the width attributes in <div> and <DIV> would be considered separate due ...

Tracking the movement of a handheld device through GPS technology

I am interested in creating a mobile application that can track the real-time location of users who have the app installed on their devices. The concept is to allow one or more users to follow the movement of another user using GPS on a map. I plan to deve ...

Is there a way to adjust the orientation of a <video> element without affecting the orientation of the video controls?

<video controls> </video> video { transform: scaleX(-1) } This CSS code flips the video horizontally, but it also flips the control buttons along with it. I attempted to flip the wrapper element containing the video using .wrapper {transfor ...

What is the best way to ensure that only one div is toggled at a time while using the toggle class function?

$(document).ready(function(){ $("#items").children().click(function(){ $(this).toggleClass('clicked'); }); }); .clicked { background-color:red; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></s ...