SlideShare a Scribd company logo
Laying out Elements with CSS
Objectives
Assess the CSS box model
Set element width and borders
Set margins and padding
Align elements with float
Control page flow with clear
HTML5 and CSS3 – Illustrated, 2nd Edition 2
Objectives (continued)
Implement fixed positioning
Implement relative positioning
Control stacking order
Implement absolute positioning
HTML5 and CSS3 – Illustrated, 2nd Edition 3
Assess the CSS Box Model
Box model: used by CSS to represent
characteristics of every web page
element
 Treats element as rectangular box
border: border surrounding element
margin: space from border to
neighboring/parent element
padding: space between border and
element content
HTML5 and CSS3 – Illustrated, 2nd Edition 4
Assess the CSS Box Model
(continued)
The CSS box model
HTML5 and CSS3 – Illustrated, 2nd Edition 5
Assess the CSS Box Model
(continued)
Box model properties
HTML5 and CSS3 – Illustrated, 2nd Edition 6
Assess the CSS Box Model
(continued)
CSS units of measure
 Relative units: ems, percent, rems
 Absolute units: pixels
Commonly used CSS units of
measure
HTML5 and CSS3 – Illustrated, 2nd Edition 7
Assess the CSS Box Model
(continued)
Size of padding, margin, and
border increase the amount of space
occupied by an element
 Dimensions of these properties not
included in specified width / height
• Specified width and height refer only to the
content of an element
• To calculate add the total space, add the
width value to the padding, margin, and
border values
HTML5 and CSS3 – Illustrated, 2nd Edition 8
Assess the CSS Box Model
(continued)
Calculating web page space occupied
by an element
HTML5 and CSS3 – Illustrated, 2nd Edition 9
Assess the CSS Box Model
(continued)
Collapse: When bottom margin of one
element is adjacent to top margin of
another, the margins combine to the
size of the larger of the two
 Affects element size planning
Separate property for each side of
padding and margin, e.g.
 padding-top: 2px;
 margin-right: 1em;
HTML5 and CSS3 – Illustrated, 2nd Edition 10
Set Element Width
and Borders
Margin, padding, and border are
shorthand properties
 Assign values to multiple CSS properties
 Assign values to all four sides of an
element
 Assign these values using a single
declaration
Can set width, style, or color of any or
all sides of an element
HTML5 and CSS3 – Illustrated, 2nd Edition 11
Set Element Width
and Borders (continued)
Specific properties for the box model
Reset rule: resets one or more
common properties to a common
baseline, e.g.
 border: 0
HTML5 and CSS3 – Illustrated, 2nd Edition 12
Set Element Width
and Borders (continued)
Code with width and border
declarations
HTML5 and CSS3 – Illustrated, 2nd Edition 13
Set Element Width
and Borders (continued)
width and border declarations
rendered in a browser and developer
tools opened to show box model
HTML5 and CSS3 – Illustrated, 2nd Edition 14
Set Margins and Padding
Use the shorthand property to assign
multiple values
 Separate each value with a space
 Meaning of different number of values
Assign auto to left and right margins
to center the element horizontally
HTML5 and CSS3 – Illustrated, 2nd Edition 15
Set Margins and Padding
(continued)
Example of padding using three
values and margin using two values
Example of margin set to auto
HTML5 and CSS3 – Illustrated, 2nd Edition 16
Align Elements with float
Page flow: the order of elements in the
HTML document
 User agents render HTML top to bottom
 Easy to create layouts with elements
stacked vertically
Use float, clear, and width
properties to create columns of text
and graphics parallel to each other
 Use width property to assign width to
each of the columns
HTML5 and CSS3 – Illustrated, 2nd Edition 17
Align Elements with float
(continued)
Example of float applied to element
Use float to create multicolumn
layoutsHTML5 and CSS3 – Illustrated, 2nd Edition 18
Align Elements with float
(continued)
Code with float and result in browser
HTML5 and CSS3 – Illustrated, 2nd Edition 19
Control Page Flow
with clear
float gives basic control over layout
clear gives more precise control
 Prevents floated elements from being
