Tips for designing a unique mosaic using flex technology

I am attempting to design a unique mosaic layout using flexbox that resembles the image provided:

https://i.sstatic.net/jrHvb.jpg

Each box should have a width equivalent to one-third of the parent container, except for box 4 which needs to be double the size in height and width.

I have made progress with the following code snippet:

#push-group-element ul,
#push-group-element ul li{
  list-style-type: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex: 1 0 33.33%;
  justify-content: center;
}

.mosaic-container {
  display: flex;
  width: 100%;
  height: 100%;
  overflow: hidden;
  list-style-type: none;
  background: red;
  flex-wrap: wrap;
}
.mosaic-item {
  position: relative;
  padding: 10px;
  background: green;
  outline: 1px solid black;
}
.mosaic-item {
  height: 150px;
}
#push-group-element .mosaic-item:nth-child(4) {
  flex: 1 0 66.6%;
  height: 300px;
}
<div id="push-group-element" style="width:600px">
  <ul class="mosaic-container">
    <li class="mosaic-item item-small" data-item="1">1</li>
    <li class="mosaic-item item-small" data-item="2">2</li>
    <li class="mosaic-item item-small" data-item="3">3</li>
    <li class="mosaic-item item-big" data-item="4">4</li>
    <li class="mosaic-item item-small" data-item="5">5</li>
    <li class="mosaic-item item-small" data-item="6">6</li>
    <li class="mosaic-item item-small" data-item="7">7</li>
    <li class="mosaic-item item-small" data-item="8">8</li>
    <li class="mosaic-item item-small" data-item="8">9</li>
  </ul>
</div>

The issue I'm facing is that box number 6 does not wrap properly and ends up on a new line, resulting in an unwanted row with box 9 and an empty space where box 6 should be located.

In summary: How can I ensure that box 6 wraps below number 5 while maintaining boxes 7, 8, and 9 in the same row as per the attached sketch. Thank you in advance.

Answer №1

To achieve the desired layout, include a span element as shown below:

#push-group-element ul,
#push-group-element ul li {
  list-style-type: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex: 1 0 33.33%;
  justify-content: center;
}

.mosaic-container {
  display: flex;
  width: 100%;
  height: 100%;
  overflow: hidden;
  list-style-type: none;
  background: red;
  flex-wrap: wrap;
}

.mosaic-item {
  position: relative;
  padding: 10px;
  background: green;
  outline: 1px solid black;
}

.mosaic-item {
  height: 150px;
}

#push-group-element .mosaic-item:nth-child(4) {
  flex: 1 0 66.6%;
  height: 300px;
}
<div id="push-group-element" style="width:600px">
  <ul class="mosaic-container">
    <li class="mosaic-item item-small" data-item="1">1</li>
    <li class="mosaic-item item-small" data-item="2">2</li>
    <li class="mosaic-item item-small" data-item="3">3</li>
    <li class="mosaic-item item-big" data-item="4">4</li>
    <span style="width: 33.3%;">
      <li class="mosaic-item item-small" data-item="5">5</li>
      <li class="mosaic-item item-small" data-item="6">6</li>
    </span>
    <li class="mosaic-item item-small" data-item="7">7</li>
    <li class="mosaic-item item-small" data-item="8">8</li>
    <li class="mosaic-item item-small" data-item="8">9</li>
  </ul>
</div>

OR Achieve a similar result using the float property instead of flex (no need to change HTML structure).

#push-group-element ul,
#push-group-element ul li {
  list-style-type: none;
  margin: 0;
  padding: 0;
  justify-content: center;
  width: 33.3%;
  float: left;
}

#push-group-element ul {
  width: 100%;
}

.mosaic-container {
  width: 100%;
  height: 100%;
  overflow: hidden;
  list-style-type: none;
  background: red;
}

.mosaic-item {
  position: relative;
  padding: 10px;
  background: green;
  outline: 1px solid black;
}

.mosaic-item {
  height: 150px;
}

#push-group-element .mosaic-item:nth-child(4) {
  width: 66.6%;
  height: 300px;
}
<div id="push-group-element" style="width:600px">
  <ul class="mosaic-container">
    <li class="mosaic-item item-small" data-item="1">1</li>
    <li class="mosaic-item item-small" data-item="2">2</li>
    <li class="mosaic-item item-small" data-item="3">3</li>
    <li class="mosaic-item item-big" data-item="4">4</li>
    <li class="mosaic-item item-small" data-item="5">5</li>
    <li class="mosaic-item item-small" data-item="6">6</li>
    <li class="mosaic-item item-small" data-item="7">7</li>
    <li class="mosaic-item item-small" data-item="8">8</li>
    <li class="mosaic-item item-small" data-item="8">9</li>
  </ul>
</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

The issue of AngularJS inline style color not functioning in Internet Explorer 11 has been identified

