Computer Science 318


Fundamentals of Web Design

Lab 14 Display Grid


Goal

Learn

body{
    display:grid;
}

display:grid;

Defines the element as a grid container and establishes a new grid formatting context for its contents. Display Grid is a layout system available in CSS. Unlike Display Flex which only allows control for either rows or columns, Display Grid allows the user to control both rows and columns.

body{
     display:grid;
     grid-template-columns: 40px 15% auto 10rem;
     grid-template-rows:25% 100px auto;
}

grid-template-rows

Defines the rows of the grid with a space-separated list of values. The values represent the track size, and the space between them represents the grid line.

grid-template-columns

Defines the columns of the grid with a space-separated list of values. The values represent the track size, and the space between them represents the grid line.

Values: can be a length, a percentage, or a fraction of the free space in the grid (using the fr unit).

Below is a representation of the display grid properties and values from the code above. The white outline is the edge of the <body> and the white vertical and horizontal lines represent the start and end of each column or row. The green values shown along the bottom and right are the numbers assigned to each horizontal and vertical line that create the grid. These numbers can be referenced with grid-item properties to move and stretch the elements within different cells in the grid.

Display Grid Visual Layout

gap

body{
     display:grid;
     grid-template-columns: 40px 15% auto 10rem;
     grid-template-rows:25% 100px auto;
     gap: 1rem;
}

Defines the gaps (gutters) between rows and columns.

Grid-Item Properties

By default any elements that are direct descendants of the grid container will be assigned a grid cell. The will be ordered from left to right, wrapping to a new row as needed. The placement of individual elements can be changed with the following properties:

.item1{
    grid-column-start:1;
    grid-column-end:4;
    grid-row-start:1;
    grid-row-end:3;
}

Hint: When using the grid-item properties to move elements around there is not a unit assigned.

The properties above define a grid item's location within the grid by referring to specific grid lines (the green numbers in the image above). So if the <body> had a four <div> each with a class such as item-1, item-2, etc... We could use grid-row-start, grid-column-start, grid-row-end, and grid-column-end to move the elements into grid cells they were initially assigned to or multiple cells. Like below:

Display grid - grid-area long hand

.item4{
     grid-column:1/4;
     grid-row:3/4;
}

The code above is a shorter version of the grid-item properties above. When using grid-row and grid-column the grid line values are separated by a slash. The values also go in this order: start/end.

Display grid - grid-area med hand

.item-2{
    grid-area: 1/4/3/5;
}

The code above is the shortest version of the grid-items properties. The values for grid-area are: row-start/column-start/row-end/column-end.

Display Grid - grid-area short hand

Practice

Experiment Try moving the items around the grid using the image above as a guide. Use grid-area, grid-column, grid-row, grid-row-start, grid-column-start, grid-row-end, or grid-column-end to move the items.

HTML

<div class="item-1">Item 1</div> <div class="item-2">Item 2</div> <div class="item-3">Item 3</div> <div class="item-4">Item 4</div>

CSS

.item-1{ background-color:aqua; } .item-2{ background-color:red; } .item-3{ background-color:yellow; } .item-4{ background-color:blue; } body{ display:grid; grid-template-columns: 40px 15% auto 10rem; grid-template-rows:25% 100px auto; color:white; background-color:black; }

Preview

Complete Grid Garden

Open GitHub Desktop (you may need to download the program again if the computers get wiped.)

Couple of things to check in GitHub Desktop.

With your cs-318 repository selected, Click open in Visual Studio code.

Create a new folder called lab14

Create a new files called index.html, nine-worlds.html, and ragnarok.html in the lab14 folder

Setup your HTML files

Create your stylesheet

Link your stylesheet to the HTML

Change Document in the <title> element to "Norse Mythology"

Add a <header>, <div class="sidenav"> and <main> on every document

All HTML documents will have the same header

Add a <h1> "Norse Mythology"

Add a <nav> with three <a>

<a> setup.

Lab 13 Header

Index.html

In the <div> add eight <a> with the following content:

In the <main> add 10 <section>

Section 1

Add class="title"

h2 "The Norse Gods & Goddesses"

Section 2

h3 "A Guide to Norse Gods and Goddesses"

Add a div with three paragraphs

Section 3

Add an id to the section with the value "odin"

h3 "Odin"

h4 "(from Old Norse: Odinn)"

Add a paragraph

Section 4

Add an id to the section with the value "thor"

h3 "Thor"

h4 "(from Old Norse: Pórr)"

Add a paragraph

Section 5

Add an id to the section with the value "loki"

h3 "Loki"

h4 "(Old Norse: loki)"

Add a paragraph

Section 6

Add an id to the section with the value "frigg"

h3 "Frigg"

h4 "(From Old High German Frija)"

Add a paragraph

Section 7

Add an id to the section with the value "baldr"

h3 "Baldr"

h4 "(Old Norse:baldz)"

Add a paragraph

Section 8

Add an id to the section with the value "freyja"

h3 "Freyja"

h4 "(Old Norse for "the Lady")"

Add a paragraph

Section 9

Add an id to the section with the value "freyr"

h3 "Freyr"

h4 "(Old Norse: 'Lord')"

Add a paragraph

Section 10

Add an id to the section with the value "heimdallr"

h3 "Heimdallr"

h4 "(from Old Norse Heimdallr)"

Add a paragraph

Add the corresponding svg (provided in the Lab 14 Canvas assignment) to each god section after the h3.

Connect the <a> in the sidenav with the corresponding id for each section.

Lab 14 index html

Nine-worlds.html

In the div add ten <a>

In the <main> add twelve <section>

Section 1

Add class="title"

h2 "The Nine Worlds & Yggdrasil"

Section 2

h3 "Völuspá"

h4 "Stanzas 1-5"

