web development -2
HTML5, CSS3, and JavaScript
6th Edition
Designing a Page Layout
Tutorial 3
Carey
XP
XP
XP
XP
XP
Objectives
Create a reset style sheet
Explore page layout designs
Center a block element
Create a floating element
Clear a floating layout
Prevent container collapse
Explore grid-based layouts
2
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
2
Objectives (continued)
Create a layout grid
Format a grid
Explore the CSS grid styles
Explore positioning styles
Work with relative positioning
Work with absolute positioning
Work with overflow content
3
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Page Layout with Floating Elements
4
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
4
Introducing the display Style
HTML elements are classified into
Block elements, such as paragraphs or headings
Inline elements, such as emphasized text or inline images
The display style can be defined for any page element using
display: type;
where type defines the display type
5
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Introducing the display Style (continued)
6
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
6
Creating a Reset Style Sheet
Reset style sheet supersedes a browser’s default styles and provides a consistent starting point for page design
The first style rule in a sheet is the display property used to display HTML5 structural elements
7
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
7
Creating a Reset Style Sheet (continued)
8
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
8
Exploring Page Layout Designs
Web page layouts fall into three categories:
Fixed layout – Size of the page and page elements are fixed, usually using pixels as the unit of measure
Fluid layout – The width of the page elements are set as a percent of the available screen width
Elastic layout – Images and text are always sized in proportion to each other in em units
9
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Exploring Page Layout Designs (continued)
Responsive design – The layout and design of a page changes in response to the device that is rendering it
10
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
10
Working with Width and Height
The width and height of an element are set using the following properties:
width: value;
height: value;
where value is the width or height using one of the CSS units of measurement or as a percentage of the width or height of the parent element
11
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Working with Width and Height (continued)
12
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
12
Centering a Block Element
Block elements can be centered horizontally within their parent element by setting both the left and right margins to auto
body {
margin-left: auto;
margin-right: auto;
}
13
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Vertical Centering (continued)
Centering an element vertically can be accomplished by displaying the parent element as a table cell and setting the vertical-align property to middle
For example, to vertically center the following h1 heading within the div element:
<div>
<h1>Pandaisia Chocololates</h1>
</div>
14
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Vertical Centering
Apply the style rule
div {
height: 40px;
display: table-cell;
vertical-align: middle;
}
Using this style rule, the h1 heading will be vertically centered
15
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Floating Page Content
Floating an element takes it out of position and places it along the left or right side of its parent element
To float an element, apply
float: position;
where position is none (the default), left to float the object on the left margin or right to float the object on the right margin
16
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Floating Page Content (continued 1)
For elements to be placed within a single row, the combined width of the elements cannot exceed the total width of their parent element
17
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
17
Floating Page Content (continued 2)
18
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
18
Clearing a Float
To ensure that an element is always displayed below floated elements, use
clear: position;
where position is left, right, both, or none
19
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Clearing a Float (continued 1)
left – Displays the element only when the left margin is clear of floating objects
right – Displays the element only when the right margin is clear of floating objects
both – Displays the element only when both margins are clear of floats
none – Displays the element alongside any floated objects
20
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Clearing a Float (continued 2)
21
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
21
Clearing a Float (continued 3)
22
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
22
Clearing a Float (continued 4)
23
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
23
Refining a Floated Layout
Content box model – The width property refers to the width of an element content only
Additional space include padding or borders
Border box model – The width property is based on the sum of the content, padding, and border spaces
Additional space taken up by the padding and border is subtracted from space given to the content
24
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Refining a Floated Layout (continued 1)
The layout model can be chosen using
box-sizing: type;
where type is content-box (the default), border-box, or inherit (to inherit the property defined for the element’s container)
25
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Refining a Floated Layout (continued 2)
26
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
26
Working with Container Collapse
Container collapse – An empty container with no content
Elements in the container are floated
27
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
27
Working with Container Collapse (continued 1)
Use the after pseudo-element to add a placeholder element after the footer
The general style rule is
container::after {
clear: both;
content: “”;
display: table;
}
where container is the selector for the element containing floating objects
28
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Working with Container Collapse (continued 2)
The clear property keeps the placeholder element from being inserted until both margins are clear of floats
The element itself is a web table and contains an empty text string
29
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Page Layout Grids
30
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
30
Overview of Grid-Based Layouts
Rows and columns form a grid
The number of rows is based on the page content
The number of columns is based on the number that provides the most flexibility in laying out the page content
31
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
31
Overview of Grid-Based Layouts (continued 1)
32
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
32
Overview of Grid-Based Layouts (continued 2)
Advantages of using a grid:
Grids add order to the presentation of page content
A consistent logical design gives readers the confidence to find the information they seek
It is easily accessible for users with disabilities and special needs
It increases the development speed with a systematic framework for the page layout
33
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Fixed and Fluid Grids
Fixed grids – Every column has a fixed position
Widths of the columns and margins are specified in pixels
Fluid grids – Provides more support across different devices with different screen sizes.
Column width is expressed in percentages
34
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
CSS Frameworks
A framework is a software package that provides a library of tools to design a website
Includes style sheets for grid layouts and built-in scripts to provide support for a variety of browsers and devices
Some popular CSS frameworks include
Bootstrap
YAML4
960 Grid System
Foundation 3
35
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Setting up a Grid
A grid layout is based on rows of floating elements
Each floating element constitutes a column
The set of elements floating side-by-side establishes a row
Many grid layouts use the div (or division) element to mark distinct rows and columns of the grid
36
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Setting up a Grid (continued 1)
This is an example of a simple grid consisting of a single row with two columns:
<div class=“row”>
<div class=“column1”></div>
<div class=“column2”></div>
</div>
The page content is placed within the div elements
37
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Setting up a Grid (continued 2)
38
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
38
Setting up a Grid (continued 3)
The code for the grid layout for the Pandaisia Chocolates website is as follows:
39
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
39
Designing the Grid Rows
Grid rows contain floating columns
Since a grid row starts a new line within a page, it should only be displayed when both margins are clear of previously floated columns
40
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
40
Designing the Grid Columns
Every grid column needs to be floated within its row
Grid columns are placed within a div element having the general class name
class=“col-numerator-denominator”
where numerator-denominator provides the fractional width of the column
41
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Designing the Grid Columns (continued)
42
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
42
Adding the Page Content
43
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
43
Outlining a Grid
Outlines – Lines drawn around an element, enclosing the element content, padding, and border spaces
Outline-width: value; – Specifies the width of a line.
Properties of value are: thin, medium, or thick
Outline-color: color; – Specifies the color of a line.
Properties of color are: CSS color name or value
44
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Outlining a Grid (continued)
Outline-style: style; – Specifies the design of a line
Properties of style are: solid, double, dotted, dashed, groove, inset, ridge, or outset
45
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Defining a CSS Grid
To create a grid display without the use of div elements, use the following grid-based properties:
selector {
display: grid;
grid-template-rows: track-list;
grid-template-columns: track-list;
}
grid – Selected elements in a grid
track-list – Space-separated list of row heights or column widths
46
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Defining a CSS Grid (continued)
fr unit – Represents the fraction of available space left on the grid after all other rows or columns have attained their maximum allowable size
For example, the following style creates four columns with the dimension specified in the style rule:
grid-template-columns: 200px 250px 1fr 2fr;
47
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Assigning Content to Grid Cells
Elements in a CSS grid are placed within a grid cell at the intersection of a specified row and column
By default, all of the specified elements are placed in the grid cell located at the intersection of the first row and first column
48
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Assigning Content to Grid Cells (continued)
To place an element in a different cell, use
grid-row-start: integer;
grid-row-end: integer;
grid-column-start: integer;
grid-column-end: integer;
where integer defines the starting and ending row or column that contains the content
49
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Layout with Positioning Styles
50
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
50
Layout with Positioning Styles (continued)
51
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
51
The CSS positioning Styles
To place an element at a specific position within its container, use
position: type;
top: value;
right: value;
bottom: value;
left: value;
where type indicates the kind of positioning applied to the element and top, right, bottom, and left properties indicate the coordinates of the element
52
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
The CSS Positioning Styles (continued 1)
Static positioning – The element is placed where it would have fallen naturally within the flow of the document
Relative positioning – The element is moved out of its normal position in the document flow
Absolute positioning – The element is placed at specific coordinates within containers
53
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
The CSS Positioning Styles (continued 2)
54
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
54
The CSS Positioning Styles (continued 3)
55
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
55
Fixed and Inherited Positioning
Fixed positioning – Fixes an object within a browser window to avoids its movement
Inherited positioning – Allows an element to inherit the position value of its parent element
56
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Using the Positioning Styles
57
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
57
Using the Positioning Styles (continued 1)
58
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
58
Using the Positioning Styles (continued 2)
59
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
59
Using the Positioning Styles (continued 3)
60
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
60
Using the Positioning Styles (continued 4)
61
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
61
Handling Overflow
Overflow – Controls a browser that handles excess content
overflow: type;
where type is visible (the default), hidden, scroll, or auto
visible – Instructs browsers to increase the height of an element to fit overflow contents
62
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Handling Overflow (continued 1)
hidden – Keeps an element at the specified height and width, but cuts off excess content
scroll – Keeps an element at the specified dimensions, but adds horizontal and vertical scroll bars
auto – Keeps an element at the specified size, adding scroll bars when they are needed
63
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Handling Overflow (continued 2)
CSS3 provides the overflow-x and overflow-y properties to handle overflow specially in the horizontal and vertical directions
64
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
64
Handling Overflow (continued 3)
65
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
65
Clipping an Element
Clip – Defines a rectangular region through which an element’s content can be viewed
Anything that lies outside the boundary of the rectangle is hidden
The syntax of the clip property is
clip: rect(top, right, bottom,left);
where top, right, bottom, and left define the coordinates of the clipping rectangle
66
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Clipping an Element (continued)
67
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
67
Stacking elements
By default, elements that are loaded later by a browser are displayed on top of elements that are loaded earlier
To specify different stacking order, use the following z-index property:
z-index: value;
where value is a positive or negative integer, or the keyword auto
68
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Stacking elements (continued 1)
The z-index property works only for elements that are placed with absolute positioning
69
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
69
Stacking elements (continued 2)
An element’s z-index value determines its position relative only to other elements that share a common parent
70
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
70