Introduction
Grid helps arrange things neatly on a page in rows and columns. It makes building layouts easier and cleaner.
Jump into concepts and practice - no test required
Grid helps arrange things neatly on a page in rows and columns. It makes building layouts easier and cleaner.
.container { display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 10px; }
display: grid; on the container to start using grid layout.grid-template-columns sets how many columns and their widths..container { display: grid; grid-template-columns: 100px 200px 100px; }
.container { display: grid; grid-template-columns: repeat(4, 1fr); }
.container { display: grid; grid-template-columns: 1fr 2fr 1fr; }
This example shows six boxes arranged in three equal columns using CSS Grid. The boxes have space between them and are centered on the page.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Grid Example</title> <style> .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; max-width: 600px; margin: 2rem auto; padding: 1rem; border: 2px solid #333; } .grid-item { background-color: #4CAF50; color: white; padding: 1rem; text-align: center; font-size: 1.2rem; border-radius: 0.5rem; } </style> </head> <body> <main> <section class="grid-container" aria-label="Simple grid layout"> <div class="grid-item">Box 1</div> <div class="grid-item">Box 2</div> <div class="grid-item">Box 3</div> <div class="grid-item">Box 4</div> <div class="grid-item">Box 5</div> <div class="grid-item">Box 6</div> </section> </main> </body> </html>
Grid is powerful for building complex layouts without extra code.
It works well with responsive design to adjust layouts on different screens.
Use browser DevTools to see grid lines and understand how items are placed.
Grid helps organize content in rows and columns easily.
It saves time and makes layouts cleaner and more flexible.
Grid works well for both simple and complex webpage designs.
display: grid; turns an element into a grid container.display: flex; creates a flex container, not grid. position: grid; is invalid. grid-template-columns defines columns but does not start the grid.container {
display: grid;
grid-template-columns: 100px 100px;
grid-template-rows: 50px 50px;
}
display: grid;, so it forms a grid with specified columns and rows..box {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-rows: 100px;
grid-template-columns: 50px 50px 50px;
}grid-template-columns is declared twice; the second declaration overwrites the first.grid-template-rows: 100px; with one value is valid, display: grid; is correct, and column values can differ.