Adjust the size of flex items to match the size of their container

I'm attempting to design a two-column layout using a flex parent with a height of 100vh. The first column consists of a div containing a heading and a paragraph, while the second column contains a div with an image. I want the flex parent's height to always be 100vh on any screen size, with the image shrinking to fit within the container.

Despite my efforts, the image is overflowing the container and not shrinking as desired. Here is the desired outcome I'm aiming for: https://i.sstatic.net/D4KNw.png https://i.sstatic.net/4udvR.png

Below is the code snippet:


        .section {
          width: 100vw;
          height: 100vh
          background-color: blue;
        }

        .container {
          display: flex;
          width: 100%;
          height: 100%;
          align-items: center;
        }

        .wrapper {
          width: 100%;
          max-height: 100%;
        }

        img {
          width: 100%
          height: 100%;
          object-fit: contain;
        }
      
    

        <section class="section">
        <div class="container">
        <div class="wrapper">
        <h1>Some heading</h1>
        <p>Lorem ipsum dolor sit amet</p>
        </div>
        <div class="wrapper">
        <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1280px-Image_created_with_a_mobile_phone.png"></img>
        <div>
        </div>
        <section>
      
    

Answer №1

To effectively organize layouts, it's essential to incorporate style

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9af8f5f5eee9eee8fbeadaaeb4acb4aa">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />
<section class="fdb-block">
  <div class="container-fluid">

    <div class="row text-left align-items-center pt-5 pb-md-5">
      <div class="col-md-5 m-md-auto order-md-5">
        <img alt="image" class="img-fluid" src="https://i.sstatic.net/6uUBx.png">
      </div>

      <div class="col-md-5">
        <h2><strong>Feature Two</strong></h2>
        <p class="lead">Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar.</p>
        <p><a href="https://www.froala.com">Learn More <i class="fas fa-angle-right"></i></a></p>
      </div>
    </div>

  </div>
</section>

Heading

Answer №2

If my understanding is correct, you are looking to have the header, content section, and image evenly spread when the viewport is at full size. However, when the viewport is resized, you want the image to move to the next line. If this is the case, you can achieve this by following two steps:

  • Add flex-wrap: wrap to your container
  • Remove the width: 100% from the wrapper

Your CSS code will look like this:

.section {
  width: 100vw;
  height: 100vh;
  background-color: blue;
}

.container {
  display: flex;
  width: 100%;
  height: 100%;
  align-items: center;
  flex-wrap: wrap;
}

.wrapper {
  /* width: 100%; */
  max-height: 100%
}

img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

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

What is the best way to set the text color of cell data in a mat-table to white for the entire row when the row is selected as

Is there a way to change the text color when a row is selected in my application? I have already figured out how to change the background color of the row, but how can I target the text within the cells? I'm considering using a similar approach to th ...

Make changes to the HTML file by directly using Jquery or JavaScript

Allow me to elaborate on my current objective. I have an HTML file that requires direct content updates. Specifically, I am working with elements sharing the 'id=test'. My goal is to dynamically update all elements with unique IDs such as ' ...

I aim to display my popup only once per day

Is there a way to display this popup only once per day? <span id='close' onclick='this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;'>x</span> <iframe width="360" height="240" sr ...

What is the relationship between `overflow:hidden` and the inability of `position:sticky` to function properly?

Within the code snippet provided, there is a sticky div positioned inside a container. It adheres to the top of the scrolling panel while remaining within its designated container at all times. This mimics the behavior seen in iOS' UITableView headers ...

Can you explain the significance of the "--" within CSS selectors?

My CSS skills are not the greatest and I stumbled upon a snippet of code that is puzzling to me. .grid-product__title.grid-product__title--body:hover { text-decoration: underline; } I checked MDN and it usually defines a reusable property, like a vari ...

Choose a specific cell in the table and store it in the session

I am new to working with PHP and have never dealt with sessions before. My current goal is to randomly select a table cell and save its location (x,y coordinates) in a new session. Below is the code I have so far, but I am unsure of what steps to take ne ...

