Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
CSS

CSS: Use Grid auto-fit and auto-fill for Responsive Layouts Without Media Queries

- 30.03.26 - ErcanOPAK

πŸ“± Responsive Grids, Zero Media Queries

Media queries for every breakpoint? Tedious. Grid auto-fit/auto-fill creates responsive layouts automatically. Items reflow based on space available.

The Magic Formula

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1rem;
}

/* That's it! Fully responsive without media queries
   Items wrap automatically based on available space */

🎯 How It Works

repeat(auto-fit, minmax(250px, 1fr))

Breaking it down:
- repeat(): Create columns
- auto-fit: As many columns as fit
- minmax(250px, 1fr): Min 250px, max 1 fraction

Wide screen (1200px):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚  250  β”‚  250  β”‚  250  β”‚  250  β”‚  (4 columns)
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜

Medium screen (800px):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚  266  β”‚  266  β”‚  266  β”‚  (3 columns)
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜

Small screen (500px):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚  250  β”‚  250  β”‚  (2 columns)
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜

Tiny screen (300px):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”
β”‚  300  β”‚  (1 column, stretches to fill)
β””β”€β”€β”€β”€β”€β”€β”€β”˜

Automatic reflow!

auto-fit vs auto-fill

auto-fit

Stretches items to fill space

3 items, wide screen:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Item   β”‚   Item   β”‚   Item   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Each item expands to fill width

auto-fill

Creates ghost columns

3 items, wide screen:
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚Item β”‚Item β”‚Item β”‚     β”‚     β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
Items stay minimum width

/* Use auto-fit when:
   You want items to stretch and fill space */
.gallery {
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

/* Use auto-fill when:
   You want consistent item sizes, allow empty space */
.product-grid {
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}

Real-World Examples

πŸ“Έ Image Gallery

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1rem;
}

.gallery img {
  width: 100%;
  height: 300px;
  object-fit: cover;
  border-radius: 8px;
}

/* Responsive without media queries!
   Desktop: 4-5 columns
   Tablet: 2-3 columns
   Mobile: 1 column */

πŸ›οΈ Product Cards

.products {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 2rem;
  padding: 2rem;
}

.product-card {
  background: white;
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

/* auto-fill ensures cards stay ~300px
   Extra space on right if not enough items */

βœ… When to Use

  • Image galleries: Photos that should fill space
  • Product grids: E-commerce item listings
  • Card layouts: Blog posts, team members
  • Icon grids: Feature showcases
  • Dashboard widgets: Admin panels

πŸ’‘ Pro Tips

  • Adjust min width: Change 250px based on content needs
  • Add max width: minmax(250px, 400px) to prevent too large items
  • Dense packing: grid-auto-flow: dense fills gaps
  • Fallback: Add media query for very old browsers

🎯 Cheat Sheet

Pattern Behavior
auto-fit Stretch items to fill
auto-fill Keep min size, allow gaps
minmax(250px, 1fr) Min 250px, max equal fractions
gap: 1rem Space between items

“Removed 200 lines of media query CSS. One line with auto-fit replaced it all. Product grid now works perfectly from 320px to 4K displays. CSS Grid is magic.”

β€” Frontend Developer

Related posts:

How to apply two classes to a single element in CSS

CSS β€” contain Property Prevents Reflow Cascades

CSS Container Queries β€” The Future of Responsive Design

Post Views: 5

Post navigation

Windows 11: Use Virtual Desktops to Organize Workspaces
HTML: Use Semantic HTML5 Elements for Better SEO and Accessibility

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (751)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (447)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (751)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com