no license
64y6kMGBSVhmzQfbQP8oc9bYR1c2g7asOs4JOlci
Bookmark

Css3

CSS Grid and Responsive Design

CSS Grid Layout

CSS Grid Layout is a two-dimensional layout system for the web. It allows you to create complex layouts more easily than using floats and positioning.

Grid Container

To create a grid layout, you need to define a container element as a grid using the display: grid; property:

.grid-container {

    display: grid;

}

Grid Tracks

Grid tracks are the rows and columns of the grid. You can define them using the grid-template-columns and grid-template-rows properties:

.grid-container {

    display: grid;

    grid-template-columns: 200px 200px 200px;

    grid-template-rows: 100px 100px;

}

In this example, the grid has three columns, each 200px wide, and two rows, each 100px high.

Grid Gaps

The grid-gap property defines the spacing between grid items:

.grid-container {

    display: grid;

    grid-template-columns: 200px 200px 200px;

    grid-template-rows: 100px 100px;

    grid-gap: 10px;

}

Placing Grid Items

To place items within the grid, you can use properties like grid-column and grid-row:

.grid-item {

    grid-column: 1 / 3;

    grid-row: 1 / 2;

}

In this example, the item spans from column 1 to column 3 and row 1 to row 2.

Media Queries

Media queries are a cornerstone of responsive design. They allow you to apply CSS rules based on the characteristics of the user's device, such as its width, height, orientation, and resolution.

Basic Syntax

A media query consists of a media type and at least one condition that must be true for the query to apply:

@media (max-width: 600px) {

    .container {

        background-color: lightblue;

    }

}

In this example, the background color of the container will change to light blue when the viewport width is 600px or less.

Responsive Design

Responsive design ensures that web pages look good on all devices, from desktops to smartphones. Key techniques include:

  • Flexible grid layouts that use relative length units like percentages.
  • Flexible images that scale with the grid layout.
  • Media queries to apply different styles based on device characteristics.

Viewport Meta Tag

To ensure proper scaling on mobile devices, include the viewport meta tag in your HTML:

<meta name="viewport" content="width=device-width, initial-scale=1.0">

Flexible Grid Layout

Using relative units like percentages for widths allows the grid to resize based on the viewport size:

.flexible-container {

    display: grid;

    grid-template-columns: 1fr 1fr 1fr;

    grid-gap: 10px;

}

In this example, each column takes up an equal fraction of the available space.

Conclusion

Today's lecture covered CSS Grid, media queries, and responsive design principles. These techniques enable you to create complex and adaptable layouts that work well on a variety of devices.

Practice

CSS Grid Practice

Create a grid container with three columns and two rows. Place an item that spans the first two columns in the first row:

.grid-container {

    display: grid;

    grid-template-columns: 1fr 1fr 1fr;

    grid-template-rows: 100px 100px;

    grid-gap: 10px;

}

.grid-item {

    grid-column: 1 / 3;

    grid-row: 1 / 2;

}

Media Queries Practice

Write a media query that changes the background color of a container to lightgreen when the viewport width is 800px or less:

@media (max-width: 800px) {

    .container {

        background-color: lightgreen;

    }

}

Responsive Design Practice

Set up a flexible grid layout that adjusts the number of columns based on the viewport width. Use media queries to create:

  • A single column layout for viewports 600px or less.
  • A two-column layout for viewports between 601px and 900px.
  • A three-column layout for viewports above 900px.
.flexible-container {

    display: grid;

    grid-gap: 10px;

}

@media (max-width: 600px) {

    .flexible-container {

        grid-template-columns: 1fr;

    }

}

@media (min-width: 601px) and (max-width: 900px) {

    .flexible-container {

        grid-template-columns: 1fr 1fr;

    }

}

@media (min-width: 901px) {

    .flexible-container {

        grid-template-columns: 1fr 1fr 1fr;

    }

}
Post a Comment

Post a Comment

Ask your questions.