Last active
August 22, 2025 13:43
-
-
Save HarryAdney/96c8accb89a292fa633371a73f9472ab to your computer and use it in GitHub Desktop.
Use This Simple Line Of CSS Grid To Make Your Layout Responsive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grid-template-columns: repeat(auto-fit, minmax(min(100%,[base-width]), 1fr)); | |
https://codepen.io/optimisticweb/full/pvjzdjg | |
HTML | |
<h1>Responsive Layout</h1> | |
<h2>Without Using Media Query</h2> | |
<section class="content-boxes"> | |
<div class="content-box">1</div> | |
<div class="content-box">2</div> | |
<div class="content-box">3</div> | |
<div class="content-box">4</div> | |
<div class="content-box">5</div> | |
<div class="content-box">6</div> | |
<div class="content-box">7</div> | |
<div class="content-box">8</div> | |
<div class="content-box">9</div> | |
<div class="content-box">10</div> | |
<div class="content-box">11</div> | |
<div class="content-box">12</div> | |
</section> | |
css | |
* { | |
margin: 0; | |
padding: 0; | |
box-sizing: border-box; | |
} | |
body { | |
font-family: system-ui, sans-serif; | |
font-size: 1rem; | |
line-height: 1.7; | |
color: #070707; | |
background: fixed linear-gradient(transparent, #fff 70%), | |
fixed | |
repeating-linear-gradient( | |
#efefef, | |
#efefef 1px, | |
transparent 1px, | |
transparent 40px | |
), | |
fixed | |
repeating-linear-gradient( | |
to right, | |
#efefef, | |
#efefef 1px, | |
transparent 1px, | |
transparent 40px | |
) | |
#fff; | |
padding: 3rem; | |
} | |
h1 { | |
font-size: clamp(0.5rem, 2vw + 0.5rem, 1.75rem); | |
text-align: center; | |
} | |
h2 { | |
font-size: clamp(0.5rem, 2vw + 0.5rem, 1.25rem); | |
font-weight: 600; | |
text-align: center; | |
margin-block-end: 2rem; | |
} | |
.content-boxes { | |
display: grid; | |
/* grid-template-columns: | |
repeat(auto-fit, minmax(200px, 1fr)); */ | |
/* Magic happens here */ | |
grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)); | |
gap: 1rem; | |
} | |
.content-box { | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
aspect-ratio: 1; | |
font-size: 1.25rem; | |
outline: 1px dotted #fff; | |
outline-offset: -0.5rem; | |
} | |
.content-box:nth-child(1), | |
.content-box:nth-child(7) { | |
background-color: #fcc7a1; | |
} | |
.content-box:nth-child(2), | |
.content-box:nth-child(8) { | |
background-color: #a6fee6; | |
} | |
.content-box:nth-child(3), | |
.content-box:nth-child(9) { | |
background-color: #bda9fa; | |
} | |
.content-box:nth-child(4), | |
.content-box:nth-child(10) { | |
background-color: #a3dbf8; | |
} | |
.content-box:nth-child(5), | |
.content-box:nth-child(11) { | |
background-color: #fba7e3; | |
} | |
.content-box:nth-child(6), | |
.content-box:nth-child(12) { | |
background-color: #a8c6fb; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment