Grid item placement helps you decide exactly where each box goes in a grid layout. It makes your page look neat and organized.
Grid item placement in CSS
grid-column: start / end; grid-row: start / end;
start and end are grid line numbers or names.
You can use span to make an item cover multiple columns or rows.
grid-column: 1 / 3; grid-row: 2 / 4;
grid-column: 2 / span 2; grid-row: 1 / 2;
grid-column: 3; grid-row: 1 / 3;
This example creates a grid with 4 columns and 3 rows. Each colored box is placed using grid-column and grid-row to show how items can span multiple columns or rows. The boxes have keyboard focus for accessibility.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Grid Item Placement Example</title> <style> .grid-container { display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(3, 5rem); gap: 0.5rem; max-width: 400px; margin: 2rem auto; border: 2px solid #333; } .item { background-color: #4a90e2; color: white; display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 1.2rem; border-radius: 0.5rem; } .item1 { grid-column: 1 / 3; grid-row: 1 / 2; } .item2 { grid-column: 3 / 5; grid-row: 1 / 3; } .item3 { grid-column: 1 / 2; grid-row: 2 / 4; } .item4 { grid-column: 2 / 4; grid-row: 3 / 4; } </style> </head> <body> <main> <section class="grid-container" aria-label="Example grid layout"> <div class="item item1" tabindex="0">Item 1</div> <div class="item item2" tabindex="0">Item 2</div> <div class="item item3" tabindex="0">Item 3</div> <div class="item item4" tabindex="0">Item 4</div> </section> </main> </body> </html>
You can use named grid lines for easier placement if you define them in grid-template-columns or grid-template-rows.
Use tabindex="0" on grid items to make them keyboard focusable for better accessibility.
Try resizing the browser to see how the grid adapts if you use flexible units like fr.
Grid item placement controls where each box sits in a grid.
Use grid-column and grid-row with start and end lines or spans.
This helps create neat, organized layouts that adapt well to different screen sizes.