Mobile Image Sizing with Bootstrap 4 Cards

I am currently developing a responsive web application using Bootstrap 4 and incorporating cards with images on one of the pages. The layout functions properly when resizing the window on desktop view; however, upon viewing it on mobile devices, I noticed that the height of the card image is significantly smaller than expected.

Below is the HTML code snippet:

<div class="container">
 <header class="jumbotron">
     <div class="container">
         <h1>By The Numbers.</h1>
         <p>View our growing database:</p>
         <p>
            <a class="btn btn-primary btn-large" href="/players/new">Add New Player</a>
         </p>
         <p>
             <form action="/players" method="GET" class="form-inline">
                 <div class="form-group">
                     <input type="text" name="search" placeholder="Player Search..." class="form-control">
                     <input type="submit" value="Search" class="btn btn-default">
                 </div>
             </form>
         </p>
     </div>
 </header>
<div class="row text-center">
        <div class="col-lg-3 col-md-4 col-sm-6 mb-4">
            <div class="card">
                <img src="https://t3.ftcdn.net/jpg/02/12/43/28/240_F_212432820_Zf6CaVMwOXFIylDOEDqNqzURaYa7CHHc.jpg" id="playerCardImage" class="card-img-top" alt="Name">
                <div class="card-body">
                    <h5 class="card-title">Name</h5>
                    <h5 class="card-title">Detail</h5>
                    <a href="#" class="btn btn-primary">More Info</a>
                </div>
            </div>  
        </div>
</div>

To maintain uniform card height, I applied the following CSS code, which appears to be causing the issue of the image appearing very small on mobile devices.

CSS:

#playerCardImage {
width: 100%;
height: 15vh;
object-fit: cover;
}

You can view an example of the card on Codepen here: https://codepen.io/franchise/pen/MWaoXOp.

Additional Details:

  • The sizing issue does not occur when inspected using Chrome Dev Tools for mobile view; it displays as expected on mobile devices.
  • To replicate the problem, access the Codepen link on a mobile device to see the image displayed at around half the size compared to desktop view.

I would appreciate any insights on what might be causing this discrepancy. Thank you for your assistance in advance.

Answer №1

Consider creating a style based on the class card-img-top instead of using id for styling.

.card-img-top {
    width: 100%;
    height: 150px;
    object-fit: cover;
}
<!DOCTYPE html>
<html>
    <head>
        <title>Footer Pen</title>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> 
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="/stylesheets/main.css">
        <script src="https://kit.fontawesome.com/175e0bfa97.js" crossorigin="anonymous"></script>
  </head>
<body>
 <div class="container">
     <header class="jumbotron">
         <div class="container">
             <h1>By The Numbers.</h1>
             <p>View our growing database:</p>
             <p>
                <a class="btn btn-primary btn-large" href="/players/new">Add New Player</a>
             </p>
             <p>
                 <form action="/players" method="GET" class="form-inline">
                     <div class="form-group">
                         <input type="text" name="search" placeholder="Player Search..." class="form-control">
                         <input type="submit" value="Search" class="btn btn-default">
                     </div>
                 </form>
             </p>
         </div>
     </header>
    <div class="row text-center">
            <div class="col-lg-3 col-md-4 col-sm-6 mb-4">
                <div class="card">
                    <img src="https://t3.ftcdn.net/jpg/02/12/43/28/240_F_212432820_Zf6CaVMwOXFIylDOEDqNqzURaYa7CHHc.jpg" id="playerCardImage" class="card-img-top" alt="Name">
                    <div class="card-body">
                        <h5 class="card-title">Name</h5>
                        <h5 class="card-title">Detail</h5>
                        <a href="#" class="btn btn-primary">More Info</a>
                    </div>
                </div>  
            </div>
    </div>
</div>

https://stackoverflow.com/questions/61617160/bootstrap-4-card-image-resizing-on-mobile#
         <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e8988798988d9ac6829ba8d9c6d9dec6d8">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    </body>
</html>

Answer №2

You should consider using height as a percentage

#playerCardImage {
    width: 100%;
    object-fit: cover;
  }


 @media all and (max-width:768px){
  #playerCardImage {
    width: 100%;
    height: 110px;
    object-fit: cover;
  }
 }
<!DOCTYPE html>
<html>
    <head>
        <title>Footer Pen</title>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> 
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="/stylesheets/main.css">
        <script src="https://kit.fontawesome.com/175e0bfa97.js" crossorigin="anonymous"></script>
  </head>
<body>
 <div class="container">
     <header class="jumbotron">
         <div class="container">
             <h1>By The Numbers.</h1>
             <p>View our growing database:</p>
             <p>
                <a class="btn btn-primary btn-large" href="/players/new">Add New Player</a>
             </p>
             <p>
                 <form action="/players" method="GET" class="form-inline">
                     <div class="form-group">
                         <input type="text" name="search" placeholder="Player Search..." class="form-control">
                         <input type="submit" value="Search" class="btn btn-default">
                     </div>
                 </form>
             </p>
         </div>
     </header>
    <div class="row text-center">
            <div class="col-lg-3 col-md-4 col-sm-6 mb-4">
                <div class="card">
                    <img src="https://t3.ftcdn.net/jpg/02/12/43/28/240_F_212432820_Zf6CaVMwOXFIylDOEDqNqzURaYa7CHHc.jpg" id="playerCardImage" class="card-img-top" alt="Name">
                    <div class="card-body">
                        <h5 class="card-title">Name</h5>
                        <h5 class="card-title">Detail</h5>
                        <a href="#" class="btn btn-primary">More Info</a>
                    </div>
                </div>  
            </div>
    </div>
