Styling bricks using CSS

For the past two days, I've been attempting to recreate a brick layout similar to the one shown in the image attached below using CSS. However, my efforts have been unsuccessful so far. Please refer to the image for reference.

In order to achieve this layout, I created some code to emulate the first row (the row containing the word "Let's") using the following HTML:

<div class="photo-row first">
    <div class="first-item-1"></div>
    <div class="first-item-2"></div>
    <div class="first-item-3"></div>
    <div class="first-item-4"></div>
    <div class="first-item-5"></div>
</div>

Here is the corresponding CSS:

.photo-row {
    width: 100%;
    height: 200px;
}
.photo-row.first div {
    display: inline-block;
    height: 100%;
}
.first-item-1 {
    width: 13.57%;
    background: red;
}
.first-item-2 {
    width: 19.21%;
    background: green;
}
.first-item-3 {
    width: 31.21%;
    background: orange;
}
.first-item-4 {
    width: 15.14%;
    background: blue;
}
.first-item-5 {
    width: 19.78%;
    background: yellow;
}

The concept behind this code was to assign each div a fixed percentage width of the parent div to create a brick-like structure that adjusts responsively. Each child div was intended to have a background image. Although the layout is functional, it collapses at certain viewports causing the last child div to move to the next line.

I also created a demonstration on CodePen to showcase this issue: https://codepen.io/Ali_Farooq_/pen/oobBYj

I'm puzzled as to why the children divs shift to the second line even though their combined width is less than 100% of the parent div. Additionally, I aim to design the layout without any white space on either side of the child divs. If anyone has a solution involving JavaScript/jQuery, I'm open to trying that approach as well.

Thank you!

Answer №1

Utilizing the display:flex property allows for the creation of a "brick"-like wall layout with greater ease. The use of flex-grow properties also provides more control over container widths compared to using percentages.

For fun, here's an interactive example where you can experiment with adjusting the flex-grow values on different brick types to create a more random pattern. The flexibility is endless.

Furthermore, Flexbox enables better text alignment control through properties like align-items and justify-content applied to the various "bricks".

In the provided demonstration, all bricks have a flex-grow: 2 setting. This uniformity ensures they will flex to the same size, occupying equal space in each row. Pseudo selectors are then used to target specific rows and apply a flex-grow: 1 (or half of two) to the first and last bricks, creating half-sized bricks at each end.

.wall {
  height: auto;
}

.row {
  display: flex;
  border-top: 2px solid white;
}

.row .brick {
  flex-grow: 2;
}

.row .brick:not(:last-child) {
  border-right: 2px solid white;
}

.row:nth-child(even) > .brick:first-child,
.row:nth-child(even) > .brick:last-child {
  flex-grow: 1;
}

.brick {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: red;
  height: 50px;
  color: #fff;
  font-family: Helvetica, sans-serif;
  font-size: 12px;
}
<div class="wall">
  <div class="row">
    <div class="brick"></div>
    <div class="brick">Lets</div>
    <div class="brick"></div>
    <div class="brick">Make</div>
    <div class="brick"></div>
  </div>
  <div class="row">
    <div class="brick"></div>
    <div class="brick">The</div>
    <div class="brick"></div>
    <div class="brick">World</div>
    <div class="brick"></div>
    <div class="brick"></div>
  </div>   
  <div class="row">
    <div class="brick"></div>
    <div class="brick"></div>
    <div class="brick">Suck</div>
    <div class="brick">Less</div>
    <div class="brick"></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

Attempting to send arguments to a jQuery ajax request

After successfully implementing my original ajax call, I decided to create a reusable function for it. This way, I can easily modify the data and URL fields of the ajax call without having to retype everything each time. However, I encountered an issue whe ...

What are the steps to transition to the new Google Identity Service sign-in process?

I am facing a challenge in migrating my app to the latest Google identity service sign-in methods. I have been struggling with this for days and find Google's documentation quite confusing. That's why I'm reaching out here for some assistanc ...

Adjust the cursor resolution to minutes on amCharts while maintaining the axis in days

Setting a custom date format for the dateAxis tooltip is necessary to display the hour, minute, and second. If only one day with different times is set, it displays as expected with the proper time in the tooltip. However, when more than one day is set in ...

Display or conceal the grid template row for content that is empty or if the grid area is not displaying

display: grid; grid-template-columns: var(--side-bar-width) 1fr; grid-template-rows: 60px 1fr 90px; gap: 0px 0px; grid-template-areas: "Sidebar Header" "Sidebar Body" "Player Player"; I ...

