JSFiddle - React, Tailwind, and code Playground
by Alexandru Gatea
HTML
<h1>height same as width. pure css. no js</h1>
<p>
we need a grid system, and each card is contained in each cell.
</p>
<p>the cell will have red border, the actual card will have black border</p>
<div class="grid">
<div class="cell">
<div class="box">
<div class="content">What goes inside the card</div>
</div>
</div>
<div class="cell">
<div class="box"></div>
</div>
<div class="cell">
<div class="box"></div>
</div>
<div class="cell">
<div class="box"></div>
</div>
<div class="cell">
<div class="box"></div>
</div>
<div class="cell">
<div class="box"></div>
</div>
<div class="cell">
<div class="box"></div>
</div>
<div class="cell">
<div class="box"></div>
</div>
<div class="cell">
<div class="box"></div>
</div>
<div class="cell">
<div class="box"></div>
</div>
<div class="cell">
<div class="box"></div>
</div>
<div class="cell">
<div class="box"></div>
</div>
</div>
CSS
/* you don't need these */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
padding: 100px;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
grid-gap: 50px;
margin-top: 100px;
}
.cell {
width: 100%;
border: 1px solid red;
padding: 10px;
height: 100%;
}
/* you need this */
/* This is the magic trick */
/* IMPORTANT. THE BOX IS POSITION: RELATIVE */
.box {
position: relative;
width: 100%;
border: 1px solid black;
/* I lied. this is the trick */
height: 0px;
padding-bottom: 100%;
}
/* IMPORTANT: THE CONTENT IS ABSOLUTE */
.content {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}