How to conceal an element using CSS in a bootstrap4 `card` when the columns are narrow

In my grid layout, I have a maximum of 12 image cards per row. When there are 12 cards in a row, the size becomes quite small, so I want to show just the image. However, when there are only 6 cards in a row, I would like to display the description as well.

I attempted to use the d-none d-sm-block utilities but they are defined by the viewport rather than considering the size of the card in the column.

<div class="d-flex flex-row flex-wrap">
  <div class="col-md-4">
    <div class="card mb-5 border-0">
      <a class="link" href="{{pathFor 'thing.page' id=_id}}" >
        <img src="{{getImage}}" class="card-img-top img-fluid" alt="">
      </a>
      <div class="card-body">
        <p class="card-text">
          text 1
        </p>
        <p class="card-text">
          text 2
        </p>
      </div>
    </div>
  </div>
</div>

The value of col-md-4 is dynamically changed based on the number of columns set by the user.

My goal is for the card-text to be hidden when there are 12 columns (smaller cards), but not if there are 5 columns!

Here is an image example:

and another one here:

Answer №1

Discover these innovative card layouts utilizing a grid system with 12 columns across all devices, including large, small, and extra-small (mobile) views. Additionally, the d-none class is employed for hiding content on all devices.

Take a look at these designs and explore how they can be utilized in your projects. Feel free to experiment and adapt as needed!

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Card</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body> 
     
    <div class="container">
      <div class="row my-3">
        <div class="col-lg-1 col-sm-2 col-xs-12 my-xs-2 my-sm-2">
          <div class="card">
          <p class="text-center mt-3">hello</p><hr />
        <div class="card-body d-block d-lg-none text-center">card 1</div>
          </div>
        </div>
        
         <div class="col-lg-1 col-sm-2 col-xs-12 my-xs-2 my-sm-2">
          <div class="card">
          <p class="text-center mt-3">hello</p><hr />
        <div class="card-body d-block d-lg-none text-center">card 2</div>
          </div>
        </div>
        
        <div class="col-lg-1 col-sm-2 col-xs-12 my-xs-2 my-sm-2">
          <div class="card">
          <p class="text-center mt-3">hello</p><hr />
        <div class="card-body d-block d-lg-none text-center">card 3</div>
          </div>
        </div>
        
        <div class="col-lg-1 col-sm-2 col-xs-12 my-xs-2 my-sm-2">
          <div class="card">
          <p class="text-center mt-3">hello</p><hr />
        <div class="card-body d-block d-lg-none text-center">card 4</div>
          </div>
        </div>
        
        <div class="col-lg-1 col-sm-2 col-xs-12 my-xs-2 my-sm-2">
          <div class="card">
          <p class="text-center mt-3">hello</p><hr />
        <div class="card-body d-block d-lg-none text-center">card 5</div>
          </div>
        </div>

        [Remaining sections of code not shown here for brevity]

      </div>
    </div>

    </body>
    </html>

Explore unique card layouts with a 12-column grid system optimized for various device sizes, along with specific classes to manage visibility based on screen size.

Dive into these creative design examples and see how they can be implemented to enhance your projects. Try them out and adapt to suit your needs!

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Card</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body> 
 
<div class="container">
  <div class="row my-3">
    <div class="col-lg-1 col-sm-2 col-xs-12 my-xs-2 my-sm-2 d-xs-none">
      <div class="card">
    <div class="card-body text-center">card 1</div>
      </div>
    </div>
    
     <div class="col-lg-1 col-sm-2 col-xs-12 my-xs-2 my-sm-2 d-sm-none">
      <div class="card">
    <div class="card-body text-center">card 2</div>
      </div>
    </div>

    [Remaining sections of code not shown here for brevity]
      
  </div>
</div>

</body>
</html>

Answer №2

Utilize the d-block d-lg-none to make it work.

I have 12 cards displayed at 1200px, and then reduce to 6 cards once below 1200px.

To modify the resolution, change lg in two instances.

First in col-2 col-lg-1 and secondly in d-block d-lg-none

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

<div class="row">
        <div class="card col-2 col-lg-1">
            <img class="card-img-top" src="https://via.placeholder.com/150" alt="Card image cap">
            <div class="card-body d-block d-lg-none">
                <h5 class="card-title">Card title</h5>
                <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                <a href="#" class="btn btn-primary">Go somewhere</a>
            </div>
        </div>
        
      (remaining HTML code structure remains unchanged)

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cabaa5babaafb8e4a0b98afbe4fbfce4fa">[email protected]</a>/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

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

I'm finding it difficult to decide which comparison operators to utilize for my JavaScript game. Experimenting with both the "&&" and "||" options has left me unsure of which

In this scenario: If the user enters a number in the text box, such as 2 (paper) or 3 (scissors), the "ComputerChoice" will randomly generate a number between 1 and 3. The goal is to make the computer understand and react to different scenarios... For exa ...

Issues with the alignment of items in an item grid in CSS/HTML are causing the display to appear

