Flexbox presenting a specific alignment issue

I'm facing a challenge using Flexbox to achieve the desired result shown below

https://i.sstatic.net/8vJGQ.png

When trying to position my 2 blocks on the right, one of them ends up crossing the line and I struggle to bring it back up.

I want this layout to be managed dynamically in the future, which is why I am avoiding CSS Grid for now.

Could it be that I'm approaching this the wrong way?

 .o-wrapper{
        padding-right: 1.875rem;
        padding-left: 1.875rem;
        margin-right: auto;
        margin-left: auto;
        max-width: 75rem;
    }
    .page__project{
        margin-bottom: 30px;
        position: relative;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: red;
    }

    .page__projects-list{
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
    }

    .page__project-bloc{
        padding: 50px;
        text-align: center;
        position: relative;
        z-index: 10;
    }

    .page__project--small{
          width: 31.0526315789%;
          height: 177px;
    }

    .page__project--large{
          width: 65%;
          height: 177px;
    }

    .page__project--big{
          width: 65%;
          height: 370px;
    }
<div class="o-wrapper page__projects-list">
        <div class="page__project page__project--small">
          <div class="page__project-bloc page__project-bloc--small">
            <h3 role="heading" aria-level="3">VLW #48</h3>
            <p>
              Mise en page du magazine Vivre la Wallonie 48e édition.
            </p>
          </div>
        </div>
        <div class="page__project page__project--large">
          <div class="page__project-bloc page__project-bloc--small">
            <h3 role="heading" aria-level="3">Elit Ligula Etiam</h3>
            <p>
              Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent commodo cursus magna, vel scelerisque
              nisl consectetur et.
            </p>
          </div>
        </div>
        <div class="page__project page__project--big">
          <div class="page__project-bloc page__project-bloc--big">
            <h3 role="heading" aria-level="3">VLW #48</h3>
            <p>
              Mise en page du magazine Vivre la Wallonie 48e édition.
            </p>
          </div>
        </div>
        <div class="page__project page__project--small">
          <div class="page__project-bloc page__project-bloc--small">
            <h3 role="heading" aria-level="3">VLW #48</h3>
            <p>
              Mise en page du magazine Vivre la Wallonie 48e édition.
            </p>
          </div>
        </div>
        <div class="page__project page__project--small">
          <div class="page__project-bloc page__project-bloc--small">
            <h3 role="heading" aria-level="3">VLW #48</h3>
            <p>
              Mise en page du magazine Vivre la Wallonie 48e édition.
            </p>
          </div>
        </div>
      </div>

Answer №1

This example showcases a common grid-based solution. By using display: grid and dividing the layout into a specific grid, you can easily achieve this effect. Learn more about grids on MDN by visiting this link.

display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);

You can define the columns and rows using the above code snippet. Then, assign a grid-area to each div element.

grid-column-gap: 5px;
grid-row-gap: 5px;

This will create space between the grid items. Take a look at the provided example for a visual representation:

.parent {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-column-gap: 5px;
  grid-row-gap: 5px;
}

.div1 {
  grid-area: 1 / 1 / 2 / 2;
}

.div2 {
  grid-area: 1 / 2 / 2 / 4;
}

.div3 {
  grid-area: 2 / 1 / 4 / 3;
}

.div4 {
  grid-area: 2 / 3 / 3 / 4;
}

.div5 {
  grid-area: 3 / 3 / 4 / 4;
}

.box {
  min-height: 100px;
  background-color: red;
}
<div class="parent">
  <div class="box div1">1</div>
  <div class="box div2">2</div>
  <div class="box div3">3</div>
  <div class="box div4">4</div>
  <div class="box div5">5</div>
</div>

Answer №2

If you're considering using flex for this task, here's an option you can experiment with.

Alternatively, I recommend working with grid.

Check out the example on CodePen

I've set up a container with a row and utilized a sub-row to manage the layout of the two smaller boxes. Each image is treated as an item within the rows. Feel free to customize classes, images, etc. to suit your needs.

The goal is to treat those two boxes as individual items.

HTML

<div class="container">
  <div class="row">
    <div class="item short thin">
      <img src="https://source.unsplash.com/collection/190727/600x600">
    </div>
    <div class="item short wide">
      <img src="https://source.unsplash.com/collection/190727/1800x600">
    </div>
    <div class="item large">
      <img src="https://source.unsplash.com/collection/190727/600x600">
    </div>
    <div class="sub-row">
      <div class="item short">
        <img src="https://source.unsplash.com/collection/190727/600x600">
      </div>
      <div class="item short">
        <img src="https://source.unsplash.com/collection/190727/600x600">
      </div>
    </div>
  </div>
</div>

CSS

.container{
    width:767px;
    padding:50px;
}
.row{
    width:100%;
    display:flex;
    flex-direction:row;
    flex-wrap:wrap;
    justify-content:space-between;
}
.sub-row{
    display:flex;
    flex-wrap: wrap;
    flex-direction:column;
    justify-content:space-between;
    width:33%;
    margin-top:10px;
}
.thin{
    width:33%;
    flex: 0 0 auto;
}
.wide{
    width:65%;
    flex: 0 0 auto;
}
.short{
    height:177px;
    flex: 0 0 auto;
}
.large{
    height:370px;
    width:65%;
    margin-top:10px;
}
.row .item img{
    width:100%;
    height:100%;
}

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

