Introduction To Cascading Style Sheet Part III
Introduction To Cascading Style Sheet Part III
Topics
Floating elements to the left and right
Relative positioning
Fixed positioning
Floating and Positioning
Floating an element moves it to the left or right and allows the
following text to wrap around it
Block elements stack up on top of one another and fill the available
width of the browser window or other containing element
Objects in the normal flow affect the layout of the objects around them
Floating
The float property moves an element as far as possible to the left or
right, allowing the following content to wrap around it
float
Default: none
Floating Example
img {
float: right;
}
Once you float an inline element, it follows the display rules for
block-level elements, and margins are rendered on all four sides
clear
Default: none
Positioning
Elements can be positioned
Default: static
Specifying Position
top, right, bottom, left
Default: auto
The value provided for each offset property defines the distance the
element should be moved away from that respective edge
Relative Positioning
Moves an element relative to its original spot in the flow
Overlap happens
Relative Positioning Example
em {
position: relative;
top: 2em; /* moves element down */
left: 3em; /* moves element right */
background-color: fuchsia;
}
Absolute Positioning
Absolutely positioned elements have no influence on the layout of
surrounding elements
In the first case the paragraph where the em element resides is not
positioned element but the second paragraph is positioned element
Absolute Positioning
The offset values apply to the outer edges of the element box (the outer
margin edge) for absolutely positioned elements
For example, the margins on all sides are maintained, even though
this is an inline element
z-axis
Default: auto
Stacking Order
The higher the number, the higher the element will appear in the stack
Lower numbers and negative values move the element lower in the
stack
The significant difference is that the offset values for fixed elements are
always relative to the viewport
The fixed positioned element stays put even when the rest of the page
scrolls
Fixed elements are often used for menus that stay in the same place at
the top, bottom, or side of a screen so they are always available, even
when the content scrolls
CSS Layout
Flexbox and Grid
Topics
When you make an element a flexbox, all of its child elements line up
on one axis
<div id="container">
<div class="box box1">1</div>
#container { <div class="box box2">2</div>
display: flex; <div class="box box3">3</div>
} <div class="box box4">4</div>
<div class="box box5">5</div>
</div>
Setting Up a Flexbox Container
You can turn any flex item into a flex container by setting its display to
flex, resulting in a nested flexbox
Specifying flow direction
flex-direction
Default: row
Flex Terminology
Flexbox is direction-agnostic, there are no references to “left,”
“right,” “top,” or “bottom” in the property values
Default: nowrap
Shorthand property flex-flow
flex-flow
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
}
Making a navigation bar with Flexbox
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
display: flex;
justify-content: center;
}
Making a navigation bar with Flexbox
nav ul li a {
display: block;
border: 1px solid;
border-radius: 0.5em;
padding: 0.5em 1em;
margin: 0.5em;
}
Aligning Items on the main axis
justify-content
Default: flex-start
#container {
display: flex;
justify-content: flex-start;
}
Aligning on the cross axis
align-items
Default: stretch
#container {
display: flex;
flex-direction: row;
height: 200px;
align-items: flex-start;
}
Aligning on the cross axis
If you’d like one or more items to override the cross-axis setting, use
the align-self property on the individual item element(s)
align-self
Default: stretch
Aligning multiple lines
align-content
The flex property, which specifies how much an item can grow and
shrink, and identifies its starting size
flex
Default: 0 1 auto
Determining How Items “Flex” in the Container
Determining How Items “Flex” in the Container
Changing the Order of Flex Items
order
.box3 {
Values: integer
order: 1;
Default: 0 }
Changing the Order of Flex Items
.box2,
order
.box3 {
Values: integer
order: 1;
Default: 0 }
Changing the Order of Flex Items
order
.box5 {
Values: integer
order: -1;
Default: 0 }
Applying Flexbox
How do you create the layout shown in the next slide
<header>…</header>
<main>
<article><h2>Where It's At</h2></article>
<aside id="news"><h2>News></h2></aside>
<aside id="contact"><h2>Contact</h2><aside>
</main>
<footer>…<footer>
main { #news {
display: flex; flex: 1 1 25%;
} order: 3;
}
article { #contact {
flex: 1 1 50%; flex: 1 1 25%;
order: 2; order: 1;
} }
CSS Grid Layout
The CSS Grid Layout Module provides a system for laying out elements
in rows and columns
How to use Grid Layout
Use the display property to turn an element into a grid container
You can set them up explicitly and/or provide directions for how rows
and columns should get created on the fly
If you don’t assign them explicitly, they flow into the cells sequentially
Grid Terminology
To make an element a grid container apply the display: grid property
Grid Track is a
generic name
for a grid
column or a
grid row
Declaring Grid Display
To turn an element into a grid container, set its display property to grid
or inline-grid
<div id="layout">
<div id="one">One</div>
#layout { <div id="two">Two</div>
display: grid; <div id="three">Three</div>
} <div id="four">Four</div>
<div id="five">Five</div>
</div>
Setting Up the Grid
Let us how to setup the grid shown
Defining grid tracks
grid-template-rows
grid-template-columns
Default: none
Grid track sizes
#layout {
display: grid;
grid-template-rows: 100px 400px 100px;
grid-template-columns: 200px 500px 200px;
}
#layout {
display: grid;
Grid track sizes
grid-template-rows: 100px 400px 100px;
grid-template-columns: 200px 500px 200px;
}
Grid track sizes
When the browser creates a grid, it also automatically assigns each
grid line a number that you can reference when positioning items
The grid line at the start of the grid track is 1, and lines are
numbered sequentially
The lines are numbered from the end of tracks as well, starting with –1,
and numbers count back from there (–2, –3, etc.)
#layout {
display: grid;
grid-template-rows: [header-start] 100px [content-start] 400px
[footer-start] 100px;
grid-template-columns: [ads] 200px [main] 500px [links] 200px;
}
The grid line at the top of the grid can now be referred to as “header-
start,” “1,” or “–4.”
Grid track sizes
To give a line more than one name, just include all the names in the
brackets, separated by spaces
#layout {
display: grid;
grid-template-rows: [header-start] 100px [header-end content-start]
400px [footer-start] 100px;
grid-template-columns: [ads] 200px [main] 500px [links] 200px;
}
Specifying track size values
The Grid specification provides the following values for the grid-
template-* properties:
fit-content()
Specifying track size values
Fractional units (flex factor)
#layout {
display: grid;
grid-template-rows: 100px 400px 100px;
grid-template-columns: 200px 1fr 200px;
}
Specifying track size values
Fractional units (flex factor)
Specifying track size values
Fractional units (flex factor)
The fr unit is great for combining fixed and flexible track widths,
but it also be used to give all the columns proportional widths
#layout {
display: grid;
grid-template-rows: 100px 400px 100px;
grid-template-columns: 1fr 2fr 1fr;
}
Specifying track size values
Minimum and maximum size range
You can constrict the size range of a track by setting its minimum
and maximum widths using the minmax() function in place of a
specific track size
#layout {
display: grid;
grid-template-rows: 100px 400px 100px;
grid-template-columns: 1px minmax(15em, 45em) 1px;
}
Specifying track size values
Content-based sizing
#layout {
display: grid;
grid-template-rows: 100px 400px 100px;
grid-template-columns: 1px min-content 1px;
}
Specifying track size values
Repeating track sizes
You can also let the browser figure it out itself based on the
available space by using the auto-fill and auto-fit values
instead of an integer
grid-template-areas
Default: none
Defining Grid Areas
#layout {
display: grid;
grid-template-rows: 100px 400px 100px;
grid-template-columns: 200px 1fr 200px;
grid-template-areas:
"header header header"
"ads main links"
"footer footer footer";
}
Defining Grid Areas
Defining Grid Areas
If there are three columns in the grid, there must be three names
provided for each row
If you want to leave a cell unnamed, type one or more periods (.) in its
place as a space holder so that every cell is still accounted for
The grid shorthand property
Use the grid shorthand property to set values for grid-template-rows,
grid-template-columns, and grid-template-areas with one style rule
grid
Default: none
The grid shorthand property
grid
Default: none
#layout {
display: grid;
grid: 100px 400px 100px / 200px 1fr 200px;
}
The grid shorthand property
To include custom line names, add the names in brackets around their
respective tracks
Area names should appear with the row information as shown below
[start line name] "area names" <track size> [end line name]
The grid shorthand property
The expanded equivalent of the following is shown in the next slide
#layout {
display: grid;
grid:
[header-start] "header header header" 100px
[content-start] "ads main links" 400px
[footer-start] "footer footer footer" 100px
/ [ads] 200px [main] 1fr [links] 200px;
}
The grid shorthand property
The expanded equivalent of the example is shown in the previous slide
#layout {
display: grid;
grid-template-rows: [header-start] 100px [content-start] 400px [footer-
start] 100px;
grid-template-columns: [ads] 200px [main] 1fr [links] 200px;
grid-template-areas:
"header header header"
"ads main links"
"footer footer footer";
}
Placing Grid Items
Without any explicit placement instruction, grid items flow into the
available grid cells sequentially
Positioning using lines
One method for describing a grid item’s location on the grid is to
specify the four lines bordering the target grid area with four
properties that specify the start and end row lines and the start and
end column lines
Positioning using lines
grid-row-start
grid-row-end
grid-column-start
grid-column-end
Default: auto
Positioning using lines
#one {
grid-row-start: 1;
grid-row-end: 2;
grid-column-start: 1;
grid-column-end: 4;
}
Positioning using lines
Using name names is also possible
#one {
grid-row-start: header-start;
grid-row-end: header-end;
}
Positioning using lines
You can use the span keyword to specify how many tracks to span over.
#one {
grid-column-start: 1;
grid-column-end: span 3;
}
#one {
grid-column-start: span 3;
grid-column-end: -1;
}
Positioning using lines
Shorthand grid-row and grid-column properties
grid-row
grid-column
#one {
grid-row: 1 / 2;
grid-column: 1 / span 3;
}
Positioning by area
grid-area
#one {
grid-area: 1 / 1 / 2 / span 3;
/* row-start / column-start / row-end / column-end */
}
Implicit Grid Behavior
Without explicit placement instructions, grid items flow into the grid
sequentially
Creating a named area implicitly generates grid lines with the “-start”
and “-end” suffixes
if you simply have more items than there are cells or areas, the browser
generates more tracks until all the items are placed
Implicit Grid Behavior
By default, any row or column automatically added to a grid will have
the size auto, sized just large enough to accommodate the height or
width of the contents
If you want to give implicit rows and columns specific dimensions, use
the grid-auto-* properties
Implicit Grid Behavior
grid-auto-rows
grid-auto-columns
Default: auto
The following styles set up a grid with explicit columns and as many
200px-high rows as needed
Default: row
Flow direction and density
Use grid-auto-flow to specify whether you’d like items to flow in by
row or column
The default flow follows the writing direction of the document (left-to-
right and top-to-bottom for English and other left-to-right languages)
#listings {
display: grid;
grid-auto-flow: column;
}
Flow direction and density
By default, items are placed in the first area in which they fit
Cells that are too small to accommodate the content will be skipped
over until a cell large enough is found for placement
The dense keyword for the grid-auto-flow property make grid items
fill the grid as densely as possible, allowing the items to appear out of
sequence in order to fill the available space
#listings {
display: grid;
grid-auto-flow: dense rows;
}
Spacing and Alignment
You can add space between tracks and adjust alignment of the grid and
its items
Spacing between tracks (gutters)
grid-row-gap
grid-column-gap
Default: 0
Spacing between tracks (gutters)
grid-gap
Default: 0 0
div#container {
border: 2px solid gray;
display: grid;
grid: repeat(4, 150px) / repeat(4, 1fr);
grid-gap: 20px 50px;
}
Aligning individual items
justify-self
You can decide how the browser should handle leftover space within the
container by using the
Default: start
Aligning tracks in the grid container
align-content
Default: start
A Grid Layout Example
A Grid Layout Example
The HTML
<div id="container">
<header>…</header>
<main>…</main>
<aside>…</aside>
<footer>…</footer>
</div>
A Grid Layout Example
Grid Container
#container {
display: grid;
}
A Grid Layout Example
The Rows
#container {
display: grid;
grid-template-rows: auto
auto 5em;
}
A Grid Layout Example
The Columns
#container {
display: grid;
grid-template-rows: auto
auto 5em;
grid-template-columns:
minmax(25em, 1fr) 16em;
}
A Grid Layout Example
Naming Areas
#container {
...
grid-template-areas:
"banner banner"
"main hours"
"footer footer";
}
A Grid Layout Example
Placing header Grid Items
header {
grid-area: banner;
}
A Grid Layout Example
Placing main Grid Items
main {
grid-area: main;
}
A Grid Layout Example
Placing aside Grid Items
aside {
grid-area: aside;
}
A Grid Layout Example
Placing footer Grid Items
footer {
grid-area: aside;
}
Must Visit References
A Complete Guide to Flexbox