A grid container helps you arrange items neatly in rows and columns, like a table but more flexible.
Grid container in CSS
selector {
display: grid;
grid-template-columns: value;
grid-template-rows: value;
gap: value;
}display: grid; turns an element into a grid container.
You define columns and rows with grid-template-columns and grid-template-rows.
.container { display: grid; grid-template-columns: 100px 100px 100px; }
.container { display: grid; grid-template-columns: 1fr 2fr 1fr; }
.container { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
This example creates a grid container with three columns. The middle column is twice as wide as the others. Each item is styled with a blue background and white text. The grid has gaps between items and padding around the container.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Grid Container Example</title> <style> .grid-container { display: grid; grid-template-columns: 1fr 2fr 1fr; gap: 1rem; padding: 1rem; background-color: #f0f0f0; } .grid-item { background-color: #4a90e2; color: white; padding: 1rem; text-align: center; border-radius: 0.5rem; font-weight: bold; } </style> </head> <body> <main> <section class="grid-container" aria-label="Example grid container"> <div class="grid-item">Item 1</div> <div class="grid-item">Item 2</div> <div class="grid-item">Item 3</div> </section> </main> </body> </html>
Use gap to add space between grid items easily.
Grid containers automatically place items in rows and columns based on your settings.
Try resizing the browser to see how grid adapts if you use flexible units like fr.
A grid container uses display: grid; to arrange items in rows and columns.
You control the layout with grid-template-columns and grid-template-rows.
Grid makes it easy to build neat, flexible, and responsive layouts.