Flexbox height not adjusting based on the length of the text within

I'm trying to create multiple child containers that contain an image and some text, with the container size adjusting based on the amount of text. The goal is for the image height to be 70% of the container's height, allowing the text to flow around it (in Desktop View Mode).

https://i.sstatic.net/8ZEf4.jpg

However, I'm encountering issues where the image does not resize itself and the inner container remains the same size regardless of the text length in the secondary and tertiary containers.

https://i.sstatic.net/3iLXM.jpg

The CSS I've implemented is as follows:

.OuterFlexContainer {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    border-style: solid;
    margin: 1px;
}
.InnerFlexContainer {
    display:inline-flex;
    align-items: center;
    border-style: solid;
    margin: 2px;
    padding: 2px;
}
.InnerBlockContainer {
    display:block;
    margin: 2px;
    padding: 2px;
}
.TextContainer {
    display: block;
    margin: 1em;
    margin-inline-start: 1em;
    margin-inline-end: 1em;
}
.image{
     display: block; 
    float: left; 
        width: auto; 
            height: auto; /*Changing it to 70% makes the image disappear*/ 
                margin: 1em;
}

The 'InnerBlockContainer' (Display:Block) is necessary in addition to the 'InnerFlexContainer' because 'Image:Float Left' does not function inside the Flex Container.

How can I ensure both 'InnerBlockContainer' and 'InnerFlexContainer' adjust according to the amount of text while also making sure that the image resizes itself to 70% of the container's size?

Below you will find the complete code snippet and JS Fiddle link:

.OuterFlexContainer {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    border-style: solid;
    margin: 1px;
}
.InnerFlexContainer {
    display:inline-flex;
    align-items: center;
    border-style: solid;
    margin: 2px;
    padding: 2px;
}
.InnerBlockContainer {
    display:block;
    margin: 2px;
    padding: 2px;
}
.TextContainer {
    display: block;
    margin: 1em;
    margin-inline-start: 1em;
    margin-inline-end: 1em;
}
.image{
    display: block;
    float: left;
    width: auto;
    height: auto; /*Changing it to 70% makes the image dissapear*/
    margin: 1em;
}
<div class="OuterFlexContainer">
<div class="InnerFlexContainer">
<div class="InnerBlockContainer">
<img class="image" src="https://cdn.pixabay.com/photo/2018/10/30/16/06/water-lily-3784022__340.jpg">
<div class="TextContainer">
<p>Matheran was identified by Hugh Poyntz Malet, the then district collector of Thane district in May 1850. Lord Elphinstone, the then Governor of Bombay laid the foundations of the development as a future hill station. The British developed Matheran as a resort to beat the summer heat in the region.</p>
<p>Matheran is the birthplace of freedom fighter Veer Bhai Kotwal. He was born on 1 December 1912 in a Barber family. The state government has built a monument in his memory. The Matheran Hill Railway was built in 1907 by Sir Adamjee Peerbhoy and covers a distance of 20 km (12 mi), over large swathes of forest territory.</p>
<p>The Matheran hill railway, also known as Matheran Light Railway (MLR), was inspected by UNESCO world heritage site officials but failed to make it to the list as a World Heritage Site. India's other Hill Railways like the Darjeeling Railway, the Kangra Valley Railway, Nilgiri Mountain Railway are already on the list.</p>
</div>
</div>
</div>
</div>

JS Fiddle: https://jsfiddle.net/mithunu/4d1c2L0b/

Answer №1

Kindly include the following in the code:

  • max-width:100px; within the .image class