Add five <p>, in each paragraph you will need to use <br> to create the line breaks in the stanzas.

Section 3

Add id of "yggdrasil"

h3 "Yggdrasil"

p

Section 4

Add id of "asgard"

h3 "Asgard"

p

Section 5

Add id of "vanaheimr"

h3 "Vanaheimr"

p

Section 6

Add id of "alfheim"

h3 "Alfheim"

p

Section 7

Add id of "midgard"

h3 "Midgard"

p

Section 8

Add id of "jotunheimr"

h3 "Jötunheimr"

p

Section 9

Add id of "muspelheim"

h3 "Muspelheim"

p

Section 10

Add id of "svartalfar"

h3 "Svart¡lfar"

p

Section 11

Add id of "niflheim"

h3 "Niflheim"

p

Section 12

Add id of "nidavellir"

h3 "Nidavellir"

p

Add the corresponding svg (provided in canvas assignment) to each realm section before the h3.

Connect the <a> in the sidenav with the corresponding id for each section.

Lab 14 nine worlds html

Ragnarok.html

In the <div> add two <a>

On the <main> add class=ragnarok

In the <main> add six <section>

Section 1

add id="ragnarok" and class="title"

h2 "Ragnarök"

Section 2

h3 "Völuspá"

h4 "Stanzas 40-58"

Add 18 <p>, in each paragraph you will need to use <br> to create the line breaks in the stanzas.

Section 3

h3 "Ragnarök"

p

Section 4

Add id="valhalla" and class="title"

h2 "Valhalla"

Section 5

h3 "Völuspá"

h4 "Stanzas 1-5"

Add five <p>, in each paragraph you will need to use <br> to create the line breaks in the stanzas.

Section 6

h3 "Valhalla"

p

Add the corresponding svg (provided in canvas assignment) before the h3 for Ragnarok and Vallhala.

Connect the <a> in the sidenav with the corresponding id for each section.

Lab 14 ragnarok html

In the <head> add the following code to link to two different Google Fonts we will be using

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Cinzel+Decorative:wght@900&family=EB+Garamond:wght@400;800&display=swap" rel="stylesheet">

CSS

Set your browser width to about 500px

body

background color #330202

font color #fff

font-family: 'EB Garamond', serif;

margin 0

h1, h2

font-family: 'Cinzel Decorative', cursive;

font color #d9ba6c

Lab 14 body and heading css

header

background color #161d2d

padding 1rem

nav>a

font color #d9ba6c

border 1px solid #d9ba6c

text-decoration none

padding 1rem

font-weight 800

display inline-block.
Hint: Changing from inline to inline-block will let the margin on the top and bottom of the anchors take effect

margin 1rem 1rem 1rem 0.
Hint: When using four values for margin the values apply in this order: top | right | bottom | left

nav>a:hover

border-color transparent

background color #d9ba6c

font color #161d2d

Lab 14 header styles

.sidenav

Background color #374a76

padding .5rem 1rem

.sidenav>a

background color #fae2c0

font color #0f0f0f

padding .5rem 1rem

display inline-block

margin .5rem .5rem 0 0

text-decoration none

.sidenav>a:hover

opacity: .8;

Lab 14 sidenav mobile styles

main

padding 1rem

section

padding 1rem

margin 1rem 0

border 1px solid #d9ba6c

.title

margin 0

padding 2rem 1rem

background color #5f1818

h4

margin 0

Lab 14 title css

section

display grid

grid-template-columns 12rem 1fr

Lab 14 section grid css

section:nth-child(2)

Hint: This will select the second section with out using a class

display block

Lab 14 second section css

section>img

max-height 10rem

Lab 14 section grid lines

Using the guide above and grid-item properties below to achieve the arrangement of the image, h3, h4 and paragraph in the section.

Grid-Item Properties:

Hint: You don't need to use all of the properties above.

Lab 14 section grid-area css

If you look at the Ragnarok page some of the section look terrible. Let's fix that with some CSS.

Lab 14 Ragnarok section 5 looks terrible

.ragnarok section:nth-child(5)

display block

Lab 14 Ragnarok section 5 looks great

Desktop CSS

Add a media query for a minimum width of 800px

All CSS from here on will be in the media query

main

display grid

grid-template-columns 1fr 1fr

gap 1rem

.title

Use grid-column to make the title span the two column in the grid.

section:nth-child(2)

Use grid-column to make the information section span the two column in the grid.

Lab 14 main 800px css

.ragnarok

display block

Lab 14 Ragnarok display block

Add a media query for a minimum width of 1100px

body

display grid

grid-template-columns 20rem auto

Lab 14 body display grid css

Lab 14 display grid - grid guide

Using the guide above and grid-item properties to achieve the arrangement of the header, sidenav, and main based on the image below.

Grid-Item Properties:

Hint: You don't need to use all of the properties above.

Lab 14 header grid-area css

header

display flex

use justify-content with one of the following values to achieve the layout pictured below

Lab 14 display flex header

.sidenav>a

display block.
Hint: This will force the links to take up the full width of the sidenav container and stack top to bottom, rather than left to right.

text align right

Lab 14 sidenav desktop styles

Final Steps

Validate your HTML.

Lint your CSS at Jigsaw.w3.org

Now look at GitHub Desktop. You should see all of your files listed out in the left column.

GitHub Desktop files listed

Now you are going to commit your changes.

With all of the files checked. Fill out the Summary field in the bottom left and then click Commit to main.
Committing is staging the files, so they can be pushed to the repository hosted at GitHub.

GitHub Desktop Summary field.

Now you want to push your changes to the Repository. Now if you access the repository from another computer Lab14 will be there. After you push in the top right of the nav it should say "Fetch Origin" instead of "Push Origin"

GitHub Push Origin

Your are Done with Lab15