displayed to the left, right, or on the side
of another element
clear property values
HTML5 and CSS3 – Illustrated, 2nd Edition 20
Control Page Flow with clear
(continued)
Example of clear property
HTML5 and CSS3 – Illustrated, 2nd Edition 21
Control Page Flow with clear
(continued)
Code including clear property and
how it is rendered in a browser
HTML5 and CSS3 – Illustrated, 2nd Edition 22
Implement Fixed
Positioning
fixed positioning keeps an element
in the same location, even when the
page is scrolled
Use the position property with the
value of fixed
Then specify
 Horizontal position using left or right
 Vertical positing using top or bottom
HTML5 and CSS3 – Illustrated, 2nd Edition 23
Implement Fixed Positioning
(continued)
Commonly used position properties
Properties and values for fixed
positioning
HTML5 and CSS3 – Illustrated, 2nd Edition 24
Implement Fixed Positioning
(continued)
Example showing fixed positioning
HTML5 and CSS3 – Illustrated, 2nd Edition 25
Implement Fixed Positioning
(continued)
Code including fixed value and how
it is rendered in a browser
HTML5 and CSS3 – Illustrated, 2nd Edition 26
Implement Relative
Positioning
Relative positioning: adjusting default
position of an element
 Preserves space allotted to element in
default page flow
 Set position property to relative
 Style exact location using the properties
•left and right for horizontal positioning
•top and bottom for vertical positioning
HTML5 and CSS3 – Illustrated, 2nd Edition 27
Implement Relative
Positioning (continued)
CSS properties and values for relative
positioning
HTML5 and CSS3 – Illustrated, 2nd Edition 28
Implement Relative
Positioning (continued)
Implementing relative positioning and
page flow
HTML5 and CSS3 – Illustrated, 2nd Edition 29
Implement Relative
Positioning (continued)
Code using relative positioning and
how it is rendered in a browser
HTML5 and CSS3 – Illustrated, 2nd Edition 30
Control stacking order
Stacking elements: positioning
elements so that they can overlap
 Additional possibilities for layouts
 Applies only to positioned element
 Elements placed in new layer
 Requires careful planning
Stacking order controlled by values
assigned to z-index property
HTML5 and CSS3 – Illustrated, 2nd Edition 31
Control Stacking Order
(continued)
Stacking positioned elements
HTML5 and CSS3 – Illustrated, 2nd Edition 32
Control Stacking Order
(continued)
Code to stack and how it is
rendered in a browser
HTML5 and CSS3 – Illustrated, 2nd Edition 33
Implement Absolute
Positioning
Absolute positioning: takes element
out of page flow entirely
 Allows other elements to flow into space
element would have occupied
 Set position property to absolute
 Style exact location using left, right,
top, and bottom properties
 Location is calculated relative to closest
ancestor element that has position
applied to it
HTML5 and CSS3 – Illustrated, 2nd Edition 34
Implement Absolute
Positioning (continued)
Absolute positioning and page flow
Properties and values
HTML5 and CSS3 – Illustrated, 2nd Edition 35
Implement Absolute
Positioning (continued)
Code to absolutely position an
element and the result in a browser
HTML5 and CSS3 – Illustrated, 2nd Edition 36
Summary
Box model: used by CSS to represent
characteristics of elements
 Treats element as rectangular box
 Defines border, margin and padding
Margins of adjacent elements can
collapse into one margin
Specify border width, style,
and/or color of any or all sides
HTML5 and CSS3 – Illustrated, 2nd Edition 37
Summary (continued)
Use multiple values to assign margin
and padding values to different sides
Use float to change the default
page flow
Use clear to create a more precise
layout
Use float, clear and width to
create multicolumn layouts
HTML5 and CSS3 – Illustrated, 2nd Edition 38
Summary (continued)
Fixed positioning: keeps an element in
the same position, even when the
page is scrolled
Relative positioning: adjusting location
of element relative to its page flow
location using position property
 Space preserved