.OuterFlexContainer {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    border-style: solid;
    margin: 1px;
}
.InnerFlexContainer {
    display:inline-flex;
    align-items: center;
    border-style: solid;
    margin: 2px;
    padding: 2px;
}
.InnerBlockContainer {
    display:block;
    margin: 2px;
    padding: 2px;
}
.TextContainer {
    display: block;
    margin: 1em;
    margin-inline-start: 1em;
    margin-inline-end: 1em;
}
.image{
    display: block;
    float: left;
    width: auto;
    max-width:100px;
    height: auto; /*Changing it to 70% makes the image disappear*/
    margin: 1em;
}
<div class="OuterFlexContainer">
    <div class="InnerFlexContainer">
        <div class="InnerBlockContainer">
            <img class="image" src="https://cdn.pixabay.com/photo/2018/10/30/16/06/water-lily-3784022__340.jpg">
            <div class="TextContainer">
                <p>Matheran was identified by Hugh Poyntz Malet, the then district collector of Thane district in May 1850. Lord Elphinstone, the then Governor of Bombay laid the foundations of the development as a future hill station. The British developed Matheran as a resort to beat the summer heat in the region.</p>
                <p>Matheran is the birthplace of freedom fighter Veer Bhai Kotwal. He was born on 1 December 1912 in a Barber family. The state government has built a monument in his memory. The Matheran Hill Railway was built in 1907 by Sir Adamjee Peerbhoy and covers a distance of 20 km (12 mi), over large swathes of forest territory.</p>
                <p>The Matheran hill railway, also known as Matheran Light Railway (MLR), was inspected by UNESCO world heritage site officials but failed to make it to the list as a World Heritage Site. India's other Hill Railways like the Darjeeling Railway, the Kangra Valley Railway, Nilgiri Mountain Railway are already on the list.</p>
            </div>
        </div>
    </div>
    <div class="InnerFlexContainer">
        <div class="InnerBlockContainer">
            <img class="image" src="https://cdn.pixabay.com/photo/2018/10/30/16/06/water-lily-3784022__340.jpg">
            <div class="TextContainer">
                <p>Matheran was identified by Hugh Poyntz Malet, the then district collector of Thane district in May 1850. Lord Elphinstone, the then Governor of Bombay laid the foundations of the development as a future hill station. The British developed Matheran as a resort to beat the summer heat in the region.</p>
                <p>Matheran is the birthplace of freedom fighter Veer Bhai Kotwal. He was born on 1 December 1912 in a Barber family. The state government has built a monument in his memory. The Matheran Hill Railway was built in 1907 by Sir Adamjee Peerbhoy and covers a distance of 20 km (12 mi), over large swathes of forest territory.</p>
            </div>
        </div>
    </div>
    <div class="InnerFlexContainer">
        <div class="InnerBlockContainer">
            <img class="image" src="https://cdn.pixabay.com/photo/2018/10/30/16/06/water-lily-3784022__340.jpg">
            <div class="TextContainer">
                <p>Matheran was identified by Hugh Poyntz Malet, the then district collector of Thane district in May 1850. Lord Elphinstone, the then Governor of Bombay laid the foundations of the development as a future hill station. The British developed Matheran as a resort to beat the summer heat in the region.</p>
            </div>
        </div>
    </div>
</div>

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

Why does PHP struggle to echo a JPEG image yet perform flawlessly when writing the same image to a file?

My PHP function uses curl to fetch an image, and when I save it to a file and view it in a browser, the image looks perfect. However, when I try to display the curl results using "echo" in my PHP script, the browser shows the broken image icon. You can see ...

Display a division upon choosing an option

I am working on a project that involves a selection menu in the form of a drop-down list. <select> <option id="one" value="something">Car</option> <option id="two" value="anything">Plane</option> </select> Also, I ...

Is it possible to adjust the default zoom level of my website by utilizing HTML or CSS?

After creating my website with a 50% zoom setting, I've encountered an issue where it appears too zoomed in for users whose browser default is set at 100%. How can I fix this problem? I attempted to use *{zoom:50%} in CSS to set the default zoom leve ...

What is the best way to arrange these badges in a row?

I'm struggling to horizontally align these badges next to each other, as inline-block isn't working and they keep stacking vertically instead of side by side. How can I achieve the alignment shown in the image below? This is the desired appeara ...

How to center the navigation text on a WordPress website within the header.php file

