What steps can I take to stop full-width images from stretching to fit the entire body size when placed within a flex container?

I'm having trouble with my layout - specifically, I have one container with four columns. It looks fine when I manually set the image sizes in each column, but I want to use percentage widths for more flexibility. However, when I do this, the images end up expanding to the width of the main container rather than staying within their respective columns.

The issue seems to be related to the CSS near the bottom.

You can view a demo on CSS Desk:

Here's the HTML:

<div id=maincontent>

<div id="shop-panels" class="flexcontainer">
  <div class="panel" id="panel1">
        <div class="content">
            <img src="https://s24.postimg.org/dn7cdoifp/sedan.jpg"/>
        </div>
        <div class="caption">
            Shop Sedans
        </div>
    </div>
    <div class="panel " id="panel2">
        <div class="content">
            <img src="https://s1.postimg.org/ujt0z038v/truck.jpg" alt=""/>
        </div>
        <div class="caption">
            Shop Trucks
        </div>
    </div>
    <div class="panel " id="panel3">
        <div class="content">
            <img src="https://s24.postimg.org/w16ku1ymt/minivan.jpg" alt=""/>
        </div>
        <div class="caption">
            Shop Minivans
        </div>
    </div>
    <div class="panel " id="panel4">
        <div class="content">
            <img src="https://s22.postimg.org/6gw49w8gh/suv.jpg" alt=""/>
        </div>
        <div class="caption">
            Shop SUVs
        </div>
    </div>
</div>


</div>

And here's the relevant CSS:

body {
    font-family: 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif;
    font-size: 14px;

}

div#maincontent {
    width: 980px;
    background: #FFF;
    height: 100%;
    margin: auto;
    padding: 1em;
    box-sizing: border-box;
}

#shop-panels {
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    width: 100%;
    }
#shop-panels div.panel {
    -webkit-flex: 1 0 auto;
    -ms-flex: 1 0 auto;
    flex: 1 0 auto;
    height: 200px;
    border-radius: 1em;
    margin: .5em;
    padding: 1em;
    background: #A00;
    background: -webkit-linear-gradient(rgba(200,0,0,1),rgba(127,0,0,1));
    background: -moz-linear-gradient(rgba(200,0,0,1),rgba(127,0,0,1));
    background: linear-gradient(rgba(200,0,0,1),rgba(127,0,0,1));
    }
#shop-panels div.panel:first-child {
    margin-left: 0;
    }
#shop-panels div.panel:last-child {
    margin-right: 0;
    }
#shop-panels div.panel:hover {
    background: #C00;
    }
#shop-panels div.panel .content {
    background: #FFF;
    border-radius: 1em;
    height: 150px;
    overflow: hidden;
    text-align:center;
    }
#shop-panels div.panel .content img {
    position: relative;
    /* THIS IS THE CULPRIT RIGHT HERE */
    width: 100%;
    /* IF THIS VALUE IS 190px, EVERYTHING'S FINE. */
    top: 50%;
    -webkit-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
    }
#shop-panels div.panel > .caption {
    color: white;
    text-align: center;
    font-weight: bold;
    font-size: 1.5em;
    margin: .7em 0;
    text-shadow: 2px 2px 2px rgba(0,0,0,.8);
    }

Answer №1

#store-section div.section {
  flex: 0 1 25%;
}
#store-section div.section .content img {
  width: 100%;
}


Reason behind the functionality:

flex: 1 0 auto;

...used in your CSS, translates to:

flex-grow: 1;
flex-shrink: 0;
flex-basis: auto;

This may not be what you intend. To limit flex-items from growing beyond 25%, you should set it as:

flex: 0 1 25%;

Alternatively, you can also specify the max-width of flex items as:

max-width: calc(25% - 1em); /* adjusting for margins */

Note: Avoid reliance on prefixed CSS versions. Stick to standard syntax and utilize autoprefixer when done. Ensure compatibility by setting > 0% in the box below.

Answer №2

To modify the flex properties of #shop-panels div.panel, adjust them from 1 0 auto to simply 1, as shown below:

#shop-panels div.panel {
    -webkit-flex: 1;
    -ms-flex: 1;
    flex: 1;
    ...
}

By setting the flex-shrink property (the second value) to 0, you prevent them from shrinking in size.

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

Tips for structuring these components using HTML/CSS to work around DOMPDF's inability to handle floats

The concept I am trying to convey is illustrated in the image below. My goal is to display a user's car picture with the name underneath. Each image/name pair should be contained within a DIV so that as the user adds more cars, they will automaticall ...

How may I utilize CSS to generate a dynamically resizing rotated triangle that adjusts based on the text content?