utilizing the removeClass method to toggle the visibility of a div by removing the "hidden" class

I am currently developing a dynamic game utilizing HTML, CSS, and jQuery. In the lower right corner of the screen, there is a question mark (which is an image file, not text). My objective is to display a help box when the question mark is clicked and held ...

Could someone please explain the meaning of that in Bootstrap in a clear and detailed manner?

<div class="row"> <div class="col-md-8">.col-md-8</div> <div class="col-6 col-md-4">.col-6 .col-md-4</div> </div> I attempted to utilize bootstrap, yet I am struggling to grasp it ...

Different choices for data attributes in React

Recently, I downloaded a new app that requires multiple API calls. The first API call retrieves 20 Objects, each with its own unique ID. The second API call is made based on the IDs from the first call. To display this data in my component: <div> ...

Creating a CSS key animation for resizing an image: What is the correct way to make an image expand and contract

Check out this CSS key animation example: http://jsfiddle.net/ryurage/Q2ELp/ I'm attempting to make an image grow and occupy most of the window when triggered by user interaction like hovering or click-and-hold. Subsequently, I want the image to shr ...

Experiencing a problem with inline JavaScript onclick

I recently came across a fantastic pure javascript code for a smooth scrolling function, but I'm struggling with integrating it into an inline onclick function. My understanding of javascript is quite limited and I could really use some help... <d ...

Is there a way to connect the YouTube iframe in order to display a pop-up message?

Is it possible to display popup messages when clicking on a YouTube video embedded in an iframe? <a href='login.html?sid=0&keepThis=true&TB_iframe=true&height=240&width=650' class="thickbox" title='test'> ...

"Implement a feature in JavaScript that allows users to click on images to navigate

I have a navigation feature on my website that allows me to cycle through images using the following code: <a href="JavaScript:previousImage()">Previous</a> | <a href="JavaScript:nextImage()">Next</a> <span id='num' ...

What is the best way to eliminate the left padding on a TimelineItem component?

When using the Timeline component to display information, there are three columns: TimelinItem, TimelineSeparator, and TimelineContent. I tried setting the padding of TimelineItem to 0 but it didn't work. The <Trace/> component is a sub-compone ...

What could be causing my hover effect to function exclusively on Chromium-based browsers and not on Firefox?

Could use some help with this code snippet .podmenu { display: flex; flex-direction: column; list-style: none; width: 10rem; margin-left: 3rem; margin-bottom: 60px; } .verze { list-style: none; flex-direction: column; ...

Tips for utilizing the "Sign In with Apple" feature through Apple JS

For implementing "Sign In with Apple" on a web platform using Apple JS, you can refer to the code example available at this link. Now, the query arises: where can I find the Client ID required for this process? I have tried using the app id identifier fro ...

Using JavaScript to Transmit URL

Imagine I have a URL similar to this: http://localhost:8000/intranet/users/view?user_id=8823 All I aim to achieve is to extract the value from the URL using JavaScript, specifically the user_id (which is 8823 in this instance), and transmit it through an ...

utilize jQuery and AngularJS to transform a JSON object into a nested JSON structure

Is there a way to convert my current JSON file into a nested JSON structure like the one below? I've attempted various methods (How to convert form input fields to nested JSON structure using jQuery), but I'm not achieving the desired outcome. Ca ...

Create a duplicate of a div element, including all of its nested elements and associated events, using

Attempting to duplicate a div containing input fields but the event listeners are not functioning. Despite performing a deep copy in the following manner - let rows = document.querySelectorAll('.row'); let dupNode = rows[0].cloneNode(true); sh ...

Still having trouble getting @font-face to work with Firefox and htaccess

I've been struggling to load my custom font in Firefox despite numerous attempts. Here is the @font-face property code I have placed in the head section: @font-face { font-family: 'MeanTimeMedium'; src: url('http://sweetbacklove.com ...