COMP 126: Practical Web Design & Development for Everyone
The CSS grid is a system of layout properties that allows us to stack and line up content in columns and rows
Flexbox allows rows or columns; grid allows rows and columns
Areas within your grid are containers for your content
Containing elements placed within your grid can be turned into sub-grids or flex boxes
A GRID IS A BUNCH OF BOXES
AND YOU PUT CONTENT IN THOSE BOXES
grid container: just like in flexbox: the containing unit
grid item: any DIRECT child element of the grid container
grid track: column or row within the grid container
grid lines: the lines bounding each side of a grid track: starts at far left-hand border and ends at far right-hand border
grid cell: the space made up by two adjacent row and column grid lines
grid area: rectangular area comprising one or more grid cells
gap: the gutter/space between rows/columns
row axis: the x axis (always, not like in Flexbox)
column axis: the y axis (ditto)
A CSS grid is made up of rows and columns
A full row or column in a grid is called a grid track
The lines at which rows and columns meet are called grid line numbers
The cells formed when rows and columns meet are called grid cells
Grid cells can be combined to create larger grid areas, which function as containers/organizers for content elements (p, h1, img, etc.) or containing elements (div, header, main, aside, footer, nav, etc.)
1. come up with the design you want to build
2. sketch out a grid that would provide useful container elements for the design you want--so maybe like this...
...or something like this that could be adapted for use across several different pages...
.grid-container {
display: grid;
background-color:grey;
width: 80%;
height: 600px;
margin: 0 auto;
}<div class="grid-container">
</div>1. create a grid container with your chosen proportions and measurements
.grid-container {
display: grid;
grid-template-columns: 1fr 1.5fr 1fr;
grid-template-rows: 1fr 1fr 5fr auto;
background-color:grey;
width: 80%;
height: 100vh;
margin: 0 auto;
}<div class="grid-container">
</div>2. define columns and rows with the desired measurements/proportions to match your sketch (the lines that you see in the image to the right will be invisible, but they'll be there)
<div class="grid-container">
<header>
<div class="logo">
<img src="logo.jpg" alt="logo">
</div>
</header>
<div class="description">
<div class="intro-text">
<h2>
text text text
</h2>
</div>
</div>
<main>
<img class="big-img" alt="big image">
</main>
<footer>
<aside class="left-aside"></aside>
<nav class="subgrid-menu">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</nav>
<aside class="right-aside"></aside>
</footer>
</div>add grid items to the areas!
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
minmax(250px, 1fr)
= each column can grow, but don't let them get narrower than 250px
auto-fit
= create as many of these columns as needed/will fit
So the column sizes will automatically adapt to fit the screen at any size! But...
Elements designed to be fluid/adaptive will adjust their behaviors automatically to different screen sizes without your having to do anything more in the code.
E.g.,
But that isn't always enough! Sometimes you want to change something that can't be changed by this approach alone. More on that in a moment.
When you want to change something in a way that can't be accomplished by using fluid/adaptive methods alone, you can explicitly tell the CSS to change things at certain viewport sizes or when certain other conditions are met. These sizes/conditions are called breakpoints.
We do this using a media query.
@media media type and (media feature/condition: breakpoint) {
// CSS rules that kick in when the feature is present or the condition is met
}The CSS outside (usually before) the media query is the default.
When the viewport is at least 700px wide, the rules in the media query override the default rules.
So this media query changes the grid-template-columns value when the viewport is at least 700px wide.
.container {
display: grid;
grid-template-columns: 1fr;
}
@media (min-width: 700px) {
.container {
grid-template-columns: 200px 1fr;
}
}