Images expanding beyond their designated size and overflowing the div

I am trying to use bootstrap in my HTML page to display a list of images. However, I am encountering issues where some images appear larger than others and the last image sometimes goes beyond the boundaries of my main container div. Additionally, when I resize the browser window, some items start overlapping. How can I address these problems?

Here is my CSS:

* { font-family: arial, sans-serif !important; }

body { padding-top: 50px; }

img.img-thumb {
    height: 180px;
    width: auto;
}

And here is my HTML:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">

    <link rel="stylesheet" href="main.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.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://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

    <title>Test Web</title>

</head>


<body>

    <div class="container-fluid">
        <div class="col-md-2">Left SideBar</div>

        <div class="col-md-8">
            <h2 class="text-center">Mens Shoes</h2>
            <hr>
            <div class="row">

                <div class="col-sm-6 col-md-4 col-lg-3">
                    <h4>Shoe 1</h4>
                    <img src="image-url-here"
                        alt="shoe-1" class="img-thumb" />
                    <p class="list-price text-danger">List Price: £50 </p>


                    <button type="button" class="btn btn-sm btn-success" onclick="">Details</button>
                </div>

                <!-- Additional image and button code for other shoes -->

            </div>

        </div>

        <div class="col-md-2">Right SideBar</div>

    </div>
    <br><br>

    <hr>
    <div class="col-md-12 text-center">
        &copy; Copyright 2013-2015
    </div>

</body>

</html>

You can view the JSFiddle link here.

One solution I discovered is to replace 'width: auto' with 'max-width: 100%' in the CSS for img.img-thumb. However, this causes the images to lose their aspect ratio. If I use only 'max-width:100% and max-height:100%', the images become distorted in size.

How can I properly address this issue to ensure the images maintain their proportions while fitting within the designated container?

Answer №1

Incorporate the img-responsive class to your images and eliminate the max-* attributes you previously included in BS3. This is a predefined BS3 class that automatically adjusts the height of your image based on its intrinsic aspect ratio while setting the max-width to 100%. For more information, refer to this resource about BS3 classes.

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

Verify whether a div element is styled in a specific manner

Upon initial load of my website, if the page is maximized or in fullscreen mode, the comBrand div will have specific CSS properties applied. However, during a resize event, I use the .css() function to adjust the properties of this element so it doesn&apos ...

How can I iterate over an array in JavaScript to create posts for an HTML feed using Bootstrap?

I have created a simple Bootstrap website in HTML that resembles a news feed. To populate the feed with various posts, I am using Javascript to work with arrays containing images, headlines, and captions for each post. My initial approach involves looping ...

The Masonry Grid is leaving empty spaces unfilled

I've been experimenting with Sveltekit and SCSS to create a Masonry grid. However, I'm facing an issue where the items in my grid are not filling in the empty space as expected. Instead, they seem to be following the size of the largest image, le ...

Tips for resolving issues with the navigation menu not appearing on the website

For more information, please visit the webpage at I am facing an issue where the dropdown menu does not show up when hovering on the Services page. The dropdown menu was created in WordPress using wp_nav_menu. There are 5 pages, including a Services page ...

Scrolls down to the bottom of the Material-UI popover menu

Trying to incorporate a material-ui popover with a scrollable menu <Popover {...popoverProps}> <Menu className="ui-dropdown-menu">{items}</Menu> </Popover> Custom styles applied: popoverStyle: { height ...

Error: Attempting to access property 'question' of an undefined value

Trying to render information from a local .json file using Javascript functions, I encountered an error in the console for const answer despite being defined. I temporarily commented it out to test the function, only to receive the same TypeError for quest ...

Changing the content of a PHP div with an Ajax callback inside another Ajax callback?

In the index.php file, there is a script that triggers an ajax call when a header element is clicked. This call sends a $selection variable to ajax.php, which then replaces #div1 with the received HTML data. <script> $(document).ready(function(){ ...

JavaScript menu that pops up

Hello everyone, I've recently created an HTML5 file that, when clicked on a specific href link, is supposed to display an image in a smooth and elegant manner. However, so far it hasn't been working for me as expected. Instead of smoothly popping ...

What is the proper method for implementing a scrollable effect to the <v-bottom-sheet> element within the Vuetify framework?

Within my Vue.js project, I am utilizing the v-bottom-sheet component from Vuetify framework (version 2.1.12). Upon reviewing my code, you can observe that the v-card is enclosed within the v-bottom-sheet. The issue arises with the scrollable parameter of ...

When taking a vertical picture on my iPhone, my Vue component does not function properly

Having trouble with displaying recently uploaded images on a form. If the image is taken from a phone, it may have an exif property that browsers do not handle correctly, causing the picture to appear flipped or sideways. Upside down photo To fix this is ...

Is there a way to determine the precise location of the scrollbar within a div element?

Is there a way to obtain the exact position of the scrollbar (scrollY) for a specific div, rather than the entire window? I found an example that seems similar to what I need at the following link: http://plnkr.co/edit/45gKhAIt0DrozS7f0Bz2?p=preview Ho ...

Improperly positioned Divs overlapping

Help needed: I am attempting to add a menu to a specific section. The menu div is extending off the page to the right, but it should be contained within the HOMEMENU section (the long black bar with no content). Despite using a clear:both div, the menu s ...

Refine the pandas Dataframe with a filter on a JavaScript-enabled website

I recently inherited a large software project using Python/Flask on the backend and HTML/Javascript on the frontend. I'm now looking to add some interactivity to one of the websites. I have successfully passed a dataframe to the webpage and can displa ...

send the user to a different page while transferring post data using Javascript

Currently, I have an HTML form that utilizes the post method to send data to a PHP page for processing into a MySQL database. However, before the user submits the form, there is a button that allows them to view a PDF of the entered data on a new page. Whi ...

Is it possible to extract information from a string that includes HTML code within a browser using CSS selectors without actually generating the DOM elements?

I've been struggling with this basic task for hours. I can't find any libraries that work and none of the questions here address my specific issue. Here's what I need to do: The entire page's markup is in a string format. I must use ...

Is it possible to submit form data to multiple destinations in an HTML form submission?

I am in the process of developing a quiz application that consists of two essential files: question.php and process.php Within question.php, the user is required to input their answer in a textbox and submit it by clicking a designated button. This input ...

What methods can be used to disable the back button functionality within an iframe?

I am facing an issue with embedding YouTube links on my webpage using iframes. When a user clicks on the link, it plays the video in the iframe. Below is the HTML and jQuery code I am using: HTML: <a href="http://www.youtube.com/embed/Q5im0Ssyyus"> ...

Unable to change the color of InputBase component's placeholder in React.js

I've been attempting to modify the color of the placeholder in an inputbase. I came across several methods online and tried implementing them, but none have been successful. Below are the codes I have tried. <InputBase id="input-id&quo ...

Tips for wrapping a div element around another div at the top left corner

I am working with a particular layout. <div class="comment"> <div class="leftpart"> </div> <div class="rightpart"> </div> </div> When viewing on a desktop, I maintain the original styling where each div is in its own s ...

Style the CSS exclusively to the expanded menu section

I need help with applying CSS rules to my navbar menu without affecting the collapsed menu. I came across a solution here and tried using this code: @media (min-width: 980px) { /* your conditional CSS*/ } However, the issue arises when my browser win ...