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: Fix a Grid Layout Where Columns Ignore the Widths You Set

- 06.09.26 - ErcanOPAK

🎨 Your Grid Columns Are Ignoring the Widths You Explicitly Set

A CSS Grid is given explicit column widths – `grid-template-columns: 1fr 1fr 1fr` – and one column still ends up wider than the others, stretching the whole layout, because a grid track’s MINIMUM size defaults to the size of its content, and long unbreakable content (a URL, a wide image, a long unbroken word) can override the width you asked for.

🔎 The Problem

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 20px;
}

/* Looks like it should split evenly into three equal columns - but a
   child with a long unbreakable string (a URL, a code snippet, an
   un-hyphenated word) forces its column wider than 1fr, because grid
   tracks default to `min-width: auto`, which means "at least as wide
   as my content needs to be", NOT "exactly this fraction of space". */

✅ Fix: Override the Implicit Minimum Size

  • Add `min-width: 0` to the grid items (not the grid container) – this is the single most common fix, since it explicitly overrides the default `auto` minimum that lets content dictate track size.
  • Alternatively, define columns with `minmax(0, 1fr)` instead of a bare `1fr` – `grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1fr)` – which achieves the same result directly on the track definition rather than on each item.
  • For the specific case of long unbreakable text, pairing the `min-width` fix with `overflow-wrap: break-word` (or `word-break: break-all` for things like raw URLs) lets the content actually wrap instead of just being clipped once it’s no longer forcing the column wide.

⚠️ Why This Isn’t Obvious From the Spec Name

  • `1fr` describes how leftover space is DISTRIBUTED, not a maximum the track is capped at – it’s entirely consistent with the spec for a `1fr` track to end up wider than an equal split if its minimum content size demands it.
  • This is the direct CSS Grid analogue of the same `min-width: auto` behavior that trips people up with Flexbox – the underlying flexbox/grid rule about implicit minimum sizing is identical, only the property name differs.

‘1fr’ is a promise about how space gets divided, not a promise about how small a track is allowed to get – content can still force it wider unless you say otherwise.

— UI Engineer

Related posts:

Why position: sticky Randomly Stops Working

CSS: Create Realistic UI Depth with Advanced Box Shadow

CSS: Master Overflow for Content Handling

Post Views: 2

Post navigation

Git: Fix a Detached HEAD Left Behind After Checking Out a Tag
The AI Prompt That Turns a Confusing Utility or Phone Bill Into a Clear Explanation of Every Charge

Leave a Reply Cancel reply

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

October 2026
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Sep    

Most Viewed Posts

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

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps
  • Photoshop: Fix a Color Profile Mismatch That Makes Printed Output Look Nothing Like What You Saw On Screen
  • WordPress: Fix Search Results That Return Pages From a Theme You Deactivated Months Ago
  • Visual Studio: Fix a Test Project That Builds Fine Alone but Fails to Discover Any Tests After a NuGet Restore
  • ASP.NET Core: Fix a File Upload That Times Out on Slow Connections Only Because Kestrel’s Minimum Data Rate Feature Kicked In
  • JavaScript: Fix an Array Destructuring Default Value That Silently Never Applies Because null Was Passed Instead of Undefined

Most Viewed Posts

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

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps

Social

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