CSS_2(Grid&Flex)
CSS_2(Grid&Flex)
22
Icebreaker questions
1. ___background-image____ provides location of an image to use as
background
2. ____text-decoration_______ specifies underlines, over lines and
line-through
3. _background-position___ specifies the location of the origin
background image
4. __background-repeat__ specifies whether and how a background image
repeats
5. ____text-transform_______ changes the capitalization of text
6. ____Opacity_______ specifies transparency level of elements.
7. ____font-family_______ specifies a typeface of a text
8. ____font-weight_______ specifies the level of boldness of a font
23
⚫ You have a flex container with flex-direction:
row, justify-content: center, and three items
inside it. One of the items has flex-grow: 1, while
the others have flex-grow: 0. What will be the
layout of the items, and why?
a) The items will be centered, and the one with flex-grow: 1
will expand to fill the remaining space
b) All items will be equally spaced, with the center item
larger than the others
c) All items will have the same width because
justify-content overrides flex-grow.
d) The items will align to the left since only one item has
flex-grow.
⚫ If you have a flex container with flex-direction: column,
align-items: center, and three flex items, how will the
items be positioned?
a) The items will be centered vertically and horizontally
within the container
b) The items will be aligned to the center horizontally but
stacked from top to bottom
c) The items will be stacked horizontally with space between
each item
d) The items will stretch across the container vertically
⚫ In a flex container with align-items: flex-start and
flex-direction: column, what effect will setting align-self:
center on one item have?
27
Important CSS Grid terminology
1. Grid Container: the element on which display: grid is
applied. It’s the direct parent of all the grid items.
2. Grid Item: the children (i.e. direct descendants) of the
grid container.
3. Grid Line: the horizontal and vertical dividing lines of
the grid
4. Grid Cell: it’s a single “unit” of the grid
5. Grid Track: the space between two adjacent grid lines.
which is a generic name for a grid column and a grid row.
6. Grid Area: it’s a rectangular area made up of one or more
adjacent grid cells
28
The parts of a CSS grid
29
Takeaway question
⚫ Can you differentiate between key components in CSS Grid
such as
⚫ the Grid Container,
⚫ Grid Item,
⚫ Grid Line,
⚫ Grid Cell,
⚫ Grid Track, and
⚫ Grid Area
30
CSS grid layout has the following features:
⚫ Fixed and/or flexible track sizes
⚫ Ability to position items precisely using line numbers,
names, or by targeting a grid area
⚫ Flexibility to add rows and columns as needed
⚫ Control over item alignment within a grid area and the
overall alignment of the entire grid
31
Grid Container
⚫ The first step to start using Grid is to create a grid
container.
⚫ This is done by specifying a container element as a
grid through "display: grid"
⚫ Followed by specifying ,
⚫ how many rows and columns we want and
⚫ how wide they should be
⚫ Subsequently, positioning the child elements within the
grid
32
Defining Grid track
⚫ grid-template-columns and grid-template-rows define
the columns and rows of a grid with a space-separated
list of values.
⚫ The list values represent the track size and the number
of values determines the number of rows or columns.
⚫ grid-template-rows, grid-template-columns
⚫ Values: none | list of track sizes and optional line names
⚫ Default: none
⚫ Applies to: grid containers
⚫ Inherits: no
33
Example
⚫ #layout {
⚫ display: grid;
⚫ grid-template-rows: 100px 400px 100px;
⚫ grid-template-columns: 200px 500px 200px;
⚫}
34
Takeaway question
1. What is the initial step to take to use CSS Grid?
2. How do grid-template-columns and
grid-template-rows in CSS help define a grid layout?
3. How do we determine the number of rows and
columns in the grid?
4. Write a CSS code snippet for a grid container with
three columns, each equal to 50 pixel, and two rows
where the first row is 100 pixels tall, and the second
row is twice the height of the first row.
35
Sizing Grid tracks
⚫ In CSS Grid Layout, grid tracks refer to the rows and
columns that make up the grid.
⚫ It’s possible to specify the size of these tracks using
various units and values using,
⚫ Flexible Sizes with Fractional Units
⚫ Minmax Function
⚫ Repeating track sizes
36
Fractional units (fr)
⚫ The Grid specific fractional unit (fr) allows us to create
track widths that expand and contract depending on
available space.
⚫ The fr unit represents a fraction of the available space.
⚫ Example,
⚫ .container {
⚫ display: grid;
⚫ grid-template-rows: 1fr 2fr;
⚫ grid-template-columns: 1fr 2fr; }
⚫ Both rows and columns are set to take up one part and
two parts of the available space, respectively.
37
Takeaway questions
1. What do grid tracks refer to? and list the different
methods to specify their sizes
2. Create a CSS Grid with three columns, where the first
column is twice as wide as the other two.
3. Create a CSS Grid layout with four columns. Set the first
and last columns to be three times wider than the middle
two columns.
4. Create a CSS Grid with five columns, where the first and
last columns are half the width of the middle three
columns.
38
Minimum and maximum size range
⚫ It is possible to restrict the size range of a track by setting its
minimum and maximum widths
⚫ The minmax function allows us to specify a size range for
grid tracks.
⚫ The function takes two arguments: minimum and maximum
size
⚫ Example,
⚫ grid-template-columns: 200px minmax(15em, 45em) 200px;
⚫ This rule sets the middle column to a width that is at least
15em but never wider than 45em.
⚫ N.B., fr units are not permitted as the minimum value in a
minmax() function
39
Repeating track sizes
⚫ In CSS Grid Layout, the repeat function is used when
creating a grid with consistent layout without having to
list each size individually. Like this,
⚫ grid-template-columns: 20px 1fr 20px 1fr 20px 1fr 20px
1fr 20px 1fr;
⚫ The pattern “20px 1fr” repeats five times, but it can be
written using the repeat function,
⚫ grid-template-columns: repeat(5, 20px 1fr);
⚫ It takes two arguments: the number of repetitions and
the size of each repetition
40
Takeaway question
⚫ What are the two arguments of the repeat function?
⚫ Create a CSS Grid layout where the pattern "30px 2fr"
repeats three times. (how many columns will it have?)
⚫ Create a CSS Grid layout Implement a repeating pattern
where the first two columns have a fixed width of 40 pixels,
followed by three columns with a flexible width of 1fr, and
then repeat this pattern twice.
41
Grid lines and names
⚫ When the browser creates a grid, it also automatically
assigns each grid line a number that can be referenced when
positioning items.
⚫ The grid line at the start of the grid track is 1, and lines are
numbered sequentially from there.
⚫ The lines are numbered from the end of tracks as well,
starting with –1, and numbers count back from there
42
Grid line cont’d
⚫ Grid names are user-defined names for lines in the grid.
⚫ Grid names are incorporated within square brackets,
indicating their position relative to the tracks.
⚫ Example,
⚫ #layout {
⚫ display: grid;
⚫ grid-template-rows: [header] 100px [content] 400px [footer]
90px;
⚫ grid-template-columns: [ads] 200px [main] 500px [links]
200px;
⚫ }
43
Takeaway questions
1. How does the browser assign numbers to grid lines, and
how does it start?
2. Can you explain how grid lines are numbered from the
end of the grid tracks and how does it start?
3. What are grid names, and how are they introduced within
the grid?
4. Write a CSS code snippet for a grid container with 2
columns, each equal to 50 pixel, and two rows each equal
to 40 pixels.
⚫ The grid should have explicit column and row names such as
"left," "center," and "right" for columns, and "top" and
"bottom" for rows.
44
Positioning grid items
⚫ A grid item’s location can be determined by
referring to specific grid lines using the following
CSS grid properties,
⚫ grid-column-start, grid-column-end, grid-row-start,
grid-row-end
⚫ Values: line | name | span <number> | span <name> | auto;
⚫ Default: none
⚫ Applies to: grid items
45
Example,
⚫ .container { ⚫ .box2 {
display: grid; column-start: 1;
grid-template-columns: row-start: 3;
repeat(3, 1fr); row-end: 5;}
grid-auto-rows: 100px;}
⚫ .box1 {
column-start: 1;
column-end: 4;
row-start: 1;
row-end: 3; }
46
Takeaway question
⚫ Create a CSS Grid layout with three columns and two
rows.
⚫ Place a grid item in the center column and second row
using the necessary properties.
⚫ Ensure the item spans two columns and one row.
47
Gutters
⚫ Gutters (or alleys) between grid cells can be created
using the column-gap and row-gap properties.
⚫ They are properties which specify the size of the grid
lines.
⚫ Example,
⚫ .container {
⚫ grid-template-columns: 100px 50px 100px;
⚫ grid-template-rows: 80px auto 80px;
⚫ column-gap: 10px;
⚫ row-gap: 15px;
⚫}
48
Takeaway question
⚫ Create a CSS Grid layout with 10 columns each equal
to 20px and three rows each 40px.
⚫ Set the gutters 15 pixels wide for columns and 10
pixels high for rows.
49
Defining grid areas (reading assignment)
⚫ In CSS Grid Layout, grid areas allow us to define named
areas within the grid and place items into the area by
referencing the names.
⚫ To define grid areas, we use the grid-template-areas
property within the grid container
⚫ grid-template-areas
⚫ Values: none | series of area names
⚫ Default: none
⚫ Applies to: grid containers
⚫ Inherits: no
50
Example,
⚫ The .container creates a grid with three rows and two
columns.
⚫ The grid-template-areas property defines named areas for
each cell in the grid.
⚫ .container {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-rows: 100px 1fr 50px;
grid-template-columns: 200px 1fr; }
51
CSS Grid Properties
⚫ Parent container
● Display: grid
● Grid-template-rows
● Grid-template-columns
● Grid-row-gap
● Grid-column-gap
● Grid-auto-rows
● Justify-items: start | end | center | stretch
● Align-items: start | end | center | stretch
● Justify-content: start | end | center | space-between |
space-around
● Align-content
52
Cont.…..
⚫ Child properties
⚫ grid-column-start
⚫ grid-column-end
⚫ grid-row-start
⚫ grid-row-end
⚫ grid-column
⚫ grid-row
⚫ grid-area
⚫ justify-self
⚫ align-self
⚫ place-self
53
Additional resource
⚫ online games for learning Flexbox and grid
⚫ https://flexboxfroggy.com/
⚫ https://cssgridgarden.com/
54
End of chapter four
55