HTML5 and CSS3 – Illustrated, 2nd Edition 39
Summary (continued)
Use z-index to create a stacking
order for elements
 Stacked elements are placed in new
layers
Absolute positioning: takes elements
out of page flow entirely
 Other elements reflow into empty space
HTML5 and CSS3 – Illustrated, 2nd Edition 40

More Related Content

PPT
Javascript programming using the document object model
PPT
Creating Visual Effects and Animation
PPT
Formatting text with CSS
PPT
Working with Links
PPT
Structure Web Content
PPT
Working with Images
PPT
Testing and Improving Performance
PPT
Getting Started with your Website
Javascript programming using the document object model
Creating Visual Effects and Animation
Formatting text with CSS
Working with Links
Structure Web Content
Working with Images
Testing and Improving Performance
Getting Started with your Website

What's hot (17)

PPT
Getting Started with CSS
PPT
Working with Video and Audio
PPT
Optimizing a website for search engines
PPT
Social media and your website
PPTX
C# Crystal Reports
PPTX
Crystal report
PPTX
Crystal report generation in visual studio 2010
PDF
Quickstart Microsoft access 2013
PPTX
New Features of HTML 5.1 to Create More Flexible Web Experiences
PPT
M02 un12 p01
PPT
Introduction To Flex
PPT
Dreamweaver Ch03
PPTX
Liquidlayout
PDF
Content Architecture
PPTX
Access 2013 Unit A
PDF
Psd to html
Getting Started with CSS
Working with Video and Audio
Optimizing a website for search engines
Social media and your website
C# Crystal Reports
Crystal report
Crystal report generation in visual studio 2010
Quickstart Microsoft access 2013
New Features of HTML 5.1 to Create More Flexible Web Experiences
M02 un12 p01
Introduction To Flex
Dreamweaver Ch03
Liquidlayout
Content Architecture
Access 2013 Unit A
Psd to html
Ad

Viewers also liked (20)

PPT
Organizing Content with Lists and Tables
PPT
Creating and Processing Web Forms
PDF
Microsoft TechDays - CSS3 Presentation
PPTX
Tutorial: Html5 And Css3 Site
PDF
CSS3 - is everything we used to do wrong?
PDF
Maintainable CSS
PPTX
Everything You know about CSS is Wrong
PPTX
Presentación Multimedia - HTML5
PPTX
Techdays 2012 - Développement Web Mobile avec Microsoft
PDF
ตัวอย่าง Workshop basic html5 and css3 จากไอทีจีเนียส
PDF
The Future State of Layout
PPTX
Css floats
PDF
CSS: a rapidly changing world
PDF
Efficient, maintainable CSS
PDF
CSS3: A practical introduction (FT2010 talk)
PDF
CSS Layouting #4 : Float
PPSX
CSS Box Model Presentation
PDF
CSS3 secrets: 10 things you might not know about CSS3
PDF
Conoce HTML5 y CSS3
Organizing Content with Lists and Tables
Creating and Processing Web Forms
Microsoft TechDays - CSS3 Presentation
Tutorial: Html5 And Css3 Site
CSS3 - is everything we used to do wrong?
Maintainable CSS
Everything You know about CSS is Wrong
Presentación Multimedia - HTML5
Techdays 2012 - Développement Web Mobile avec Microsoft
ตัวอย่าง Workshop basic html5 and css3 จากไอทีจีเนียส
The Future State of Layout
Css floats
CSS: a rapidly changing world
Efficient, maintainable CSS
CSS3: A practical introduction (FT2010 talk)
CSS Layouting #4 : Float
CSS Box Model Presentation
CSS3 secrets: 10 things you might not know about CSS3
Conoce HTML5 y CSS3
Ad