When jQuery updates the content, the div content disappears temporarily

I am currently using jQuery to generate HTML by fetching data from a JSON file. Below is the code snippet where I create the HTML: $(document).ready(function () { $('#searchForm').submit(function () { var entry= $("#searchFo ...

Guide to optimizing the display of a responsive iframe across various devices such as mobile and desktop

I have implemented the following CSS in an iframe to make it responsive. It works well on desktop browsers, but on mobile browsers, the iframe only displays half of the screen. Can you please review the code below and provide guidance on how to fix this is ...

Excessive letter spacing in font-face causing display issues on Apple devices such as iPhone and iPad

Recently, I designed a website using some unique fonts that aren't supported on all devices. To ensure compatibility across various platforms, I converted the fonts to all major file formats, including SVG for iPhones and iPads. Surprisingly, this sol ...

Incorrect font display occurring exclusively on Windows Chrome

There seems to be an issue with the Google served Open Sans font on my website, specifically with Win Chrome where the font appears thick and ugly. However, when I tested it on Win FF and Safari, everything seemed fine. The font also displays correctly on ...

What kinds of font file formats are you utilizing?

In numerous projects, I have observed a common practice of using multiple font extensions such as .TTF, .WOFF, and .WOFF2. However, this can result in having multiple files for a single source. Imagine the clutter if one has several sources with different ...

To retrieve the value of a specific row within a table and remove that row

I have utilized handlebars to display a table with data, each row containing an edit button. I am unsure how to retrieve the specific values from the row when the edit button in clicked. Here is the HTML code: <table class="table table-bordered"& ...

Refresh the data in the DataTables table using a fragment

Currently, I am attempting to reload a fragment using ajax. However, after the reload, my event select and row count do not function properly. It seems that the default configuration is not behaving as expected: @PostMapping("/admin/add") ...

Is it better to have a heading titled "Navigation" above a <nav> element, or should it be included inside the navigation element itself?

Which is the better way to structure navigation: <h1>Navigation</h1> <nav> <ul> <li>...</li> </ul> </nav> Or: <nav> <h1>Navigation</h1> <ul> <li>...</li> ...

Customizable page layout with fixed width that adjusts based on content

I am looking to enhance the design of my website by updating its template. I want the content to be displayed within a fixed width centered div. While there are plenty of examples on the web for this, I want to ensure that existing text and tables in my ...

Angular encountering issues with loading external JavaScript files due to the error: ENOENT - indicating that the specified file or directory does

I am attempting to incorporate a bootstrap template into Angular. The template requires some external JavaScript and CSS files that need to be linked. I have placed these files in the assets folder and referenced them in the styles and scripts arrays of an ...

Can you provide a succinct explanation of what constitutes a well-defined "landing page"?

Could someone provide a clear and concise explanation of what exactly constitutes a landing page and how it should be utilized? I'm struggling to grasp the concept. Where is the optimal placement for a landing page on a website? Is it best placed o ...

Extract a section of the table

I'm looking to copy an HTML table to the clipboard, but I only want to include the rows and not the header row. Here is the structure of the table: <table style="width:100%" #table> <tr> <th class="border"></th> ...

A step-by-step guide to displaying a label component upon clicking an AjaxLink

How can I display data from a JSON file when an AjaxLink is clicked? The code I have implemented is not working, so I would appreciate any corrections or suggestions. Additionally, I am wondering if it's possible to add a label inside AjaxLink. Thank ...

Combining various functions into a single button

I am currently working on creating a full-screen menu that functions like a modal. Everything seems to be working fine, except for the fadeOut animation. Can someone please help me understand what is causing issues with my scripts/codes? I want the content ...

Problem Alert: Click Event Not Functioning on Generated Links

Take a look at these two code snippets. Despite other jQuery functions in the same JS file working fine on the UL element, nothing seems to be happening with these. Is there something obvious that I am missing? <ul id="activityPaganation" class="paga ...

Printing Strings in JavaScript IF Conditional Block

Hello there! I am looking for assistance with a JavaScript concept. Recently, I created some code where in the HTML file, there is a div element with an id of "GetID" where the string outputs are meant to be displayed. The JavaScript code looks something ...

Use .load() to set an image as background in the div

There is a block of code that is supposed to display images in a div with the class "img", but it isn't functioning correctly. Can you provide some guidance on how to fix this issue? <div class="other"><a href="/index1.html">Link1</a&g ...

Navbar links in Bootstrap unexpectedly expanding in width

My website has a navigation menu that looks like this: https://i.stack.imgur.com/1R8mv.png However, when I click on a menu item, the link container inherits the width of the dropdown menu. Is there a way to prevent this using purely CSS? I am currently ...

Enhancing text visibility in dompdf by making it bold or increasing its size

Recently, I began using dompdf v0.6.0beta3 along with the dompdf Codeigniter helper in Codeigniter. To address the issue of missing fonts, I manually moved the Arial font family tff files from the Windows 7 font folder to the /lib/fonts directory within do ...

Effortless 'rotational' script

I am currently developing a HTML5 Canvas game with the help of JavaScript. My aim is to create an object that smoothly transitions to a specific direction. The direction is being stored as a variable and calculated in radians. Here's how the code op ...