Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
CSS Grid Elements
CSS Grid Elements consist of a grid container (parent element) and grid items (child elements). The grid container is defined using display: grid, while all direct children automatically become grid items arranged within the grid layout.
Syntax
.grid-container {
display: grid;
/* Grid properties */
}
.grid-item {
/* Item styling */
}
Grid Structure
A CSS Grid consists of −
-
Grid Container: The parent element with
display: grid - Grid Items: Direct children of the grid container
Example: Basic Grid Layout
The following example creates a 2-column grid with 6 grid items −
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: grid;
grid-template-columns: auto auto;
gap: 10px;
padding: 20px;
background-color: #f1f1f1;
}
.grid-item {
background-color: #4CAF50;
border: 2px solid #333;
padding: 25px;
font-size: 20px;
text-align: center;
color: white;
font-weight: bold;
}
</style>
</head>
<body>
<h1>CSS Grid Elements</h1>
<div class="container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
<div class="grid-item">Item 4</div>
<div class="grid-item">Item 5</div>
<div class="grid-item">Item 6</div>
</div>
</body>
</html>
A 2-column grid layout displays with 6 green grid items arranged in 3 rows. Each item has white text, borders, and is centered within its cell. The grid has a light gray background with spacing between items.
Key Properties
| Property | Description | Applied To |
|---|---|---|
display: grid |
Creates a grid container | Parent element |
grid-template-columns |
Defines column sizes | Grid container |
grid-template-rows |
Defines row sizes | Grid container |
gap |
Sets spacing between items | Grid container |
Conclusion
CSS Grid Elements work together as a parent-child relationship where the grid container defines the layout structure and grid items automatically flow into the defined grid cells. This creates powerful, responsive layouts with minimal code.
Advertisements