Similar to Laying Out Elements with CSS (20)

PPTX
Castro Chapter 11
PPTX
css3.pptx
PDF
CSS3 Refresher
PPTX
CSS_Dibbo
PPT
Css advanced – session 4
PDF
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
PDF
PDF
Evolution of CSS
PDF
Web Design & Development - Session 3
PDF
Full download Responsive Web Design with HTML5 and CSS3 Second Edition Ben Fr...
PPTX
Cascading Style Sheets CSS
PDF
Web Layout
PPTX
HTML and CSS part 3
PPTX
Designing for the web - 101
PPTX
CSS Walktrough Internship Course
PPT
Tutorial0eesrtjfzjgxkgxkxxkxkgxkgxjffj3.ppt
PDF
Intro to HTML and CSS basics
PPTX
Css intro
PPTX
CSS_Day_Three (W3schools)
PPTX
Css training
Castro Chapter 11
css3.pptx
CSS3 Refresher
CSS_Dibbo
Css advanced – session 4
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
Evolution of CSS
Web Design & Development - Session 3
Full download Responsive Web Design with HTML5 and CSS3 Second Edition Ben Fr...
Cascading Style Sheets CSS
Web Layout
HTML and CSS part 3
Designing for the web - 101
CSS Walktrough Internship Course
Tutorial0eesrtjfzjgxkgxkxxkxkgxkgxjffj3.ppt
Intro to HTML and CSS basics
Css intro
CSS_Day_Three (W3schools)
Css training

More from Nicole Ryan (16)

PPT
Inheritance
PPT
Python Dictionaries and Sets
PPTX
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
PPTX
Chapter 11: Object Oriented Programming Part 2
PPTX
Intro to Programming: Modularity
PPT
Programming Logic and Design: Arrays
PPTX
Programming Logic and Design: Working with Data
PPT
Setting up your development environment
PPTX
Dynamic vs static
PPT
Working with Databases and MySQL
PPT
Using arrays with PHP for forms and storing information
PPTX
Tables and forms accessibility and microformats
PPTX
Creating and styling forms
PPTX
Creating and styling tables
PPT
Files and Directories in PHP
PPT
Handling User Input and Processing Form Data
Inheritance
Python Dictionaries and Sets
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 11: Object Oriented Programming Part 2
Intro to Programming: Modularity
Programming Logic and Design: Arrays
Programming Logic and Design: Working with Data
Setting up your development environment
Dynamic vs static
Working with Databases and MySQL
Using arrays with PHP for forms and storing information
Tables and forms accessibility and microformats
Creating and styling forms
Creating and styling tables
Files and Directories in PHP
Handling User Input and Processing Form Data

Recently uploaded (20)

PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Cell Types and Its function , kingdom of life
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
Classroom Observation Tools for Teachers
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Institutional Correction lecture only . . .
Sports Quiz easy sports quiz sports quiz
Cell Types and Its function , kingdom of life
Microbial diseases, their pathogenesis and prophylaxis
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
GDM (1) (1).pptx small presentation for students
Pharmacology of Heart Failure /Pharmacotherapy of CHF
102 student loan defaulters named and shamed – Is someone you know on the list?
2.FourierTransform-ShortQuestionswithAnswers.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Final Presentation General Medicine 03-08-2024.pptx
01-Introduction-to-Information-Management.pdf
Classroom Observation Tools for Teachers
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O7-L3 Supply Chain Operations - ICLT Program
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Institutional Correction lecture only . . .

