CIS195 Web Authoring 1: HTML

Using the CSS Grid for Layout

Topics by Week for the Ten-Week Term
1. Intro to HTML 6. Layout with CSS
2. More HTML, file paths 7. CSS Flexbox and Grid
3. Site structure and navigation 8. HTML Forms
4. Formatting with CSS 9. Multimedia
5. Midterm, Project Propposal 10. Tables, Project Completion
11. Final

 

Contents

Q and A

Grid Container Elements

The CSS grid is used to position elements on a page.

For example, this HTML code:

Will be displayed like this on the web page:

Item 1 Item 2 Item 3

Item 4 Item 5 Item 6

Item 7 Item 8 Item 9

Note that the three auto values in grid-template-columns: auto auto auto are what specify the three columns and that their width is automatically determined.

Grid Rows and Columns

Example:

 

Grid Lines and Gaps

A gap is like a combination of padding and margin in the CSS box model; it's just the space between rows or columns.

An element can be made to span multiple columns using the column lines as a reference; for example:

An element can be made to span multiple rows using the row lines as a reference; for example:

We can apply these CSS rules like this:

Which will look like this on the web page:

Item 1 Item 2

Item 3 Item 4 Item 5

Item 6 Item 7

Item 8 Item 9

 

Grid Template Areas

The grid-template-areas property in CSS is used to position structural elements within a grid layout. Each element to be positioned must be given a grid-area name. The grid-template-areas property is then used to arrange the named elements into rows and columns like this:

In this example, the div is a grid container that contains a grid with six areas that are specified by the grid-template-areas CSS property. The grid items are placed in the grid container using the grid-area CSS property. The grid items are not placed in the grid container in the order they appear in the HTML. Their order is determined by the order of area names in the grid-template-areas CSS property.

Each grid-area name should only be applied to one element. Note that the grid-template-areas names don't need to match the element names.

 

Example Code

Open the example web page in your browser and use the View Source feature to look at the source code.

CSS Grid Examples

 

Reference

CSS Grid Tutorial by W3schools

CSS Grid in The CSS Handbook by Flavio Copes

Grids in MDN Web Docs

 


Creative Commons License Web Authoring Lecture Notes, written by Brian Bird in , are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


1 The CSS2 property grid-gap, is now deprecated and replaced by the CSS3 property gap.