Steps
- make the parent of the elements grid
display:grid;
- add this value for grid-template-columns property of the parent
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
The Complete Code
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: sans-serif;
}
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #eee;
}
.container {
width: 90%;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
.box {
height: 200px;
border: 3px solid green;
background: rgba(255, 255, 0, 0.424);
display: grid;
place-content: center;
position: relative;
}