
##What is CSS Grid?
CSS Grid is a two-dimensional layout system that lets you create complex and flexible grid layouts on your website. You can define columns and rows and place elements on the grid.
Example HTML structure:
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
##Basic Grid Setup
To use CSS Grid, you first need to define a grid container with the grid-template-columns and grid-template-rows properties.
CSS example:
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* Tres columnas de igual tamaño */
grid-template-rows: auto; /* Altura automática de fila */
}
##Placing Elements on the Grid
Use the grid-column and grid-row properties to place elements on the grid.
CSS example:
.item:nth-child(2) {
grid-column: 2 / 3; /* Segunda columna */
grid-row: 1; /* Primera fila */
}
##Grid Alignment
You can align elements within grid cells using justify-items and align-items.
CSS example:
.container {
justify-items: center; /* Centrar horizontalmente */
align-items: center; /* Centrar verticalmente */
}
##Responsive Layouts
Make your grid responsive by using media queries to change the layout at different screen sizes.
CSS example:
@media (max-width: 768px) {
.container {
grid-template-columns: 1fr; /* Una sola columna en pantallas pequeñas */
}
}
##Working with Named Areas
You can create named areas in the grid for a more readable and maintainable layout.
CSS example:
.container {
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
This article provides detailed examples of how to create grid layouts with CSS Grid, including setup, element placement, and responsive design techniques.