Guide to utilizing CSS Flexbox for flexible alignment of containers with various heights and sizes

I'm currently experimenting with CSS flexbox to create a grid layout where an image in one div spans the entire length of the parent div, while four other divs form a 2x2 grid next to it. Here's an example of what I'm trying to achieve: https://i.sstatic.net/00rIE.png

However, I'm encountering difficulties in properly structuring the 2x2 grid next to the first div. I am struggling to make sure that the grid always displays as 2x2 and that each div within it dynamically adjusts its width based on the size of the first div.

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

The code snippet I am currently working with looks like this:

#intro-section {
    padding-top: 16px;
    padding-left: 32px;
    display: inline;
}

.flex-container {
  display: flex;
  flex-wrap: wrap;
  background-color: DodgerBlue;
}

.flex-container > div {
  background-color: #f1f1f1;
  width: 300px;
  height: 100px;
  margin: 10px;
  text-align: center;
  line-height: 75px;
  font-size: 30px;
}

#img-box {
    height: 200px;
    width: 200px;
   background-color: #f1f1f1;
  margin: 32px;
  text-align: center;
  line-height: 75px;
  font-size: 30px;
  display: inline;
}
<div id="intro-section">
  <div class="flex-container">
    <div id="img-box">1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
  </div>
</div>

Can anyone provide guidance on how I can restructure this grid to achieve a layout similar to the reference image I provided?

Answer №1

Perhaps try something similar to this approach?

PLEASE NOTE: This solution utilizes Flexbox exclusively.

* {
  box-sizing: border-box;
}
#main-container {
  display: flex;
  background-color: SkyBlue;
}

.flex-wrapper {
  display: flex;
  flex-wrap: wrap;
  flex-grow: 1;
}

.flex-wrapper > div {
  background-color: #eaeaea;
  width: calc(50% - 20px);
  margin: 10px;
  height: 100px;
  text-align: center;
  line-height: 75px;
  font-size: 30px;
}

#image-container {
 height: 200px;
 width: 200px;
 background-color: #eaeaea;
 margin: 16px;
 text-align: center;
 line-height: 75px;
 font-size: 30px;
}
<div id="main-container"> 
    <div id="image-container">1</div>
    <div class="flex-wrapper">
      <div>2</div>
      <div>3</div>  
      <div>4</div>
      <div>5</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

What is the best way to retrieve a particular div element from a webpage?

I'm having trouble retrieving a specific div element (specifically with the attribute id="vung_doc") from a website. Instead of getting just that element, I seem to be getting almost every element on the page. Any ideas on what could be causing this i ...

Enhancing the tooltip inner content in Bootstrap with a description:

Can you help me troubleshoot the code below? I am trying to add a description to numbers in my tooltip, but it doesn't seem to be working. Here is the HTML: <input id="contract-length" class="slider slider-step-2 slider-contract-length" ...

Problem with holding a dropdown button

Having trouble maintaining the position of this button within the floating button holder above my website content. How can I incorporate a drop-down menu while ensuring it stays within the same button holder? .btn-holder { backgr ...

displaying the information when we mouse over a specific row in the table

I need to display a pop-up with data from a table row, similar to the image at this URL for CS WHOLESALE GROCERS. I've implemented the following AngularJS code in my controller: app.directive('tooltip', function(){ return { res ...

Tips for adjusting the size of your logo for the mobile edition of your WordPress site

I have been attempting to adjust the size of our website's logo ( ) for mobile viewing, but so far no code has proved effective. I've tested various iterations like the one below without success: @media (max-width: 360px) { .site-branding ...

JavaScript formula for calculating dates together

I'm currently developing a PHP program for generating invoices. I'm implementing a datetimepicker to select the invoice generation date and due date. Now, I need the due date field to be automatically populated by adding a specific value to the i ...

Capturing user input with Angular Material forms in HTML

In the process of working on a project in Angular, I am utilizing the Angular Material component library. As part of this project, I am creating a form with multiple fields. Everything is functioning properly, however, the layout appears slightly off: ht ...

Display temporary image if image is not located

I attempted to utilize the onerror attribute to display a placeholder image when the desired image is not found in the folder. The image path is dynamically generated from the backend. The code snippet below shows how I implemented this: <img class=&quo ...

Using Javascript to Treat an HTML Document as a String

Check out the complete project on GitHub: https://github.com/sosophia10/TimeCapsule (refer to js/experience.js) I'm utilizing JQuery to develop "applications" for my website. I'm encountering a problem where I can't locate the correct synta ...

Send the style description to the child component

Is there a way to define a style in a parent component and then pass it to a child component? <style> .teststyle { background-color: red; width: 100px; } </style> I initially thought that if I did not use scoped, the .teststyle ...

Updating information in mysql from html/php can sometimes be a complex process

The issue I am facing involves a "contact" form that is supposed to send data to my database. Everything seems to be working fine with no errors when I access the page from localhost, and it displays the desired result. However, the database (localhost/php ...

Struggling to align nested grids in the center

I'm currently following a mobile-first approach in designing my website. The entire website is contained within one main grid, and I also want to include smaller grids that will serve as links to other HTML pages. I created a sample nested grid but am ...

Is it possible to use ajax to display the combination of multiple strings?

In my code, I have a span containing some text. By default, the <span class="switcher__result"> has a CSS property of display: none. However, when my <button class="switcher switcher__Last collapsible"> changes the text, the result span switche ...

Use jQuery to create a toggle effect with unique attributes and incorporate AJAX functionality

When using this jQuery plugin for creating toggles, I encountered an issue with multiple toggles having the same IDs and classes. This made it difficult to identify a specific toggle to apply auto load ajax when the value changed. I'm wondering how I ...

Mastering the Art of Tab Selection with Jquery

I am trying to implement a tabs control using jQuery. Here is the HTML code I have: <div id="tabs" class="news1"> <ul> <li><a href="#tabs-1">Track</a></li> <li><a href="#tabs-2">History&l ...

The function in PHP does not arbitrarily return a boolean value

After creating a template file for WordPress, I encountered an issue with a function that I wrote. Despite ensuring that the two variables are different (as I confirmed by printing them), the function consistently returns true. Even after testing and attem ...

Mastering the correct application of flex properties within nested flex containers

I'm struggling with utilizing flexbox properly and would like some clarification on how nesting parent and child elements functions. I understand that the child elements inherit the parent's flex properties, but I'm unsure if this inheritan ...

The CSS grid functionality seems to be malfunctioning on a specific page, yet it operates perfectly on other

I'm currently working on my Portfolio application, but I'm facing an issue on the home page where the grid layout is not functioning properly. The different div tags are overlapping each other instead of displaying correctly. https://i.sstatic.n ...

Are Css3 Transition and Transform properties dysfunctional in older versions of IE?

Snippet of HTML Code: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="css/demo.css" /> </head> <body> <div></div> </body> </html> CSS Snippet: div { width: 100p ...

Different methods to disable scrolling when the mobile menu pops up

I have implemented a mobile menu option that appears when you click on the triple line logo, but unfortunately, users can still scroll while the menu is open. I've tried using position:fixed; but haven't been successful in preventing scrolling be ...