Looking to add a dynamic badge in the top left corner of a div that adjusts its size based on the text content. Check out this example: http://jsfiddle.net/sbick/npxaasht/ <article> <div class="triangle"> <span>NEW</span ...

Accessing Django static files is proving to be difficult

I recently set up a django project and attempted to integrate an HTML template. Unfortunately, I encountered an issue when trying to access files in the static folder. Below is my current configuration: #BASE_DIR = os.path.dirname(os.path.dirname(os.path ...

Picking a live Selected choice within an HTML form

I currently have a dropdown menu that displays a list of employee names: <select class="form-control" id="assignee" name="assignee" style="max-width: 300px;"> <option value="Employee 1">Employee 1</option> <option value="Em ...

Obtain CSS3 gradient background-color of an element using JavaScript

Currently, I am using the following JavaScript (jQuery) to retrieve the background color (in RGB) of various other div elements: $theColor = $(this).css("background-color"); This method works perfectly, except when dealing with CSS3 gradients. For insta ...

Enhance the visual appeal of incoming data using Angular Material's dynamic styling feature

Is it possible to enhance the text with some CSS styling to make each item stand out like in this example: https://i.stack.imgur.com/kSyZE.png I prefer not to include a cross button or provide users with the option to add tags. The data is coming from a R ...

The Material UI button shifts to a different row

I need help adjusting the spacing between text and a button on my webpage. Currently, they are too close to each other with no space in between. How can I add some space without causing the button to move to the next line? const useStyles = makeStyles((the ...

There seems to be a problem with the while loop in the PHP code while creating a forum

Encountered a new error that says: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND type='o'' at line 1. Can someone assist in fixing this issu ...

Changing the background color of the dropdown button in Vue Bootstrap: A step-by-step guide

I've been struggling to modify the background color of a dropdown button, but no method seems to work. I've attempted changing it by using ID, removing its class and applying a new one, and even using !important to override the styles, but the ba ...

Explore interactive hover effects with jQuery and UI for a dynamic user experience!

Is it possible to initiate dragging on mouseover instead of click? Imagine a div that instantly starts dragging when hovered over. How can this be achieved? I would appreciate any guidance on how to make this happen. Thank you ...

Appearance with grey background color instead of transparency

After attempting to remove the background-color attribute, I noticed that the background remains a shade of grey instead of becoming transparent. Here is the code snippet: CSS: #contact-form button[type="submit"] { position: absolute; width: 100 ...

Certain videos may display as a blank white screen on mobile devices when utilizing an HTML5 video tag

I'm currently working on a React app that allows users to upload videos from their devices. The uploaded video is then displayed on the webpage using a <video> tag. During testing, I encountered an issue where some specific videos would only sh ...

Tips for modifying the default settings of a CSS framework like Material UI

Exploring the realm of CSS for the first time and feeling a bit lost. I am working with material ui alongside react and redux. My goal is to customize certain properties of a specific component, such as TextField with the disabled attribute. Upon inspectin ...

What are the steps for implementing the innerClass style on the searchBox within the appBase?

I'm currently incorporating the searchBox component from appbase io into my upcoming js web application, but I've been facing challenges in customizing the layout of both the search box and the list. Despite following the documentation which sug ...

Having issues with jQuery's .hover function in Chrome

<div class="front_hover"> <img class="bg_img" src="image.jpg" width="320px" height="400px"/> <div class="front_roll" style="display:none;"> <!-- CONTENT HERE --> </div> </div> <script language="Java ...

Using PHP, dynamic webpage content is displayed on top of a Navigation Bar

I needed a Navigation Bar that would be consistent across all webpages, so I decided to create a file called Header.html and use <?php include('Includes/Header.html') ?> in all of my webpages to easily add the Navbar/Header to the top of ea ...

When I submit the form, my goal is to automatically focus on the next text input field

When rendering text inputs for player names by mapping the components and passing down the player number, I aim for a functionality where the user can simply hit enter and have the screen focus on the next input. One attempt that I made is as follows: & ...

Leveraging JSON data with jQuery's ajax function

Utilizing jQuery to retrieve data from this API () has been a success. I have successfully fetched the main details such as "name", "height", and "mass". However, I am facing a challenge when trying to access the values of "homeworld", "films", "species", ...

Unable to convert cursor to jpg format

I am facing an issue where I have a jpg file stored in the same folder as my styles.css, and I want to change the cursor on a webpage to this particular jpg file. Despite my efforts, it seems like the cursor is not changing as expected. Currently, I am us ...

Python code to convert HTML into JSON format

"I'm currently searching for an efficient method to convert HTML to JSON using Python, but so far I've only come across complex ways that require manual building which seems impractical for large HTML files. Can anyone provide assistance? I' ...