Tips for preventing bootstrap from resizing the top card image too large while maintaining consistency across various image files

Hello, I'm facing an issue with bootstrap image sizing. When I insert images with smaller dimensions into a card, they are being blown up to fit the card which results in them becoming blurry. Here is the HTML code that's causing the problem: {% for item in book_list %} The Arsonist Lorem Ipsum...

More Info ~~~

When this is rendered, it produces:

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

The card-img-top elements end up much larger than the original image. This is not what I want for two reasons:

  1. I prefer the cards to be smaller with white space on each side.

  2. The resizing of the images leads to blurriness, especially when they have low resolutions to begin with.

Is there a way to make the card images smaller without altering the size of other images within similar resolution limits?

Similar to this example (image at top shows current output with black box representing full screen). It can be less than 5 cards in a row and margins don't need to be as large, just a rough idea of what I'm aiming for.

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

The goal is to display images regardless of their original dimensions like this - ensuring a 2000x3000 image doesn't occupy more space than a 200x300 one.

Attempts so far:

I've experimented with custom CSS, adjusting the height property

.card-img-top {
    width: 100%;
    height: 15vw;
    object-fit: cover;
}

and also tried using inline styles like style="width: 100%" assuming it would inherit width from the original image but still no success. I need help resolving this issue.

In summary:

How do I prevent my card images from expanding, irrespective of their original dimensions?

I'm new to this topic and platform, any guidance would be appreciated!

Thank you.

Answer №1

After numerous attempts, the solution that finally proved effective for me was updating the class name from "card-img-top" to "card-img"...

<img src="pic.jpg" class="card-img" style="width: 200px;">

Answer №2

Here is a simple layout you can use:

<div class="container-fluid">
    <div class="row mb-5">
        <div class="col py-1 d-flex align-items-stretch">
            <div class="card m-3">
                <img src="https://www.booktopia.com.au/blog/wp-content/uploads/2018/12/the-arsonist.jpg" class="card-img-top" style="width: 100%;" alt="image">

                <div class="card-body">
                    <h5 class="card-title">The Arsonist</h5>

                    <p class="card-text">Lorem Ipsum...</p>

                    <a href="#" class="btn btn-sm btn-primary">More Info</a>
                </div>
            </div>

            <div class="card  m-3">
                <img src="https://www.booktopia.com.au/blog/wp-content/uploads/2018/12/the-arsonist.jpg" class="card-img-top" style="width: 100%;" alt="image">

                <div class="card-body">
                    <h5 class="card-title">The Arsonist</h5>

                    <p class="card-text">Lorem Ipsum...</p>

                    <a href="#" class="btn btn-sm btn-primary">More Info</a>
                </div>
            </div>

            <div class="card  m-3">
                <img src="https://www.booktopia.com.au/blog/wp-content/uploads/2018/12/the-arsonist.jpg" class="card-img-top" style="width: 100%;" alt="image">

                <div class="card-body">
                    <h5 class="card-title">The Arsonist</h5>

                    <p class="card-text">Lorem Ipsum...</p>

                    <a href="#" class="btn btn-sm btn-primary">More Info</a>
                </div>
            </div>

            <div class="card m-3">
                <img src="https://www.booktopia.com.au/blog/wp-content/uploads/2018/12/the-arsonist.jpg" class="card-img-top" style="width: 100%;" alt="image">

                <div class="card-body">
                    <h5 class="card-title">The Arsonist</h5>

                    <p class="card-text">Lorem Ipsum...</p>

                    <a href="#" class="btn btn-sm btn-primary">More Info</a>
                </div>
            </div>

            <div class="card m-3">
                <img src="https://www.booktopia.com.au/blog/wp-content/uploads/2018/12/the-arsonist.jpg" class="card-img-top" style="width: 100%;" alt="image">

                <div class="card-body">
                    <h5 class="card-title">The Arsonist</h5>

                    <p class="card-text">Lorem Ipsum...</p>

                    <a href="#" class="btn btn-sm btn-primary">More Info</a>
                </div>
            </div>
        </div>
    </div>
</div>

Check out this demo here:https://jsfiddle.net/sugandhnikhil/rx4p8zgc/1/

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

Is it possible to retrieve a callback function from a select event in the Bootstrap dual listbox?

I'm fairly new to front-end development and have been using the Bootstrap DualListbox for a project. I'm looking for a way to save items moved from one list to another to a database before they are officially transferred. Is there a callback func ...

Sharing a div or text upon checking a box

I am new to JavaScript and I'm searching for an article or method to achieve the following task, whether it's through a form or just JS (without using PHP). I have a set of checkboxes labeled as box 1 - 4. When any one of them is checked, I want ...

What could be causing the HTML page to not interpret the PHP page properly?

