7. Css Grid and Media Queries
7. Css Grid and Media Queries
2. Media Queries
CSS Grid
The CSS Grid Layout Module offers a grid-based layout system, with rows and columns,
making it easier to design web pages without having to use floats and positioning.
An HTML element becomes a grid container when its display property is set to grid
Grid Container
These are the grid container properties:
gap
the gap is a shorthand property for row-gap and column-gap
grid-template-columns
The grid-template-columns property defines the number of columns in your grid
layout, and it can define the width of each column.
The value is a space-separated list, where each value defines the width of the
respective column.
.grid-container {
display: grid;
grid-template-columns: 80px 200px auto 40px;
grid-template-columns: auto auto auto auto;
}
grid-template-rows
The grid-template-rows property defines the height of each row.
justify-content
The justify-content property is used to align the whole grid inside the container.
It can have the following values:
space-evenly
space-between
space-around
center
start
end
align-content
The align-content property is used to vertically align the whole grid inside the
container. It can have the following values:
space-evenly
space-between
space-around
center
start
end
align-items
To align the items inside the grid.
grid-template-areas
.grid-container {
grid-template-areas:
'header header header header header header'
'menu main main main right right'
'menu footer footer footer footer footer';
}
Grid Item
grid-column
.item1 {
grid-column: 1 / span 3;
}
grid-row
The grid-row property defines on which row to place an item. You define where
the item will start, and where the item will end.
The grid-row property is a shorthand property for the grid-row-start and the grid-
row-end properties.
grid-area
The grid-area property can be used as a shorthand property for the grid-row-
start, grid-column-start, grid-row-end, and grid-column-end properties.
justify-self
Aligns a grid item inside a cell along the inline (row) axis
start
end
center
strech
align-self
Aligns a grid item inside a cell along the block (column) axis (as opposed to
justify-self which aligns along the inline (row) axis). This value applies to the
content inside a single grid item.
start
end
center
strech
Types of Breakpoints
/* Small devices (portrait tablets and large phones, 600px and up) */
@media screen and (min-width: 600px) {...}
/* Extra large devices (large laptops and desktops, 1200px and up) */
@media screen and (min-width: 1200px) {...}
Media queries can also be used to change layout of a page depending on the
orientation of the browser.
You can have a set of CSS properties that will only apply when the browser
window is wider than its height, a so called "Landscape" orientation:
Further Readings:
https://developer.mozilla.org/en-
US/docs/Web/CSS/Media_Queries/Using_media_queries
Assignments:
1. Build a Calculator layout like this: