You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Media queries不生效求助:1024px宽度下Grid无法实现4列布局

Fixing Your 4-Column Grid Media Query Issue

Hey there! Let's figure out why your grid isn't switching to 4 columns when the screen hits 1024px. I spot a couple of key issues that are likely causing this—let's break them down and fix them step by step:

1. Missing Grid Container Column Definition for Desktop

The biggest problem is that your .grid-container doesn't get updated to a 4-column layout in the min-width:1024px media query. Right now, you're only adjusting the grid-column values for the items, but if the container itself is still set to 1 or 2 columns, span 1 won't create a 4-column grid.

Fix: Update the Grid Container in Each Breakpoint

First, make sure your base .grid-container has a column setup, then adjust it in each media query:

/* Base mobile styles */
.grid-container {
  display: grid;
  gap: 1.5rem; /* Optional: Adds spacing between items */
  grid-template-columns: 1fr; /* 1 column for mobile */
}

/* Tablet (768px+) */
@media (min-width: 768px) {
  .grid-container {
    grid-template-columns: repeat(2, 1fr); /* 2 columns for tablet */
  }
  /* Your existing styles here... */
  header { justify-content: space-between; }
  nav { display: inline-block; align-items: flex-end; font-family: 'Roboto', sans-serif; font-size: 20px; justify-content: space-around; padding: 0 20px; }
  h1 { font-size: 42px }
  p { font-size: 32px; }
  img { width: 100%; }
  .one, .two { grid-column: span 2; }
  .three, .four { grid-column: span 2; }
  .five, .six { grid-column: span 2; } /* Fixed: Removed extra comma here */
  .seven, .eight { grid-column: span 2; }
}

/* Desktop (1024px+) */
@media (min-width: 1024px) {
  .grid-container {
    grid-template-columns: repeat(4, 1fr); /* 4 columns for desktop */
  }
  /* Your existing styles here... */
  .flex-parent { flex-direction: row; justify-items: flex-end; }
  .flex-child { width: 100%; }
  .one, .two, .three, .four { grid-column: span 1; }
  .five, .six, .seven, .eight { grid-column: span 1; }
}

2. Syntax Error in Tablet Media Query

In your min-width:768px query, you have a trailing comma in .five, .six, {—this invalid CSS can break the selector (and potentially other styles in that block). I fixed that in the code above by removing the extra comma.

3. Quick Check for Conflicting Styles

Double-check if any other CSS rules are overriding your grid settings:

  • Make sure no !important declarations are forcing a different column layout
  • Verify that .grid-item doesn't have a fixed width that would prevent it from fitting into 4 columns
  • Ensure display: grid is properly applied to .grid-container (no accidental display: flex or other layout properties overriding it)

Give these fixes a go, and your grid should switch smoothly to 4 columns on screens 1024px and wider!

内容的提问来源于stack exchange,提问作者dannuzak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 00:42:47