I am experiencing an issue with the CSS formatting of a section on my website. The products in the product grid do not display properly in order when the screen size is too small. For instance, if you visit this specific site: and view it on a screen lar ...

Accessing the value of a hidden field within an ng-repeat loop when binding to an HTML table

I am working on a project where I have an HTML table with data being bound from AngularJS. The structure looks something like this: <tr ng-repeat="item in List| filter: { MachineType: 'Physical' }"> <td>{{item.MachineType}}< ...

"Utilizing a range input element with a set value on the slider

I have implemented the input type range property on my webpage and have applied some CSS styling to it. However, I feel like there is room for improvement. .Slider { -webkit-appearance: none; width: 75%; height: 15px; border-radius: 5px; backg ...

JavaScript and jQuery experiencing difficulty rendering proper style and image in output display

I am currently working on code that extracts information from a JSON variable and displays it on a map. The code looks like this: marker.info_window_content = place.image + '<br/>' +"<h4>" + place.name + "</h4> ...

What is the best way to show a dynamic number of data elements for a specific attribute in a Vue js component?

Currently, I am facing an issue with my Vue app where I have a product listing displayed using v-for. Each product has multiple headlines and corresponding top features. The layout for these headline-top feature pairs looks like this in the webpage: PRODU ...

Is it possible to consolidate all HTML button functionality into a single jQuery function?

Currently, I'm in the process of developing a price calculator that adjusts the price based on the selected button. However, I'm seeking a solution where I can achieve this functionality with just one function instead of cluttering the code with ...

Adjusting the height of a div based on the height of the window

Here is the HTML structure I am working with: <body> <div class="header">header</div> <div class="content"> hello </div> <div class="footer">footer</div> </body> This is the current ...

Tips for displaying a div near the cursor's location when hovering in React JS

Looking for a way to show a hidden div at cursor position when hovering on Text item1 or item2. Check out the sample GIF animation in this Link My attempt using Jquery code inside React resulted in an error: $(".text-item").mouseenter(function ( ...

Show variously colored information for each selection from the dropdown using Angular Material

I am trying to change the color of displayed content based on user selection in an Angular application. Specifically, when a user chooses an option from a dropdown list, I want the displayed text to reflect their choice by changing colors. For example, i ...

What is the best way to apply a background-image to a DIV while also implementing a double line border to prevent the image from showing through the border?

Within my DIV, I have applied a background-image and added a double border to the DIV. The image is currently visible in between the lines. Is there a method to resolve this issue and prevent the image from displaying in this manner? ...

The concept of an HTML pop-up message that hovers above the content

While working on my HTML form, I encountered an issue regarding the display of a warning message notifying users about the caps lock being on. Currently, the warning is shown correctly in a div located to the right of the text box. However, this div's ...

The bootstrap navbar dropdown feature isn't functioning properly on iPhones

Currently utilizing "bootstrap": "~3.3.4" within the mean.js framework, I am facing an issue with the navbar dropdown menu. On desktop, everything functions as expected - the dropdown opens and remains open when the icon is clicked. However, once deployed ...

Issues with properly triggering re-clicking events in AngularJS when using Bootstrap modal

I am currently utilizing Angular JS in combination with Bootstrap modal. Below is the Anchor link I am referring to: <li><a href="#" data-toggle="modal" data-target="#inviteFriendModal" ><span class="frndInvt"></span>Invite Friends ...

Having difficulty extracting information from an HTML page using Swift

I'm attempting to convert data retrieved from an HTML page into a readable format, but the string urlContent is showing as nil even though the data received from the NSURLSession is not null. This is my approach: var city = "London" var url = NSURL ...

"Utilizing the Bootstrap framework to enhance the functionality

I have a bootstrap table containing data that needs to be edited, with certain fields not displayed in the table. To enable editing, I have added an edit button to each row along with a modal form. The button successfully loads the modal form without any ...

Modifying the background color of a table row element when hovering over it

How can I make my table rows change color when hovered over without using Javascript? I tried using CSS with the following code: tr:hover { background: #000; } Unfortunately, this does not work as expected. While td:hover changes the background color ...

Adding dynamic text to a <span> tag within a <p> element is causing a disruption in my layout

I'm encountering an issue with a Dialog box that displays a message to the user regarding file deletion. Here's how it looks: +-----------------------------------------------------------------------+ | Delete File? ...

When using Angular with mdbootstrap, the mdb-tabs directive will move to the end if the ngIf condition is true

Currently facing a challenge with a significant amount of code here. It is referenced as follows: "ng-uikit-pro-standard": "file:ng-uikit-pro-standard-8.3.0.tgz", I am attempting to display a tab between 1 and 3 if a certain condition ...

The minification of CSS in Bootstrap 4 is causing unexpected outcomes

Following the installation of Bootstrap 4.0-alpha using Bower, a dist directory now contains both precompiled bootstrap.css and bootstrap.min.css files. During development, the uncompressed version is utilized, while the minified version is used for depl ...