Whenever I click on the submit button, nothing happens and my data is not inserted into the database. I'm in the process of creating a registration page (register2.html) and db_reg.php to manage database entry. However, it seems that the HTML page i ...

Issue with ng-true-value in AngularJS version 1.6.1 - not functioning as expected

Recently, I delved into AngularJS and followed an online tutorial that showcased how to utilize ng-true-value and ng-false-value. Here's the snippet: <!DOCTYPE html> <html lang="en"> <head> <script src="https://ajax.googleapis ...

Missing arrow icon in Bootstrap 4 navbar menu and sub menu

I am attempting to use the navbar component from Twitter Bootstrap 4 with a nested sub menu, but the arrow in the menu item that has a sub menu is not appearing at all, and I am unsure why. Here is where the arrow appears: https://i.sstatic.net/J6xI4.jpg ...

Positioning a floating div to the right side within a wrapper element

Is there a way to align my navicon menu button to the right edge of the wrapper div on screens smaller than 768px without causing it to float outside of the wrapper? Currently, when I try to move the navicon button to the right side using floating, it appe ...

Reducing image file sizes in Ionic 3

I have been struggling to compress an image client-side using Ionic 3 for the past couple of days. I have experimented with: ng2-img-max - encountered an error when utilizing the blue-imp-canvas-to-blob canvas.toBlob() method (which is a dependency of ng2 ...

The body's width is more compact compared to one of my containers in HTML/CSS

After completing my code for a beginner's HTML/CSS project, I realized that the main parent container holding two smaller containers needed to be set at specific widths. The two inner containers had to be exactly 650px and 270px wide, while the main p ...

Include the window.innerWidth value in the initial GET request

When dealing with a server-side rendered app that has responsive styling based on window.innerWidth, the challenge lies in how to pass the client's screen size in the initial GET request for index.html. This is crucial so that the server can prepare a ...

Price Table with Unique CSS3 Design Shapes

I'm currently working on designing a price table that resembles a bone shape. I stumbled upon some code on CodePen that could help me achieve this look, but I'm unsure of how to tweak and merge it. I'm facing difficulties when it comes to s ...

Having issues with implementing CSS3 animations on my webpage

I'm having trouble implementing a bouncing mouse animation on my website. The code works perfectly on another site, but on mine, it's not functioning at all. Here is the CSS code: .mouse { display: block; margin: 0 auto; text-alig ...

What is the proper way to incorporate quotation marks within other quotation marks?

Is there a way to generate an ID within the myFunction() function in JavaScript? I need a single string to call an HTML script as a variable. Any corrections or suggestions are welcome. Here is a sample code snippet: <button class="tablinks" onclick=" ...

When the text is long, it does not extend all the way to the right

I am working on designing a login page and have created an input text field for the user's email address. However, I have noticed that when the email address is long, there is some space left at the end of the border (I suspect it may be due to paddin ...

Is there a way to retrieve the name of the active tab in ngb-tabset when the component loads in Angular?

Incorporating ngbTabset for tab navigation in my current project has been a game-changer. However, I'm encountering an issue where the active tab name is not being displayed in the console upon app initialization. Any ideas on how to resolve this? You ...

What is the best way to implement a never-ending scrolling grid loader within a scrollable area using Codeigniter?

In my Codeigniter framework and bootstrap installation, I have multiple sub-pages. On one of these pages, I am attempting to implement an infinite scroll loader using a jQuery script from a tutorial found at gridScrollFx.js. Here is the JS file I am using: ...

Setting various font sizes for a single element

I'm looking to enhance my text by incorporating an embedded font in addition to Arial as a backup option. However, the embedded font requires a larger font size compared to Arial. Is there a way to ensure that when displaying the same text element, A ...

This code runs perfectly in JSFiddle, however, it encounters issues when implemented on a custom

This example on jsFiddle is functioning correctly: https://jsfiddle.net/qf089a0a/51/ However, when attempting to implement this example independently, the dropdown menu fails to display any options - despite functioning as expected in the jsFiddle (try i ...

The CSS styles for a:link, a:active, and a:visited do not seem to work with

My page contains the following CSS rule: a:link,a:active,a:visited{text-decoration:none;} I have encountered an issue where this rule is not working as expected. To apply the rule within a div with the id="test", I had to modify the rule like this: #tes ...

Ways to implement CSS styling to every input element excluding inputs located within list items of a specific class

input.not(li.tagit-new >input) { background-color: lightgrey; } <ul id="tagAdministrator" style="width: 505px" class="tagit ui-widget ui-widget-content ui-corner-all"> <li class="tagit-new" style="box-shadow: none;"> <input ty ...

Setting a class for a specific date on a jQuery UI calendar using the MultipleDates plugin

I recently encountered an issue with the Multiple Dates picker for jQuery UI date picker. It seems that the developer who created it has not updated it in quite some time, resulting in pre-highlighting dates no longer functioning properly. To address this ...