Hello, I am currently attempting to dynamically change the color of a p tag. However, I am facing issues with IE-11 not supporting this functionality. How can I resolve this problem? <p style="color:{{userData.color}} !important;">{{userData.someTex ...

The functionality of the onclick button input is not functioning properly in the Google

Here is some JavaScript code: function doClick() { alert("clicked"); } Now, take a look at this HTML code: <div > <table border="2" cellspacing="0" cellpadding="0" class="TextFG" style="font-family:Arial; font-size:10pt;"> <tr> <t ...

Creating an online bidding platform using ASP.Net, MVC 5, and Entity Framework

Having recently delved into the world of ASP.NET, I am in the process of developing a basic auction site for a charitable cause. Utilizing MVC 5 and Entity Framework with Code-First approach for database management. The core of this project is the Item mo ...

Adding plain HTML using jQuery can be done using the `.after()` and `.before()` methods

I have encountered a situation where I need to insert closing tags before an HTML element and then reopen it with the proper tags. The code snippet I am using is as follows: tag.before("</div></div>"); and then re-open it by adding proper tag ...

What is the best way to center align tabs in a Bootstrap 4 list?

I am working with tabs structured like this: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist"> <li class="nav-item"> ...

Stop the endless scrolling of the carousel and keep it fixed in place

I'm having trouble with disabling auto scrolling even though I've set bs-interval to "false". Here's my code snippet: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfem ...

Select your vehicle from the drop-down menu

Seeking assistance with a Javascript drop-down selector for vehicle make, model, and year. The goal is to dynamically generate a link based on the user's selections. I'm struggling with implementing the year selection without replacing the model. ...

Conceal a different div unless it includes

Hi everyone, I need help with a filter code snippet: $(".title").not(":contains('" + $("[name=filter]").val() + "')").hide() The issue I'm facing is that the .title class is nested within the .sortAll class along with many other divs. I w ...

Creating this design using only HTML and CSS is possible by following these steps

I attempted to create this design using only HTML and CSS but unfortunately couldn't achieve it. Can anyone provide me with some assistance? Thank you in advance Check out the image on imgur.com ...

Enhance the appearance of the <td> <span> element by incorporating a transition effect when modifying the text

I need help with creating a transition effect for a span element within a table cell. Currently, when a user clicks on the text, it changes from truncated to full size abruptly. I want to achieve a smooth growing/scaling effect instead. You can view an exa ...

HTML Tooltip with Bootstrap 4

After creating a website using Bootstrap 4 and jQuery, I encountered an issue with HTML-styled tooltips. Instead of displaying the tooltips with the correct styling, they appear to be populated using the jQuery text() function, showing the HTML as plain te ...

PersistJS callback function is malfunctioning

I stumbled upon a great library for managing client storage. You can find the latest version here: https://github.com/jeremydurham/persist-js However, I encountered an issue with the callback function. var result = store.get('saved_data', func ...

Invoking a function means calling another one simultaneously

There are two buttons in my code: The button on the right triggers a function called update(): <script> function update(buttonid){ document.getElementById(buttonid).disabled = true; event.stopPropagation(); var textboxid = buttonid.sli ...

Navigate files on a Windows server remotely using a Linux Apache web server by clicking on a browser link

After setting up an apache webserver on Linux Debian and successfully creating an intranet, I encountered a challenge. The intranet is designed to display tables with data from sql queries using php and mysql. However, I wanted to include a hyperlink withi ...

Animating Font Awesome content through CSS transitions

I am looking to animate a font awesome pseudo element on my website. Below is the HTML code I am working with: <li class="has-submenu"> <a onclick="$(this).toggleClass('open').closest('.has-submenu').find('.submenu&a ...

In HTML, the size and position of an <a> element cannot be altered or adjusted

I am having trouble with an anchor element that is contained within a div. No matter what I try, I cannot resize or move it up or down without it covering its sibling element. I have attempted using transform-translate and margin properties, but to no avai ...

Display or conceal columns based on their index

<div ng-controller="checkBoxController"> <div id="myModal" class="modal fade" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="moda ...

Tips on determining the type of DOM element for CSS Selector adjustment

In this case, $self is not returning the expected result and I am getting "undefined". To adjust the CSS selector to correctly target the desired element, I need to determine which element type I am dealing with. Is it the "ul", "li", or "a" element? Whil ...

"Enhanced Bootstrap 4 table featuring a single column that stands out in size compared

I have set up a Bootstrap4 table with multiple columns, but I need one of them to be larger in order to accommodate more text. I want to specify the minimum width for this larger column and let Bootstrap4 adjust the sizes of the other columns accordingly. ...

jQuery's show/hide functionality allows for the dynamic resizing of images,

I am experiencing an issue with a Joomla template that has a custom jQuery menu. When I hover over the map with my mouse, the overlay appears slightly larger than expected. This problem seems to be occurring in Firefox and IE 11, leading me to believe it ...