I have not made any changes to the <div> tag with my CSS or JavaScript coding

Click here to see the problem I am facing on JSFiddle. I am trying to create a loading screen that will slide up after a few seconds using jQuery. Although I am familiar with HTML, JavaScript, and CSS, I am still new to jQuery. I have a feeling that the so ...

Anticipated a task or method invocation but encountered an expression instead no-unused-expressions (line 14/15)

I can't seem to figure out the issue with my code. Despite trying to fix it multiple times by adjusting lines 14/15, I keep encountering the same error. If anyone has any insights or suggestions on what might be causing this problem, I would greatly a ...

Step-by-Step Guide for Uploading an Entire Folder and Its Contents

I have been working on a code to upload multiple files, but now I am facing the challenge of uploading an entire folder with multiple files and possibly subfolders containing even more files. Currently, I am utilizing JavaScript for obtaining the files and ...

`Can you explain how to specify the elements of an array within a form using AngularJS?`

Below is an array containing objects: //Data source code $scope.data = [ { name: 'Lname', items: [{ label: 'Lastname', type: 'text', model: 'lname', pattern: '/^[a-zA-Z]$/', ...

Opt for a line break over a semicolon when using jQuery

I need assistance with breaking the line wherever a semicolon is present. var locdata = { "locations": [{ "id": 0, "name": 'USA', "cx": 100, "cy": 100, "address":'545, 8t ...

javascript implementation of jquery1.3.2 plugin for creating a marquee

Hi everyone, I'm facing a situation where I need to show horizontally scrolling text moving from left without using the marquee tag. The solution should involve only the jquery1.3.2.min.js plugin. Please assist me with this task. Thank you in advance ...

Automatically submitting the selection when it changes

I am facing an issue with a selection form that is supposed to update the database on change using jQuery, but it seems like nothing is happening. Can anyone provide assistance with this problem? <SELECT name='status' id='status'> ...

How come my picture is clinging to the bottom of the container?

I am facing an issue where my "vertical rule" is sticking to the bottom of the container when placed to the right of the image. <div> <div style="display:inline-block; width:210px;"> <img src="res/img/PlayBtn.png" style="top:- ...

Upon initial server-side rendering load in Nuxt 3, Vercel fails to transmit the client's IP address to the Laravel backend

I'm encountering an issue with my setup where the actual client's IP address is not being forwarded correctly during the initial Server-Side Rendering (SSR) load of a page. Backend: Running Laravel on a VPS Frontend: Utilizing Nuxt 3 deployed on ...

In JavaScript, what is the optimal method for organizing an array of objects into distinct sections?

I have an extensive collection of objects, which resembles the following structure: [ { id: "some_id", timestamp: 12345 }, { id: "some_other_id", timestamp: 12347 }, { id: "some_id", timestamp ...

Stop future date selection in HTML using Vue

Looking for a way to ensure only past dates are selected in an HTML template. The chosen date is then sent to Vue for further processing. Check out the code snippet below. <div class="container text-center justify-content-center container-user&q ...

Switching the default z-index for child elements within an HTML container

According to the specification, elements are typically drawn "in tree order" for in-flow, non-positioned elements of similar block level or float status and identical z-index. This means that elements declared last in the HTML markup appear on top. But wha ...

Before I make a selection, there is only one image displayed. But as soon as I choose that image, it transforms into a

When I select a radio button, should it replace the image with another one? function getAirlinesById(idAirlines) { $.ajax({ dataType: 'json', type: "POST", url: '{{route('get- ...

Nextjs version 13 encountered hydration failure due to discrepancies between the initial UI and the server-rendered content

I am currently utilizing the latest version 13.1.0. I have implemented a ContextProvider that allows switching between light and dark themes. 'use client'; import { Theme, ThemeContext } from '@store/theme'; import { ReactNode, useState ...

Refresh a variety of div elements using the dropbox selection. One dropbox is fully functional, while another is experiencing technical difficulties

Our website is designed to be responsive, meaning the tablet and desktop views are on the same page but with a different layout that involves show/hide div elements. If you find it hard to understand, I have replicated the code on JSFiddle for better visu ...

Assertion error: 1 does not match the expected value of 6

I am faced with a challenging test that requires me to write code that will pass successfully. However, no matter what I try, all I get is the following error messages: "Failed asserting that 1 matches expected 6" when I return 6 and "Too few arguments t ...