</div>


         <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="23534c535346510d495063120d12150d13">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    </body>
</html>

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

MPDF does not support certain types of characters

When utilizing MPDF to generate a PDF, it may fail if it comes across certain characters such as this one: < (lower than). I attempted to configure MPDF by setting: $this->ignore_invalid_utf8 = true; and $this->allow_charset_conversion = false ...

How to access a separate database's Javascript library in XPages resources

Is there a way to import the javascript library from another database (content.nsf) into my current xpages database resources? I am trying to load the common.js file using the code below but it's not working. Can someone please help me correct it? &l ...

Compiling Nextjs with Tailwind typically takes anywhere from 8 to 16 seconds

My first time using tailwind has resulted in slow compilation times when used with nextjs in development mode. The slowdown can be confirmed by simply removing the following code: @tailwind base; @tailwind components; @tailwind utilities; This significan ...

Error message appearing repeatedly and submit button functioning only after second click

I am currently working on my CS50 Final Project, where I am in the process of designing a web app using Flask. Specifically, this issue arises in the login/register page where I am attempting to verify if the username is already taken (through jsonify) and ...

Alignment problem detected in Firefox and Safari, but works perfectly in Chrome

Currently dealing with a CSS problem on my website that is only appearing in Safari and Firefox, while Chrome displays it correctly. The images are not positioning correctly and are stacking instead of being placed in each box as intended. I've expe ...

How can we stop Vuetify from overwriting Bootstrap3's classes when using treeshaking?

I am currently in the process of transitioning an app from jQuery/Bootstrap3 to Vue/Vuetify. My approach is to tackle one small task at a time, such as converting the Navbar into its own Vue component and then gradually updating other widgets. Since the n ...

Can variables be transmitted through Real-Time Communication in JavaScript?

Currently, I am in the process of developing a multiplayer game using three.js. The basic framework is all set up and my next step is to implement the multiplayer aspect. After some research, I came across RTC as a solution that doesn't require comple ...

Tips for Avoiding Line Breaks in CSS Divs

I am currently designing a website menu with items such as Home, Contact us, and About us. Each item is styled with a background color and text size of 125X30. I initially used the float property in CSS to align them correctly in a single line from left ...

Is it possible to adjust the positioning of this navigation style?

Discover a unique collection of navigation styles by following this link: http://tympanus.net/Development/ArrowNavigationStyles/ Be sure to explore the final style "Fill Path" at the end of the page. I'm interested in adjusting the position of the ...

issues with jquery functionality on user control page

For searching through dropdown lists in my project, I have implemented the Chosen jQuery plugin. In the Master page before the closing body tag, ensure to include the necessary CSS and jQuery script files. <link href="/assets/css/chosen.css" rel=" ...

Issue with React's setState method causing Bootstrap table view not to render

My component includes a Bootstrap Table and a Modal. The goal is for users to be able to access the modal and update the state of the data, reflecting changes in both the table and the modal. However, I am encountering an issue where only the modal view up ...

Tips for resolving the issue of the symbol ' displaying as &#39 in an Angular 2 application

I am currently working on an Angular application that makes API calls when a name displayed in a grid table is clicked. However, I have encountered an issue where names containing an apostrophe are being displayed incorrectly as &#39 instead. I managed ...

Imitate adjustment of orientation without the need to resize the browser window

Is there a way to simulate page portrait orientation on a PC browser without resizing the window? In my HTML code, I have a <div> element that displays content dynamically based on screen orientation. Is it possible to use JavaScript to trick this & ...

What is the reason behind the Chrome icon flickering momentarily while loading certain web pages?

I recently put together a basic html document, here it is: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8> <title>Just a test.</title> </head> <body> <svg ...

Is it possible in Javascript to trace the origins of a particular element's property inheritance for debugging purposes?

I'm currently dealing with an issue where the computed style font-size of a particular element is "16px". I've been attempting to pinpoint where in the CSS or JavaScript this font size setting is coming from, specifically within one of its parent ...

What method is most effective for comparing two HTML pages that contain identical content but varied markup?

I'm in search of a method to analyze two HTML pages for content. Both pages were created with React, but they have different structures. Nevertheless, the text within these pages is identical. What would be the most effective approach for comparing th ...

Increase the jQuery level and utilize the .find() method

Currently, I am delving into the realm of jquery and facing a minor hiccup with selectors. Below is the structure of my DOM: <li> <header class="title"> <span>Text</span> <a href="#work-1">My trigger</a> ...

Is there an HTML editing tool that has the ability to collapse code blocks by tags and rearrange them?

I'm in search of a text editor or IDE that includes syntax highlighting and allows me to collapse HTML code blocks by tag. I currently use Eclipse, but its "folding" feature only works on top level tags like TABLE, not child tags like TD and TR. Are t ...

Tips for fixed positioning of a div on a webpage without being affected by the Windows logo shortcut keys + Left/Right arrow

Whenever I utilize the keyboard shortcuts to move a window from one side of the screen to another and then minimize it either upwards or downwards, I find myself faced with an issue. I can accomplish this action by using the Windows key in combination with ...

Achieving a consistent user experience across both mobile and desktop devices without using a separate mobile version or media queries

Is it possible to have the same URL website.com display completely different content depending on whether it is accessed from a mobile device or not? I am looking for a solution that does not involve using CSS media queries or creating a separate subdomai ...