Laying Out Elements with CSS

  • 2. Objectives Assess the CSS box model Set element width and borders Set margins and padding Align elements with float Control page flow with clear HTML5 and CSS3 – Illustrated, 2nd Edition 2
  • 3. Objectives (continued) Implement fixed positioning Implement relative positioning Control stacking order Implement absolute positioning HTML5 and CSS3 – Illustrated, 2nd Edition 3
  • 4. Assess the CSS Box Model Box model: used by CSS to represent characteristics of every web page element  Treats element as rectangular box border: border surrounding element margin: space from border to neighboring/parent element padding: space between border and element content HTML5 and CSS3 – Illustrated, 2nd Edition 4
  • 5. Assess the CSS Box Model (continued) The CSS box model HTML5 and CSS3 – Illustrated, 2nd Edition 5
  • 6. Assess the CSS Box Model (continued) Box model properties HTML5 and CSS3 – Illustrated, 2nd Edition 6
  • 7. Assess the CSS Box Model (continued) CSS units of measure  Relative units: ems, percent, rems  Absolute units: pixels Commonly used CSS units of measure HTML5 and CSS3 – Illustrated, 2nd Edition 7
  • 8. Assess the CSS Box Model (continued) Size of padding, margin, and border increase the amount of space occupied by an element  Dimensions of these properties not included in specified width / height • Specified width and height refer only to the content of an element • To calculate add the total space, add the width value to the padding, margin, and border values HTML5 and CSS3 – Illustrated, 2nd Edition 8
  • 9. Assess the CSS Box Model (continued) Calculating web page space occupied by an element HTML5 and CSS3 – Illustrated, 2nd Edition 9
  • 10. Assess the CSS Box Model (continued) Collapse: When bottom margin of one element is adjacent to top margin of another, the margins combine to the size of the larger of the two  Affects element size planning Separate property for each side of padding and margin, e.g.  padding-top: 2px;  margin-right: 1em; HTML5 and CSS3 – Illustrated, 2nd Edition 10
  • 11. Set Element Width and Borders Margin, padding, and border are shorthand properties  Assign values to multiple CSS properties  Assign values to all four sides of an element  Assign these values using a single declaration Can set width, style, or color of any or all sides of an element HTML5 and CSS3 – Illustrated, 2nd Edition 11
  • 12. Set Element Width and Borders (continued) Specific properties for the box model Reset rule: resets one or more common properties to a common baseline, e.g.  border: 0 HTML5 and CSS3 – Illustrated, 2nd Edition 12
  • 13. Set Element Width and Borders (continued) Code with width and border declarations HTML5 and CSS3 – Illustrated, 2nd Edition 13
  • 14. Set Element Width and Borders (continued) width and border declarations rendered in a browser and developer tools opened to show box model HTML5 and CSS3 – Illustrated, 2nd Edition 14
  • 15. Set Margins and Padding Use the shorthand property to assign multiple values  Separate each value with a space  Meaning of different number of values Assign auto to left and right margins to center the element horizontally HTML5 and CSS3 – Illustrated, 2nd Edition 15
  • 16. Set Margins and Padding (continued) Example of padding using three values and margin using two values Example of margin set to auto HTML5 and CSS3 – Illustrated, 2nd Edition 16
  • 17. Align Elements with float Page flow: the order of elements in the HTML document  User agents render HTML top to bottom  Easy to create layouts with elements stacked vertically Use float, clear, and width properties to create columns of text and graphics parallel to each other  Use width property to assign width to each of the columns HTML5 and CSS3 – Illustrated, 2nd Edition 17
  • 18. Align Elements with float (continued) Example of float applied to element Use float to create multicolumn layoutsHTML5 and CSS3 – Illustrated, 2nd Edition 18
  • 19. Align Elements with float (continued) Code with float and result in browser HTML5 and CSS3 – Illustrated, 2nd Edition 19
  • 20. Control Page Flow with clear float gives basic control over layout clear gives more precise control  Prevents floated elements from being displayed to the left, right, or on the side of another element clear property values HTML5 and CSS3 – Illustrated, 2nd Edition 20
  • 21. Control Page Flow with clear (continued) Example of clear property HTML5 and CSS3 – Illustrated, 2nd Edition 21
  • 22. Control Page Flow with clear (continued) Code including clear property and how it is rendered in a browser HTML5 and CSS3 – Illustrated, 2nd Edition 22
  • 23. Implement Fixed Positioning fixed positioning keeps an element in the same location, even when the page is scrolled Use the position property with the value of fixed Then specify  Horizontal position using left or right  Vertical positing using top or bottom HTML5 and CSS3 – Illustrated, 2nd Edition 23
  • 24. Implement Fixed Positioning (continued) Commonly used position properties Properties and values for fixed positioning HTML5 and CSS3 – Illustrated, 2nd Edition 24
  • 25. Implement Fixed Positioning (continued) Example showing fixed positioning HTML5 and CSS3 – Illustrated, 2nd Edition 25
  • 26. Implement Fixed Positioning (continued) Code including fixed value and how it is rendered in a browser HTML5 and CSS3 – Illustrated, 2nd Edition 26
  • 27. Implement Relative Positioning Relative positioning: adjusting default position of an element  Preserves space allotted to element in default page flow  Set position property to relative  Style exact location using the properties •left and right for horizontal positioning •top and bottom for vertical positioning HTML5 and CSS3 – Illustrated, 2nd Edition 27
  • 28. Implement Relative Positioning (continued) CSS properties and values for relative positioning HTML5 and CSS3 – Illustrated, 2nd Edition 28
  • 29. Implement Relative Positioning (continued) Implementing relative positioning and page flow HTML5 and CSS3 – Illustrated, 2nd Edition 29
  • 30. Implement Relative Positioning (continued) Code using relative positioning and how it is rendered in a browser HTML5 and CSS3 – Illustrated, 2nd Edition 30
  • 31. Control stacking order Stacking elements: positioning elements so that they can overlap  Additional possibilities for layouts  Applies only to positioned element  Elements placed in new layer  Requires careful planning Stacking order controlled by values assigned to z-index property HTML5 and CSS3 – Illustrated, 2nd Edition 31
  • 32. Control Stacking Order (continued) Stacking positioned elements HTML5 and CSS3 – Illustrated, 2nd Edition 32
  • 33. Control Stacking Order (continued) Code to stack and how it is rendered in a browser HTML5 and CSS3 – Illustrated, 2nd Edition 33
  • 34. Implement Absolute Positioning Absolute positioning: takes element out of page flow entirely  Allows other elements to flow into space element would have occupied  Set position property to absolute  Style exact location using left, right, top, and bottom properties  Location is calculated relative to closest ancestor element that has position applied to it HTML5 and CSS3 – Illustrated, 2nd Edition 34
  • 35. Implement Absolute Positioning (continued) Absolute positioning and page flow Properties and values HTML5 and CSS3 – Illustrated, 2nd Edition 35
  • 36. Implement Absolute Positioning (continued) Code to absolutely position an element and the result in a browser HTML5 and CSS3 – Illustrated, 2nd Edition 36
  • 37. Summary Box model: used by CSS to represent characteristics of elements  Treats element as rectangular box  Defines border, margin and padding Margins of adjacent elements can collapse into one margin Specify border width, style, and/or color of any or all sides HTML5 and CSS3 – Illustrated, 2nd Edition 37
  • 38. Summary (continued) Use multiple values to assign margin and padding values to different sides Use float to change the default page flow Use clear to create a more precise layout Use float, clear and width to create multicolumn layouts HTML5 and CSS3 – Illustrated, 2nd Edition 38
  • 39. Summary (continued) Fixed positioning: keeps an element in the same position, even when the page is scrolled Relative positioning: adjusting location of element relative to its page flow location using position property  Space preserved HTML5 and CSS3 – Illustrated, 2nd Edition 39
  • 40. Summary (continued) Use z-index to create a stacking order for elements  Stacked elements are placed in new layers Absolute positioning: takes elements out of page flow entirely  Other elements reflow into empty space HTML5 and CSS3 – Illustrated, 2nd Edition 40