I am trying to create 4 responsive divs for homepage; tried to decreae padding and margin of div not working.divs should be responsive on mobile devices they shpuld move below each one ; two divs appear on tabs; 4 divs on desktop and one div on mobile
i have tried following HTML:
<div class="flex-container">
<div class="flex-item">
<div class="flex-item-inner">
<div class="homepage-div homepage-div-shadow">
<img class="homepage-div-icon"/>
<h2>Heading</h2>
<h6>Sub-Heading</h6>
<p>This is responsive paragraph. Text would not move out of div as below .</p>
<button class="bttn">Practice Now</button>
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="homepage-div homepage-div-shadow">
<img class="homepage-div-icon"/>
<h2>Heading</h2>
<h6>Sub-Heading</h6>
<p>This is responsive paragraph. Text would not move out of div as below .</p>
<button class="bttn">Practice Now</button>
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="homepage-div homepage-div-shadow">
<img class="homepage-div-icon"/>
<h2>Heading</h2>
<h6>Sub-Heading</h6>
<p>This is responsive paragraph. Text would not move out of div as below .</p>
<button class="bttn">Practice Now</button>
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="homepage-div homepage-div-shadow">
<img class="homepage-div-icon"/>
<h2>Heading</h2>
<h6>Sub-Heading</h6>
<p>This is responsive paragraph. Text would not move out of div as below .</p>
<button class="bttn">Practice Now</button>
</div>
</div>
</div>
</div>
here is css divs are not showing properly on tab and mobile devices and i want to decreae the padding and margin:
.homepage-div-shadow
{
padding: 20px;
box-shadow: 2px 2px 8px 1px #cccccc;
border: solid 1px #cccccc;
border-radius: 2px;
}
.homepage-div-icon{
background-image: url("image.png");
width: 100px;
height: 100px;
}
.homepage-div a
{
text-decoration: none;
color: white;
font-size: 16px;
}
.homepage-div h2{
font-size: 28px;
opacity: 0.90;
font-weight: 600;
margin: 5%;
margin-bottom: 6px;
}
.homepage-div h6{
font-size: 17px;
padding: 0px;
margin: 0px;
margin-bottom: 20px;
opacity: 0.6;
}
#media(min-width: 769px) {
.flex-container {
display: flex;
flex-wrap: wrap;
}
.flex-container .flex-item {
flex: 1 0 50%;
}
}
#media (min-width: 1024px) {
.flex-container .flex-item {
flex: 1 0 25%;
}
}
.flex-item-inner {
padding: 25px;
margin: 20px;
background-color: white;
}
Here is the demo link: testfellow
Hope the solution below helps. I have just tried to mimic the scenario where there are a certain number of div s. And as per your question and requirement, have:
1. Followed mobile-first approach.
2. For mobile devices, each div should come in separate lines.
3. For tabs (at your breakpoint: 769px and above), two divs should come in a line.
4. For desktops (at your breakpoint: 1024px and above), four divs should come in a line.
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
background-color: #2980b9;
}
.flex {
max-width: 80%;
}
.flex-item {
background-color: #ffffff;
padding: 5em;
border: 1px solid black;
margin: 2% auto;
width: 25%;
}
#media(min-width: 769px) {
.flex {
display: flex;
flex-wrap: wrap;
}
.flex-item {
width: 50%;
}
}
#media(min-width: 1024px) {
.flex {
display: flex;
flex-wrap: wrap;
}
.flex-item {
width: 25%;
}
}
<div class="flex">
<div class="flex-item"></div>
<div class="flex-item "></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item "></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
</div>
Related
I need three columns of image layout with image text like this picture
its responsive, like below picture no border no background color, image text background color like this required, I'm new to css/html
I need three columns of image layout with image text like this picture
its responsive, like below picture no border no background color, image text background color like this required, I'm new to css/html
* {
box-sizing: border-box;
}
/* Create three equal columns that floats next to each other */
.column {
float: left;
width: 33.33%;
padding: 10px;
height: 300px; /* Should be removed. Only for demonstration */
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
.figure {
border: thin #c0c0c0 solid;
display: flex;
flex-flow: column;
padding: 5px;
max-width: 220px;
margin: auto;
position: relative;
}
.imgdiv {
max-width: 220px;
max-height: 150px;
}
.figcaption {
background-color: pink;
color: #fff;
font: italic smaller sans-serif;
padding: 3px;
text-align: center;
}
/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
#media screen and (max-width: 700px) {
.column {
width: 100%;
}
}
<h2>Responsive Three Column Layout</h2>
<p>Resize the browser window to see the responsive effect (the columns will stack on top of each other instead of floating next to each other, when the screen is less than 600px wide).</p>
<div class="row">
<div class="column" style="">
<figure>
<img class="column" src="image1.jpg" style="width:100%;height:100%;">
<figcaption>Register here</figcaption>
</figure>
</div>
<div class="column" style="background-color:#bbb;">
<h3>image 2</h3>
<p>Info 2</p>
</div>
<div class="column" style="background-color:#ccc;">
<h3>Publications</h3>
<p>Info 3</p>
</div>
</div>
Use flex and flex-direction
.row {
display: flex;
flex-direction: row;
justify-content: center;
align-items: flex-end;
}
.column {
padding: 10px;
box-sizing: border-box;
}
img {
max-width: 100%;
}
.caption {
background-color: purple;
color: white;
/* padding: 0.1rem; */
}
.caption h3,
.caption p {
padding: 0.5rem;
margin: 0;
}
/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
#media screen and (max-width: 700px) {
.row {
flex-direction: column;
}
}
<h2>Responsive Three Column Layout</h2>
<p>Resize the browser window to see the responsive effect (the columns will stack on top of each other instead of floating next to each other, when the screen is less than 600px wide).</p>
<div class="row">
<div class="column">
<div class="image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/arctichare.png" alt="arctichare">
</div>
<div class="caption">
<h3>image 1</h3>
<p>Info 1</p>
</div>
</div>
<div class="column">
<div class="image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" alt="airplane">
</div>
<div class="caption">
<h3>image 2</h3>
<p>Info 2</p>
</div>
</div>
<div class="column">
<div class="image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/baboon.png" alt="baboon">
</div>
<div class="caption">
<h3>Publications</h3>
<p>Info 3</p>
</div>
</div>
I'm working to build the following layout as seen in the image below... The goal is the image is on the left with a fixed width. The text on the right of the image is aligned accordingly and takes up the available width.
Lastly, the goal is that these items stack in a grid.
I'm having problems getting the items to align properly, here's what I have:
.feature-items {
background: #CCC;
box-sizing: border-box;
width: 100%;
padding: 0px;
}
.feature-item--wrapper {
background: #efefef;
display: inline-block;
flex-direction: row;
box-sizing: border-box;
width: 336px;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
.img-wrapper {
box-sizing: border-box;
width: 88px;
padding-right: 24px;
}
.img-wrapper,
img {
width: 64px;
height: 64px;
}
.text {
box-sizing: border-box;
padding-top: 24px;
padding-bottom: 24px;
flex: 1 1 auto;
}
<div class="feature-items">
<div class="feature-item--wrapper">
<div class="img-wrapper">
<img src="http://clipground.com/images/minecraft-server-clipart-64x64-9.png" width="64">
</div>
<div class="text">
<strong class="feature-title">Title</strong>
<p class="feature-description">This is a sentence description, hello world.</p
</div>
</div>
<div class="feature-item--wrapper">
<div class="img-wrapper">
<img src="http://clipground.com/images/minecraft-server-clipart-64x64-9.png" width="64">
</div>
<div class="text">
<strong class="feature-title">Title</strong>
<p class="feature-description">This is a sentence description, hello world.</p
</div>
</div>
<div class="feature-item--wrapper">
<div class="img-wrapper">
<img src="http://clipground.com/images/minecraft-server-clipart-64x64-9.png" width="64">
</div>
<div class="text">
<strong class="feature-title">Title</strong>
<p class="feature-description">This is a sentence description, hello world.</p
</div>
</div>
<div class="feature-item--wrapper">
<div class="img-wrapper">
<img src="http://clipground.com/images/minecraft-server-clipart-64x64-9.png" width="64">
</div>
<div class="text">
<strong class="feature-title">Title</strong>
<p class="feature-description">This is a sentence description, hello world.</p
</div>
</div>
</div>
Any help would be appreciate as to what I'm doing wrong? Thanks
to .feature-items and .feature-item--wrapper add display flex
.feature-items {
background: #CCC;
box-sizing: border-box;
width: 100%;
padding: 0px;
display: flex;
flex-wrap: wrap;
}
.feature-item--wrapper {
background: #efefef;
flex-direction: row;
box-sizing: border-box;
width: 50%;
align-items: center;
justify-content: center;
display: flex;
}
.img-wrapper {
box-sizing: border-box;
width: 88px;
padding-right: 24px;
}
.img-wrapper,
img {
width: 64px;
height: 64px;
}
.text {
box-sizing: border-box;
padding-top: 24px;
padding-bottom: 24px;
flex: 1 1 auto;
}
<div class="feature-items">
<div class="feature-item--wrapper">
<div class="img-wrapper">
<img src="http://clipground.com/images/minecraft-server-clipart-64x64-9.png" width="64">
</div>
<div class="text">
<strong class="feature-title">Title</strong>
<p class="feature-description">This is a sentence description, hello world.</p>
</div>
</div>
<div class="feature-item--wrapper">
<div class="img-wrapper">
<img src="http://clipground.com/images/minecraft-server-clipart-64x64-9.png" width="64">
</div>
<div class="text">
<strong class="feature-title">Title</strong>
<p class="feature-description">This is a sentence description, hello world.</p>
</div>
</div>
<div class="feature-item--wrapper">
<div class="img-wrapper">
<img src="http://clipground.com/images/minecraft-server-clipart-64x64-9.png" width="64">
</div>
<div class="text">
<strong class="feature-title">Title</strong>
<p class="feature-description">This is a sentence description, hello world.</p>
</div>
</div>
<div class="feature-item--wrapper">
<div class="img-wrapper">
<img src="http://clipground.com/images/minecraft-server-clipart-64x64-9.png" width="64">
</div>
<div class="text">
<strong class="feature-title">Title</strong>
<p class="feature-description">This is a sentence description, hello world.</p>
</div>
</div>
</div>
These styles worked for me (with slight modifications).
And In HTML part check with the closing 'p' tag it is not closed properly
.feature-items {
background: #CCC;
box-sizing: border-box;
width: 100%;
padding: 0px;
}
.feature-item--wrapper {
background: #efefef;
display: inline-block;
flex-direction: row;
box-sizing: border-box;
width: auto;/* changed */
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
.img-wrapper {
box-sizing: border-box;
width: 88px;
padding-right: 24px;
display: inline-block;/* added*/
}
.img-wrapper,img {width: 64px;height: 64px;}
.text {
box-sizing: border-box;
padding-top: 24px;
padding-bottom: 24px;
flex: 1 1 auto;
display: inline-block;/* added*/
}
try this. You have to set flex to the main container, not just for the childrens
.feature-items {
background: #CCC;
display: flex; /* changed */
flex-flow: wrap;
width: 100%;
padding: 0px;
}
.feature-item--wrapper {
background: #efefef;
display: flex; /* added */
flex-direction: row;
box-sizing: border-box;
width: 336px;
flex-wrap: nowrap;
align-items: center;
justify-content: flex-start;
}
.img-wrapper {
box-sizing: border-box;
width: 88px;
padding-right: 24px;
}
.img-wrapper,
img {
width: 64px;
height: 64px;
}
.text {
box-sizing: border-box;
padding-top: 24px;
padding-bottom: 24px;
flex: 1 1 auto;
}
IMO Flexbox isn't really the best tool for something like this as it was originally designed for aligning items in one direction, horizontal or vertical. It makes much more sense to build a 4 x 4 grid using CSS Grid then use Flexbox to position the items horizontally inside your "feature-item-wrapper" div. See below:
body {
width: 50%; /* reduces available width so that */
margin: 0 auto; /* items take up two rows. margin centers everthing */
}
.feature-items {
display: grid;
grid-template-columns: repeat(auto-fill,minmax(300px, 1fr)); /* minimum and max column width */
grid-gap: 20px; /* seperates each grid item */
}
.feature-item--wrapper {
background: #efefef;
display: flex;
width: 100%;
}
.img-wrapper img {
padding-top: 20px;
width: 64px;
height: 64px;
}
.text {
text-align: left;
padding: 20px;
}
The top answer given, which you seem to have marked as correct, doesn't successfully stack the grid items on top of each other when on smaller devices. To make them stack I set a minimum width for the columns which forces the stack when the window reaches the set width. Also be mindful that CSS Grid isn't supported by legacy browsers.
Hope this helps :)
I'm building my own Pill component using HTML:
.panel1 {
width: 100%;
}
.panel2 {
width: 100px;
}
.pill {
display: inline-block;
}
.pill-content {
display: flex;
flex-direction: row;
font-size: 12px;
vertical-align: middle;
padding: 0px;
background-color: white;
border: solid;
border-width: 1px;
border-radius: 4px;
color: black;
border-color: blue;
background-color: white;
}
.pill-text {
flex: 1;
}
.pill-icon {
flex: 1;
max-width: 18px;
padding: 0px 5px 0px 5px;
}
<div class="panel1">
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Why is that text breaking as it fits in width?
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
</div>
<div class="panel2">
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Test
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Very big text that does not fit in width
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
<div>
</div>
My problems:
I need to position text always on the left side of the Pill.
I need to keep the close icon (I´m using an X here but that will be a font awesome icon) always vertically centered.
I can´t see why my text is breaking on panel1. I expected the text to break only when the panel size is smaller that text + icon.
Can I make that HTML/CSS simpler?
JSFiddle here
I've made some modifications to your code to give you the desired result:
/* recommended */
.panel1 *,
.panel2 * {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.panel1 {
/*width: 100%; by default*/
}
.panel2 {
width: 100px;
}
.pill {
display: inline-flex; /* modified, since you're using flexbox */
}
.pill-content {
display: flex;
/*flex-direction: row; by default*/
font-size: 12px;
/*vertical-align: middle; has no effect here*/
align-items: center; /* now its vertically centered */
/*padding: 0; already covered*/
border: 1px solid blue; /* shorthand */
border-radius: 4px;
}
.pill-text {
/*flex: 1; not necessary, the culprit for breaking*/
}
.pill-icon {
/*flex: 1; not necessary since you're using max-width*/
max-width: 18px;
padding: 0 5px;
text-align: center; /* for horizontal alignment */
}
<div class="panel1">
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Why is that text breaking as it fits in width?
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
</div>
<div class="panel2">
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Test
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Very big text that does not fit in width
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
</div>
Here are some simplification tips (as I cannot comment):
For all values that are 0, you can drop the units (px, etc.)
You can shorten your code by using the border shorthand: border: 1px solid blue. Luckily there's no need to specify each attribute individually
background-color: white; is specified twice for .pill-content, unless I'm missing something.
I have a pay layout which works how I want to when using the .slides element has a background colour and height, it flexes as expected, however when I switch to filling it with an image this behaviour breaks as the images won't resize and I can't get my head around how to make them resize.
The idea is the left column of text remains fixed size, and the right column flexes up and down, eventually snapping under the left column at small sizes.
The reason for using an <img> and not a background image is because there is a image slide show that I want to put in here.
Can anyone help fix this issue?
body {
margin: 1em;
}
p {
margin: 0;
}
.container {
display: flex;
margin-top: 1em;
width: 100%;
flex-wrap: wrap;
}
.text {
flex: 0 0 auto;
width: 15em;
margin-right: 1em;
}
.images {
flex: 1 1 auto;
min-width: 15em;
max-width: 800px;
}
.caption {
margin-top: .25em;
}
<div class="header">
Title
</div>
<div class="container">
<div class="text">
<p>Something about this project is really interesting.</p>
</div>
<div class="images">
<div class="slides">
<img src="https://via.placeholder.com/800x800">
</div>
<div class="caption">
<p>Text about this project</p>
</div>
</div>
</div>
Changes made:
Enabled responsiveness for img elements
Commented out the flex-wrap: wrap
Set #media queries to define when the wrapping inside the .container div takes place
body {
margin: 1em;
}
p {
margin: 0;
}
img {
display: block; /* removes bottom margin/whitespace */
max-width: 100%; /* horizontally responsive */
max-height: 100vh; /* vertically responsive */
}
.container {
display: flex;
margin-top: 1em;
width: 100%;
/*flex-wrap: wrap;*/
}
.text {
flex: 0 0 auto;
width: 15em;
margin-right: 1em;
}
.images {
flex: 1 1 auto;
min-width: 15em;
max-width: 800px;
}
.caption {
margin-top: .25em;
}
#media (max-width: 33em) { /* 2 x 15em (.text & .images) + 2em (left & right margin of the body element) + 1em (.text margin-right) */
.container {flex-wrap: wrap}
}
<div class="header">
Title
</div>
<div class="container">
<div class="text">
<p>Something about this project is really interesting.</p>
</div>
<div class="images">
<div class="slides">
<img src="https://via.placeholder.com/800x800">
</div>
<div class="caption">
<p>Text about this project</p>
</div>
</div>
</div>
Do you mean something like this ?
body {
margin: 1em;
}
p {
margin: 0;
}
.container {
display: flex;
margin-top: 1em;
width: 100%;
flex-flow:row wrap;
}
.text {
flex: 0 0 auto;
width: 15em;
margin-right: 1em;
}
.images {
flex:1 0 15em;
min-width: 15em;
max-width:800px;
}
.slides {
display:flex;
}
.caption {
margin-top: .25em;
}
<div class="header">
Title
</div>
<div class="container">
<div class="text">
<p>Something about this project is really interesting.</p>
</div>
<div class="images">
<div class="slides">
<img src="https://via.placeholder.com/800x800" />
</div>
<div class="caption">
<p>Text about this project</p>
</div>
</div>
</div>
I have a page that is 50/50 wide. The left half has a row with six divs.
Criteria:
6 squares must always remain square.
First 5 squares should have margin/padding to right for separation.
All six squares must stay on same single row. If I can get that to work i can make the needed adjustments for responsiveness in smaller viewports.
Cross browser compatible for newest version of ie, chrome, and firefox.
My codepen: https://codepen.io/johnsontroye/pen/zzNVBr
Image:
<body>
<div class="container">
<div class="column" style="margin-right: 20px">
<div class="flex-container">
<div class="flex-item">
<div class="flex-item-inner">
<div class="flex-item-inner-content">
L1
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="flex-item-inner-content">
L2
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="flex-item-inner-content">
L3
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="flex-item-inner-content">
L4
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="flex-item-inner-content">
L5
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="flex-item-inner-content">
L6
</div>
</div>
</div>
</div>
</div>
<div class="column" style="margin-left: 20px; border: 1px black solid; height: 500px">
Other stuff
<div>
</body>
.container {
display: flex;
flex-direction: row;
padding: 25px;
border: 2px red solid;
}
.column {
width: 100%;
height: 100%;
float: left;
}
.flex-container {
padding: 0;
font-size: 0;
border: 1px solid black;
box-sizing: border-box;
}
.flex-item {
position: relative;
display: inline-block;
height: 0;
width: 100%;
padding-top: 100%;
border: 1px black solid;
font-size: 20px;
color: black;
font-weight: bold;
text-align: center;
box-sizing: border-box;
}
#media (min-width: 480px) {
.flex-item {
width: 33.3333%;
padding-top: 33.3333%;
}
}
#media (min-width: 768px) {
.flex-item {
width: 16.6666%;
padding-top: 16.6666%;
}
}
.flex-item-inner {
position: absolute;
display: flex;
justify-content: center;
align-items: center;
top: 0;
bottom: 0;
right: 0;
left: 0;
margin-right: 25px;
background: white;
border: 1px solid red;
box-sizing: border-box;
}
.flex-item-inner-content {
border: 1px solid orange;
}
.flex-item:last-child .flex-item-inner {
margin-right: 0;
color: green;
}
The main trick here is to make the div a square.
Normally one set a width, the height to 0 and a padding that equals to the width
.square {
height: 0;
width: 33%;
padding-bottom: 33%;
background: lightgray;
}
<div class="square">
<div>
Content
</div>
</div>
Now, when we add display: flex, we can't use padding with percent (Firefox bug) and we can't use height with percent since we used height: 0.
To overcome these issues when can use viewport units vw instead, and with that we can also use height instead of padding to keep it squared.
So instead of setting a width like this, calc((100% / 6) - 10px);, to spread 6 items equally with a gutter about 10px wide, we use viewport units like this calc(( (50vw - 65px) / 6) - 10px);
The 50vw is half the browser width, the 65px is the sum of the container's left/right padding, 50px, plus the 15px gutter between the columns.
This also allows us to skip the extra flex-item-inner element, skip using position: absolute on the content element, and, as we didn't use percent for the height on the flex-item, we can do like this to center the content
.flex-item-content {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
And the end result is this
Fiddle demo
Stack snippet
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
padding: 25px;
border: 2px red solid;
}
.column {
flex-basis: calc(50% - 15px);
}
.flex-container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.flex-item {
position: relative;
flex-basis: calc(( (50vw - 65px) / 6) - 10px);
height: calc(( (50vw - 65px) / 6) - 10px);
background: white;
border: 1px solid red;
overflow: hidden;
}
.flex-item-content {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.flex-item:last-child .flex-item-content {
color: green;
}
.column .other {
padding: 15px;
border: 1px solid black;
padding-bottom: 35px;
}
.column.left .other {
margin-top: 10px;
}
.column.right .other:nth-child(n+2) {
margin-top: 10px;
}
#media (max-width: 768px) {
.flex-item {
flex-basis: calc(( (50vw - 65px) / 3) - 10px);
height: calc(( (50vw - 65px) / 3) - 10px);
}
.flex-item:nth-child(n+4) {
margin-top: 12px;
}
}
#media (max-width: 480px) {
.flex-item {
flex-basis: calc(( (50vw - 65px) / 2) - 10px);
height: calc(( (50vw - 65px) / 2) - 10px);
}
.flex-item:nth-child(n+3) {
margin-top: 15px;
}
}
<div class="container">
<div class="column left">
<div class="flex-container">
<div class="flex-item">
<div class="flex-item-content">
L1
</div>
</div>
<div class="flex-item">
<div class="flex-item-content">
L2
</div>
</div>
<div class="flex-item">
<div class="flex-item-content">
L3
</div>
</div>
<div class="flex-item">
<div class="flex-item-content">
L4
</div>
</div>
<div class="flex-item">
<div class="flex-item-content">
L5<br>L5
</div>
</div>
<div class="flex-item">
<div class="flex-item-content">
L6
</div>
</div>
</div>
<div class="other">
Other stuff - left
</div>
</div>
<div class="column right">
<div class="other">
Other stuff - right
</div>
<div class="other">
Other stuff - right
</div>
</div>
</div>
This is doable with some fairly simple code, as long as the parent column's width is consistent at 50%-ish, and that the space between squares don't have to be strictly equal to a certain value. The vw (viewport width percentage) unit allows for a consistent size to be applied to both width and height of an element.
Here is an example that I boiled down to the fewest elements, and some notes help to move it in to your codebase.
Experiment with .flex-item's height and flex-basis (third value of flex) to get a size you like.
No padding or margin values are needed because justify-content: space-between; helpfully calculates that for us.
Using a line-height equal to the height of .flex-item would allow for an inner element with display: inline-block; and vertical-align: middle; to be centred.
.column {
width: 48vw;
height: 48vw;
padding: 1vw;
border: 1px solid #ccc;
}
.flex-container {
display: flex;
flex-flow: row;
justify-content: space-between;
}
.flex-item {
height: 6vw;
line-height: 6vw;
text-align: center;
border: 1px solid #ccc;
flex: 0 0 6vw;
}
<div class="column">
<div class="flex-container">
<div class="flex-item">
L1
</div>
<div class="flex-item">
L2
</div>
<div class="flex-item">
L3
</div>
<div class="flex-item">
L4
</div>
<div class="flex-item">
L5
</div>
<div class="flex-item">
L6
</div>
</div>
</div>
Only in the latest browsers? CSS Grid to the rescue! It's got great support in the latest versions. You may need some vendor prefixes still; check on CanIUse for the details.
Here it is as a fork: https://codepen.io/jackmakesthings/pen/MoJNNV
.container {
display: flex;
flex-direction: row;
padding: 25px;
border: 2px red solid;
}
.column {
width: 100%;
height: 100%;
float: left;
}
.grid-row {
display: grid;
grid-gap: 10px; /* set this to whatever space you need between boxes */
grid-template-columns: repeat(6, 1fr); /* grid autosizes 6 columns */
}
.row-item {
grid-column: 1 / 7; /* to span the whole row */
border: 1px solid;
padding: 10px;
}
.grid-item {
position: relative;
border: 1px solid;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
/* This is a nifty trick for getting those fixed aspect ratio boxes. */
.grid-item:before {
content: '';
float: left;
width: 0;
height: 0;
padding-bottom: 100%;
}
.grid-item:after {
display: table;
clear: both;
}
/* Responsive grid changes? Sure! */
#media (max-width: 1000px) {
/* We just have to change the grid template: */
.grid-row {
grid-template-columns: repeat(3, 1fr);
}
/* Unexpected thing I ran into - you also have to change this, or the grid stays big enough to accommodate the old 6-column-sized row-item. Makes sense, but vexed me for a minute! */
.row-item {
grid-column: 1 / 4;
}
}
<div class="container">
<div class="column" style="margin-right: 20px">
<div class="grid-row">
<div class="grid-item">L1</div>
<div class="grid-item">L2</div>
<div class="grid-item">L3</div>
<div class="grid-item">L4</div>
<div class="grid-item">L5</div>
<div class="grid-item">L6</div>
<div class="row-item">some other thing</div>
<div class="row-item">and another</div>
</div>
</div>
<div class="column" style="margin-left: 20px; border: 1px black solid; height: 500px">
Other stuff
<div>