10.2 Student Activity

Create a simple grid container

Student Activity:

  1. Objective: Create a simple grid container to understand column and row definitions and observe how grid items fill the cells.

  2. 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-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.

  3. 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:

  1. css-grid-student-activity10.2B.html: The structure of the webpage.

  2. css-grid-10.2B.css: The CSS for defining the grid layout.

  3. 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:

  1. Change the Number of Columns:

    • Modify grid-template-columns to change the number of columns, for example:

  2. 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.

Last updated