The .grid-container uses display: grid to 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-item represents an individual item inside the grid.
Observe the Layout:
Open grid-activity.html in 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:
The page has a main div with the class container.
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 container is set to display as a grid:
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:
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:
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:
.footer spans all columns:
Step 4: Adding Styles to Grid Items
Open css-grid-10.2B-basic.css to understand how each grid item is styled:
Each div inside the .container is styled using flex to center the text and given unique background colors based on their position:
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-columns to change the number of columns, for example:
Reposition Elements:
Experiment by changing grid-column or grid-row values for different elements:
Step 6: Reflect and Discuss
How does changing grid-template-columns affect the layout?
What happens when you modify grid-row values for the menu?
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.