I'm wondering how I would go about creating a layout with responsive squares. Each square would have vertically and horizontally aligned content. The specific example is displayed below...
This question is related to
html
css
responsive-design
grid-layout
aspect-ratio
You could use vw (view-width) units, which would make the squares responsive according to the width of the screen.
A quick mock-up of this would be:
html,_x000D_
body {_x000D_
margin: 0;_x000D_
padding: 0;_x000D_
}_x000D_
div {_x000D_
height: 25vw;_x000D_
width: 25vw;_x000D_
background: tomato;_x000D_
display: inline-block;_x000D_
text-align: center;_x000D_
line-height: 25vw;_x000D_
font-size: 20vw;_x000D_
margin-right: -4px;_x000D_
position: relative;_x000D_
}_x000D_
/*demo only*/_x000D_
_x000D_
div:before {_x000D_
content: "";_x000D_
position: absolute;_x000D_
top: 0;_x000D_
left: 0;_x000D_
height: inherit;_x000D_
width: inherit;_x000D_
background: rgba(200, 200, 200, 0.6);_x000D_
transition: all 0.4s;_x000D_
}_x000D_
div:hover:before {_x000D_
background: rgba(200, 200, 200, 0);_x000D_
}
_x000D_
<div>1</div>_x000D_
<div>2</div>_x000D_
<div>3</div>_x000D_
<div>4</div>_x000D_
<div>5</div>_x000D_
<div>6</div>_x000D_
<div>7</div>_x000D_
<div>8</div>
_x000D_
I use this solution for responsive boxes of different rations:
HTML:
<div class="box ratio1_1">
<div class="box-content">
... CONTENT HERE ...
</div>
</div>
CSS:
.box-content {
width: 100%; height: 100%;
top: 0;right: 0;bottom: 0;left: 0;
position: absolute;
}
.box {
position: relative;
width: 100%;
}
.box::before {
content: "";
display: block;
padding-top: 100%; /*square for no ratio*/
}
.ratio1_1::before { padding-top: 100%; }
.ratio1_2::before { padding-top: 200%; }
.ratio2_1::before { padding-top: 50%; }
.ratio4_3::before { padding-top: 75%; }
.ratio16_9::before { padding-top: 56.25%; }
See demo on JSfiddle.net
The accepted answer is great, however this can be done with flexbox
.
Here's a grid system written with BEM syntax that allows for 1-10 columns to be displayed per row.
If there the last row is incomplete (for example you choose to show 5 cells per row and there are 7 items), the trailing items will be centered horizontally. To control the horizontal alignment of the trailing items, simply change the justify-content
property under the .square-grid
class.
.square-grid {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.square-grid__cell {
background-color: rgba(0, 0, 0, 0.03);
box-shadow: 0 0 0 1px black;
overflow: hidden;
position: relative;
}
.square-grid__content {
left: 0;
position: absolute;
top: 0;
}
.square-grid__cell:after {
content: '';
display: block;
padding-bottom: 100%;
}
// Sizes – Number of cells per row
.square-grid__cell--10 {
flex-basis: 10%;
}
.square-grid__cell--9 {
flex-basis: 11.1111111%;
}
.square-grid__cell--8 {
flex-basis: 12.5%;
}
.square-grid__cell--7 {
flex-basis: 14.2857143%;
}
.square-grid__cell--6 {
flex-basis: 16.6666667%;
}
.square-grid__cell--5 {
flex-basis: 20%;
}
.square-grid__cell--4 {
flex-basis: 25%;
}
.square-grid__cell--3 {
flex-basis: 33.333%;
}
.square-grid__cell--2 {
flex-basis: 50%;
}
.square-grid__cell--1 {
flex-basis: 100%;
}
.square-grid {_x000D_
display: flex;_x000D_
flex-wrap: wrap;_x000D_
justify-content: center;_x000D_
}_x000D_
_x000D_
.square-grid__cell {_x000D_
background-color: rgba(0, 0, 0, 0.03);_x000D_
box-shadow: 0 0 0 1px black;_x000D_
overflow: hidden;_x000D_
position: relative;_x000D_
}_x000D_
_x000D_
.square-grid__content {_x000D_
left: 0;_x000D_
position: absolute;_x000D_
top: 0;_x000D_
}_x000D_
_x000D_
.square-grid__cell:after {_x000D_
content: '';_x000D_
display: block;_x000D_
padding-bottom: 100%;_x000D_
}_x000D_
_x000D_
// Sizes – Number of cells per row_x000D_
_x000D_
.square-grid__cell--10 {_x000D_
flex-basis: 10%;_x000D_
}_x000D_
_x000D_
.square-grid__cell--9 {_x000D_
flex-basis: 11.1111111%;_x000D_
}_x000D_
_x000D_
.square-grid__cell--8 {_x000D_
flex-basis: 12.5%;_x000D_
}_x000D_
_x000D_
.square-grid__cell--7 {_x000D_
flex-basis: 14.2857143%;_x000D_
}_x000D_
_x000D_
.square-grid__cell--6 {_x000D_
flex-basis: 16.6666667%;_x000D_
}_x000D_
_x000D_
.square-grid__cell--5 {_x000D_
flex-basis: 20%;_x000D_
}_x000D_
_x000D_
.square-grid__cell--4 {_x000D_
flex-basis: 25%;_x000D_
}_x000D_
_x000D_
.square-grid__cell--3 {_x000D_
flex-basis: 33.333%;_x000D_
}_x000D_
_x000D_
.square-grid__cell--2 {_x000D_
flex-basis: 50%;_x000D_
}_x000D_
_x000D_
.square-grid__cell--1 {_x000D_
flex-basis: 100%;_x000D_
}
_x000D_
<div class='square-grid'>_x000D_
<div class='square-grid__cell square-grid__cell--7'>_x000D_
<div class='square-grid__content'>_x000D_
Some content_x000D_
</div>_x000D_
</div>_x000D_
<div class='square-grid__cell square-grid__cell--7'>_x000D_
<div class='square-grid__content'>_x000D_
Some content_x000D_
</div>_x000D_
</div>_x000D_
<div class='square-grid__cell square-grid__cell--7'>_x000D_
<div class='square-grid__content'>_x000D_
Some content_x000D_
</div>_x000D_
</div>_x000D_
<div class='square-grid__cell square-grid__cell--7'>_x000D_
<div class='square-grid__content'>_x000D_
Some content_x000D_
</div>_x000D_
</div>_x000D_
<div class='square-grid__cell square-grid__cell--7'>_x000D_
<div class='square-grid__content'>_x000D_
Some content_x000D_
</div>_x000D_
</div>_x000D_
<div class='square-grid__cell square-grid__cell--7'>_x000D_
<div class='square-grid__content'>_x000D_
Some content_x000D_
</div>_x000D_
</div>_x000D_
<div class='square-grid__cell square-grid__cell--7'>_x000D_
<div class='square-grid__content'>_x000D_
Some content_x000D_
</div>_x000D_
</div>_x000D_
<div class='square-grid__cell square-grid__cell--7'>_x000D_
<div class='square-grid__content'>_x000D_
Some content_x000D_
</div>_x000D_
</div>_x000D_
</div>
_x000D_
Fiddle: https://jsfiddle.net/patrickberkeley/noLm1r45/3/
This is tested in FF and Chrome.
Now we can easily do this using the aspect-ratio
ref property
.container {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr)); /* 3 columns */
grid-gap: 10px;
}
.container>* {
aspect-ratio: 1 / 1; /* a square ratio */
border: 1px solid;
/* center content */
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
img {
max-width: 100%;
display: block;
}
_x000D_
<div class="container">
<div> some content here </div>
<div><img src="https://picsum.photos/id/25/400/400"></div>
<div>
<h1>a title</h1>
</div>
<div>more and more content <br>here</div>
<div>
<h2>another title</h2>
</div>
<div><img src="https://picsum.photos/id/104/400/400"></div>
</div>
_x000D_
Also like below where we can have a variable number of columns
.container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
grid-gap: 10px;
}
.container>* {
aspect-ratio: 1 / 1; /* a square ratio */
border: 1px solid;
/* center content */
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
img {
max-width: 100%;
display: block;
}
_x000D_
<div class="container">
<div> some content here </div>
<div><img src="https://picsum.photos/id/25/400/400"></div>
<div>
<h1>a title</h1>
</div>
<div>more and more content <br>here</div>
<div>
<h2>another title</h2>
</div>
<div><img src="https://picsum.photos/id/104/400/400"></div>
<div>more and more content <br>here</div>
<div>
<h2>another title</h2>
</div>
<div><img src="https://picsum.photos/id/104/400/400"></div>
</div>
_x000D_
Source: Stackoverflow.com