Check out my website at I'm looking to center the navigation menu under the image. I've attempted multiple methods but can't seem to figure it out. Here's a snippet from the header.php file: <center><img src="http ...

Run a PHP function using <button onclick=""> tag

Is it possible to trigger the execution of a PHP script when clicking an HTML button? I am aware that simply calling a PHP function directly from the button's onclick event like this: <button onclick="myPhpFunction("testString")">Button</butt ...

Tips for positioning a button beside a ListItem

Is there a way to place a button next to each list item in ASP.NET? <asp:CheckBoxList ID="lstBrembo" runat="server"> <asp:ListItem Text="Popular" Value="9"></asp:ListItem> // <--- trying to add button here, but getting parse error ...

Is there a way to make the header reach the full width of the page?

Is there a way to make my header extend across the entire page? I attempted using margin-left and right, but it didn't yield the desired outcome. Header.css .header{ background: green; height: 70px; width: 100%; display: flex; ju ...

Display a full-screen image and align it horizontally from the center using CSS styling

When I fill a screen with an image using the following code: width:auto; height:100%; The image fills the screen vertically and almost entirely horizontally, but it gets cropped off on the right side. Is there a way to make the image float centered on th ...

Using HTML and CSS to evenly align text in individual sections

Is it possible to recreate the exact look and feel of a justified page from a book or article online using HTML/CSS? This would include replicating the text justification with precise line breaks and setting the outer wrapper width to match the min/max-wid ...

Video with a personalized play button overlay

I'm currently facing an issue with adding text above a GIF on a video play button specifically for mobile devices. The custom play button is necessary because no mobile device supports video autoplay. I've tried various methods to display the tex ...

Struggling with certain CSS design elements

I'm encountering some CSS styling issues. The button in my form remains perfectly positioned when I resize the window, but the inputs are moving around. Why is this happening? Also, when a button is pushed, a h2 element with a little arrow needs to b ...

Changing position with flair in CSS

Is there a way to create a transition effect on an element when it changes position from static to fixed? I attempted using the code: transition: position 2s linear;, however, it does not seem to have any impact. Are there any other methods I could experi ...

How to prevent labels from overlapping textboxes when the labels are lengthy using CSS

I am having an issue with an asp control that renders as a text area. I have applied some CSS to style it, but I am facing a problem where a long label is overlapping the textbox. Is there any CSS I can add to push the textbox down if the label gets too lo ...

What is the best way to have a div created by JavaScript automatically expand to cover the entire screen after clicking on a dynamically generated table cell?

A unique interactive game inspired by Jeopardy is in the works. By clicking on a specific element within the game board, players can reveal questions fetched from the Jeopardy API. These questions are then presented elegantly within a dynamically created d ...

Designing a unique diagonal layout with HTML and CSS

Hey there, I have a question about creating diagonal backgrounds in web design. I've seen websites like Udacity and one my friend is working on that feature diagonal shapes or designs in the background. I'm curious about how to achieve this effec ...

Alter the website link in an HTML file as well as in the corresponding CSS and JavaScript

Is it possible for JQuery to alter URLs within a CSS or Javascript resource before they are loaded into the browser, or even as they load in the browser? URLs typically point to resources such as images and fonts. I have been considering this idea becaus ...

Is there a way for me to view the contents within the box?

Hey there! I have a question about how to access the content inside this mysterious box "" within the style sheet. For instance, I am curious to find out the link of the image: .facebookicon::before { content: ""; } ...

What is the best way to display table headers for each record on mobile devices? Is there a way to create a stacked view

I recently started working with bootstrap, specifically version 3. I am currently trying to find a way to create a layout that resembles a regular table when viewed on desktop but switches to a stacked format on mobile devices. This is the design I am aim ...

SVGs are not resizing correctly in Internet Explorer and there is unnecessary blank space around them

Recently, I made the decision to switch to using SVG symbols for one of my projects. However, I encountered a challenge as I needed these SVGs to be responsive. My main objective was to minimize the number of HTTP requests, leading me to explore the option ...