求助:CSS Grid Critters 9.3底部网格项对齐问题解决方法
Let's break down what's causing your bottom row alignment issue and fix it without messing up the top row:
First, let's look at your current grid setup:
- You're using
grid-template-rows: 50% autowhich can behave unpredictably if theplanetcontainer doesn't have an explicit height—50% of an undefined height won't allocate space as you expect. justify-items: endapplies right-alignment to all grid items by default, which is likely why your bottom row elements aren't sitting where you want them.
Solution: Target Bottom Row Elements with justify-self
Instead of modifying the global justify-items or breaking the top row by styling dunes/water directly, override the alignment only for the bottom row items using justify-self—this lets you keep the top row's alignment intact while fixing the bottom:
planet { display: grid; grid-template-columns: auto auto; grid-template-rows: 1fr auto; /* Swap 50% for 1fr to let the top row fill available space reliably */ justify-content: space-evenly; justify-items: end; /* Keep top row aligned to the end */ align-content: space-between; height: 100%; /* Ensure the grid container has a defined height for row calculations */ } /* Override alignment only for bottom row elements */ dunes, water { justify-self: start; /* Adjust to center/end based on the challenge's requirements */ }
Why This Works:
1fringrid-template-rowslets the top row take up all remaining space in the container, whileautomakes the bottom row fit its content perfectly—this is more flexible than a fixed 50% for dynamic layouts.- Adding
height: 100%toplanetgives the grid a clear reference point to calculate row heights correctly. justify-selftargets individual grid items, so you can align the bottom row elements without disrupting the top row'sendalignment.
If You Need to Keep the 50% Top Row Height:
If the challenge explicitly requires a 50% height for the top row, make sure the planet container has a fixed or explicit height (e.g., height: 200px or height: 100vh). Then keep the justify-self override for dunes/water to fix the bottom row alignment.
内容的提问来源于stack exchange,提问作者Dani Amsalem

