10.2 Student Activity
Create a simple grid container
Student Activity:
Objective: Create a simple grid container to understand column and row definitions and observe how grid items fill the cells.
Instructions:
Step 1: Open a code editor and create an HTML file named
grid-activity.html.Step 2: Inside
grid-activity.html, set up a basic HTML structure with a grid container.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CSS Grid Activity</title> <style> .grid-container { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 100px 200px; gap: 10px; border: 1px solid #333; } .grid-item { background-color: #4CAF50; color: white; text-align: center; padding: 20px; font-size: 1em; } </style> </head> <body> <div class="grid-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>Step 3: Review the Code:
The
.grid-containerusesdisplay: gridto define it as a grid container.grid-template-columns: 1fr 1fr 1fr;divides the container into three equal columns.grid-template-rows: 100px 200px;sets the height of the first row to 100px and the second row to 200px.Each
.grid-itemrepresents an individual item inside the grid.
Observe the Layout:
Open
grid-activity.htmlin a browser.Observe how the items are distributed in the grid cells across the specified rows and columns.
Challenge Activity: Positioning Grid Items with CSS Grid
Objective:
Learn how to position elements within a grid layout using CSS Grid. By the end of this activity, you will understand how to use grid-template-columns, grid-template-rows, and positioning properties to create a structured webpage layout.
Prerequisites:
Basic knowledge of HTML and CSS, including CSS selectors.
Files Overview:
You have three files to work with:
css-grid-student-activity10.2B.html: The structure of the webpage.
css-grid-10.2B.css: The CSS for defining the grid layout.
css-grid-10.2B-basic.css: The CSS for styling individual grid items.
Step 1: Understand the HTML Structure
Open css-grid-student-activity10.2B.html and examine the structure:
// Some code
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="css-grid-10.2B.css" />
<link rel="stylesheet" href="css-grid-10.2B-basic.css" />
</head>
<div class="container">
<div class="header">HEADER</div>
<div class="menu">MENU</div>
<div class="content">CONTENT</div>
<div class="footer">FOOTER</div>
</div>
</html>
The page has a main
divwith the classcontainer.Inside the
container, there are four elements: HEADER, MENU, CONTENT, and FOOTER.These elements will be positioned using the CSS Grid defined in css-grid-10.2B.
css.
Step 2: Defining the Grid Layout in CSS
Open css-grid-10.2B.css to understand how the grid layout is defined for the container:
The
containeris set to display as a grid:.container { display: grid; grid-gap: 3px; /* Adds spacing between grid items */ grid-template-columns: repeat(12, 1fr); /* Defines 12 equal-width columns */ grid-template-rows: 40px 200px 40px; /* Defines three rows of varying heights */ }The container consists of 12 equal-width columns and 3 rows with different heights:
First row: 40 pixels high.
Second row: 200 pixels high.
Third row: 40 pixels high.
Step 3: Positioning Grid Items
The elements in the grid (header, menu, content, footer) are positioned using grid properties like grid-column and grid-row:
.header is positioned across the second column to the last column:
.header { grid-column: 2 / -1; }This means the header starts from the second column and spans to the end of the container.
.menu occupies the first and second rows in its column:
.menu { grid-row: 1 / 3; }This means the menu starts in the first row and spans down into the second row.
.content starts from the second column to the last column:
.content { grid-column: 2 / -1; }.footer spans all columns:
.footer { grid-column: 1 / -1; }
Step 4: Adding Styles to Grid Items
Open css-grid-10.2B-basic.css to understand how each grid item is styled:
Each
divinside the.containeris styled usingflexto center the text and given unique background colors based on their position:.container > div { display: flex; justify-content: center; align-items: center; font-size: 2em; color: #ffeead; } .container > div:nth-child(1n) { background-color: #96ceb4; /* Light green */ } .container > div:nth-child(2n) { background-color: #ff6f69; /* Pink */ } .container > div:nth-child(3n) { background-color: #88d8b0; /* Aqua */ } .container > div:nth-child(4n) { background-color: #ffcc5c; /* Yellow */ }
Step 5: Experiment with Grid Positioning
Now, modify the grid-template properties and grid-column or grid-row values to see how the layout changes:
Change the Number of Columns:
Modify
grid-template-columnsto change the number of columns, for example:
grid-template-columns: repeat(6, 1fr); /* Changes to 6 columns */Reposition Elements:
Experiment by changing
grid-columnorgrid-rowvalues for different elements:
.content { grid-column: 1 / 7; /* Spans from column 1 to 7 */ }
Step 6: Reflect and Discuss
How does changing
grid-template-columnsaffect the layout?What happens when you modify
grid-rowvalues for themenu?How can you use these techniques to create more complex layouts?
Step 7: Additional Challenge
Try adding a new element, like a SIDEBAR, and use the grid properties to position it appropriately within the layout. Update the HTML and CSS files accordingly.
This step-by-step activity will help students get hands-on experience with grid layouts, providing a solid understanding of how to position elements within a CSS Grid. Encourage students to experiment and make changes to reinforce their understanding.
Last updated