Quick CSS Guide to CSS Grid
Quick CSS Guide to CSS Grid
It
allows developers to define rows, columns, and areas for precise control over layout and
alignment. This guide introduces CSS Grid concepts, syntax, and practical examples.
1
1. Grid Container Properties 3
2. Grid Item Properties 3
Examples 3
Example 1: Simple Grid Layout 3
Example 2: Responsive Grid Using auto-fit 4
Exercise 1: Define a Grid Layout 4
Multiple-Choice Questions 5
Question 1: 5
Question 2: 5
Advanced Example: Grid Areas 6
Exercise 2: Recreate the Layout 7
CSS Grid Layout is a two-dimensional system for managing both rows and columns in a
container. It's ideal for complex layouts, allowing for grid-based alignment of elements without
the need for additional markup or complex calculations.
The parent container (grid container) is defined with the property display: grid;. Inside the
container, child elements (grid items) are aligned into rows and columns.
Example:
<div class="grid-container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
}
.item {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
2
background-color: lightblue;
border: 1px solid #ccc;
text-align: center;
padding: 20px;
font-size: 18px;
}
</style>
Explanation:
Examples
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
3
display: grid;
grid-template-columns: 100px 100px 100px;
gap: 5px;
}
.item {
background: coral;
text-align: center;
padding: 10px;
}
</style>
Solution:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
4
<div class="grid-container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(3, 100px);
gap: 15px;
justify-items: center;
align-items: center;
}
.item {
background: gray;
color: white;
padding: 10px;
font-size: 20px;
}
</style>
Multiple-Choice Questions
Question 1:
Which property is used to define the spacing between rows in a CSS Grid?
1. gap
2. grid-gap
3. row-gap
4. All of the above
Question 2:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
5
What does grid-template-columns: repeat(3, 1fr); mean?
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
}
.footer {
grid-area: footer;
background: darkgray;
}
</style>
This guide introduces fundamental concepts, practical examples, and exercises to help learners
build a solid foundation in CSS Grid. Let me know if you'd like further examples or a different
focus!
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis