SlideShare a Scribd company logo
The fundamentals
of browser
rendering - part 2
By Barak Drechsler
Recap from our previous
session...
1. We discussed the browser rendering flow DOM + CSSOM => Render Tree => Layout =>
paint.
2. Each element is box.
3. It size is defined by it box-model, either (content-box or border-box).
4. We have a different types of boxes which affect element behaviour (inline, block, inline-
block, flex, none)
Today's Agenda
We now have to see, how can we define how elements are aligned together.
Floats
Positioning
Collapsing Margins
Z-index and elements visibility order (stacking context).
Float
The float CSS property specifies that an element should be taken from the normal flow and
placed along the left or right side of its container, where text and inline elements will wrap
around it.
A floating element is one where the computed value of float is not none.
(MDN)
Play Time - floats
https://plnkr.co/edit/Bz4hS2E9e
mkbr3fvts16?p=preview
Floats Tricky Parts
Float - summary
1. When an element is floated it is taken out of the normal flow of the document. It is shifted
to the left or right until it touches the edge of its containing box or another floated element.
2. Float elements must be given explicit width, otherwise unexpected behavior could occur.
3. Non-positioned, non-floated, block-level elements act as if the floated element is not there.
4. To make parent of floats take it height, we must clear the floats, a trick could be adding
overflow: auto.
5. Bootstrap grid system is float based.
Positioning
1. Positioning allows you to move an element around the document.
2. We have 4 types of Positioning:
a. Static (default)
b. Relative
c. Absolute
d. Fixed
Static
The default value for all elements, represents the normal document flow.
Position related attributes have no effect (top, left, etc…)
Only usage is to override and reset other position values.
Isn’t affected by z-index.
Relative
This keyword lays out all elements as though the element were not positioned, and then adjust
the element's position, without changing layout (and thus leaving a gap for the element where it
would have been had it not been positioned (MDN)
- The Element starts where it should have been
- You can move it from it’s original position via (top, left, etc…)
- limits the scope of absolutely positioned child elements
Absolute
Do not leave space for the element. Instead, position it at a specified position relative to its
closest positioned ancestor if any, or otherwise relative to the initial containing block.
Absolutely positioned boxes can have margins, and they do not collapse with any other
margins. (MDN)
- The element is out of the browser flow, meaning it won’t be affected by other elements.
- Also it won’t affect other elements flow.
- Will be relative to it closest non static parent, or to the html tag itself if none found.
Fixed
Do not leave space for the element.
Instead, position it at a specified position relative to the screen's viewport and don't move it
when scrolled. When printing, position it at that fixed position on every page. This value always
create a new stacking context (MDN).
Play Time - Positions
https://plnkr.co/edit/JosJySkjT7V
Ah4xuTp98?p=preview
Collapsing Margins
CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can
combine to form a single margin. Margins that combine this way are said to collapse, and the
resulting combined margin is called a collapsed margin. (W3C SPEC)
In short: when two topbottom margins touch each other they collapse
1. The higher margin is taken, and the smaller collapse to 0.
2. Negative and positive margins are added up rather than collapse.
3. Two Negative margins collapse, with the smaller one taken.
Play Time - Collapsing Margins
https://plnkr.co/edit/wvaBa6A7R
9snWJVxoIOP?p=preview
Stacking context is the three-dimensional conceptualization of HTML elements along an
imaginary z-axis relative to the user who is assumed to be facing the viewport or the webpage.
HTML elements occupy this space in priority order based on element attributes. (MDN)
Elements Order without z-index, are formed by the HTML hierarchy:
1. Last elements in HTML order will be displayed on top.
2. Positioned Elements have higher priority over static elements, which is strong then html
order.
3rd Dimension, stacking context
Play Time - Stacking Context no
z-index.
https://plnkr.co/edit/9ygOvS1BiB
qsyatxcCML?p=preview
Positioning and assigning a z-index value to an HTML element creates a stacking context, (as
does assigning non-full opacity).
Stacking contexts can be contained in other stacking contexts, and together create a hierarchy
of stacking contexts.
Each stacking context is completely independent from its siblings: only descendant elements are
considered when stacking is processed.
Each stacking context is self-contained: after the element's contents are stacked, the whole
element is considered in the stacking order of the parent stacking context.
Z-index & multi hierarchy stacks
Play Time - Stacking Contexts
https://plnkr.co/edit/tzW4NsFWA
Hu0TLjAWEO1?p=preview
MDN menu example
Stacking Context - Summary
1. Stacking Context, is the concept of how the browser renders elements in the 3rd dimension
(facing browser window).
2. By default context order is derived from html order (last element on top)
3. Default order gives priority for positioned elements over static elements.
4. Each context can host more contexts, each stacking context is self-contained.
5. New contexts are created by certain conditions
a. HTML root
Other Worth Knowing...
1. Css Transform
2. Css Pseudo Elements, check this
3. MDN, anytime you are not sure about
what's going on...
Questions?

More Related Content

Similar to Fundamentals of Browser Rendering Css Overview PT 2

Css from scratch
Css from scratchCss from scratch
Css from scratch
Ahmad Al-ammar
 
How Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) WorksHow Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) Works
Amit Tyagi
 
But what about old browsers?
But what about old browsers?But what about old browsers?
But what about old browsers?
Rachel Andrew
 
CSS
CSSCSS
CSS_Day_Three (W3schools)
CSS_Day_Three (W3schools)CSS_Day_Three (W3schools)
CSS_Day_Three (W3schools)
Rafi Haidari
 
CSS_Dibbo
CSS_DibboCSS_Dibbo
CSS_Dibbo
Sayanton Vhaduri
 
Working-With-The-Box-Model-Lesson-5.pptx
Working-With-The-Box-Model-Lesson-5.pptxWorking-With-The-Box-Model-Lesson-5.pptx
Working-With-The-Box-Model-Lesson-5.pptx
AxestetikrieyHales
 
Js info vis_toolkit
Js info vis_toolkitJs info vis_toolkit
Js info vis_toolkit
nikhilyagnic
 
Flexbox and Grid Layout: How you will structure layouts tomorrow.
Flexbox and Grid Layout: How you will structure layouts tomorrow.Flexbox and Grid Layout: How you will structure layouts tomorrow.
Flexbox and Grid Layout: How you will structure layouts tomorrow.
Diego Eis
 
Flexbox and Grid Layout
Flexbox and Grid LayoutFlexbox and Grid Layout
Flexbox and Grid Layout
Rachel Andrew
 
Hello Android
Hello AndroidHello Android
Hello Android
Trong Dinh
 
CSS Flexbox (flexible box layout)
CSS Flexbox (flexible box layout)CSS Flexbox (flexible box layout)
CSS Flexbox (flexible box layout)
Woodridge Software
 
Fundamentals of Software Engineering
Fundamentals of Software Engineering Fundamentals of Software Engineering
Fundamentals of Software Engineering
Madhar Khan Pathan
 
AtomiDB FAQs
AtomiDB FAQsAtomiDB FAQs
AtomiDB FAQs
JEAN-MICHEL LETENNIER
 
Intro to HTML and CSS basics
Intro to HTML and CSS basicsIntro to HTML and CSS basics
Intro to HTML and CSS basics
Eliran Eliassy
 
3 css essentials
3 css essentials3 css essentials
3 css essentials
Anchu S
 
Context at design
Context at designContext at design
Context at design
Kranthi Kumar
 
Displaying.pptx
Displaying.pptxDisplaying.pptx
Displaying.pptx
naresh414857
 
Web Development 4 (HTML & CSS)
Web Development 4 (HTML & CSS)Web Development 4 (HTML & CSS)
Web Development 4 (HTML & CSS)
ghayour abbas
 
Web Development 4
Web Development 4Web Development 4
Web Development 4
ghayour abbas
 

Similar to Fundamentals of Browser Rendering Css Overview PT 2 (20)

Css from scratch
Css from scratchCss from scratch
Css from scratch
 
How Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) WorksHow Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) Works
 
But what about old browsers?
But what about old browsers?But what about old browsers?
But what about old browsers?
 
CSS
CSSCSS
CSS
 
CSS_Day_Three (W3schools)
CSS_Day_Three (W3schools)CSS_Day_Three (W3schools)
CSS_Day_Three (W3schools)
 
CSS_Dibbo
CSS_DibboCSS_Dibbo
CSS_Dibbo
 
Working-With-The-Box-Model-Lesson-5.pptx
Working-With-The-Box-Model-Lesson-5.pptxWorking-With-The-Box-Model-Lesson-5.pptx
Working-With-The-Box-Model-Lesson-5.pptx
 
Js info vis_toolkit
Js info vis_toolkitJs info vis_toolkit
Js info vis_toolkit
 
Flexbox and Grid Layout: How you will structure layouts tomorrow.
Flexbox and Grid Layout: How you will structure layouts tomorrow.Flexbox and Grid Layout: How you will structure layouts tomorrow.
Flexbox and Grid Layout: How you will structure layouts tomorrow.
 
Flexbox and Grid Layout
Flexbox and Grid LayoutFlexbox and Grid Layout
Flexbox and Grid Layout
 
Hello Android
Hello AndroidHello Android
Hello Android
 
CSS Flexbox (flexible box layout)
CSS Flexbox (flexible box layout)CSS Flexbox (flexible box layout)
CSS Flexbox (flexible box layout)
 
Fundamentals of Software Engineering
Fundamentals of Software Engineering Fundamentals of Software Engineering
Fundamentals of Software Engineering
 
AtomiDB FAQs
AtomiDB FAQsAtomiDB FAQs
AtomiDB FAQs
 
Intro to HTML and CSS basics
Intro to HTML and CSS basicsIntro to HTML and CSS basics
Intro to HTML and CSS basics
 
3 css essentials
3 css essentials3 css essentials
3 css essentials
 
Context at design
Context at designContext at design
Context at design
 
Displaying.pptx
Displaying.pptxDisplaying.pptx
Displaying.pptx
 
Web Development 4 (HTML & CSS)
Web Development 4 (HTML & CSS)Web Development 4 (HTML & CSS)
Web Development 4 (HTML & CSS)
 
Web Development 4
Web Development 4Web Development 4
Web Development 4
 

More from Barak Drechsler

Stay lazy, use lerna
Stay lazy, use lernaStay lazy, use lerna
Stay lazy, use lerna
Barak Drechsler
 
Atomic javascript
Atomic javascriptAtomic javascript
Atomic javascript
Barak Drechsler
 
Room service (worker) please!
Room service (worker) please!Room service (worker) please!
Room service (worker) please!
Barak Drechsler
 
From MVC to Component Based Architecture
From MVC to Component Based ArchitectureFrom MVC to Component Based Architecture
From MVC to Component Based Architecture
Barak Drechsler
 
Js: master prototypes
Js: master prototypesJs: master prototypes
Js: master prototypes
Barak Drechsler
 
Understanding the flex layout
Understanding the flex layoutUnderstanding the flex layout
Understanding the flex layout
Barak Drechsler
 

More from Barak Drechsler (6)

Stay lazy, use lerna
Stay lazy, use lernaStay lazy, use lerna
Stay lazy, use lerna
 
Atomic javascript
Atomic javascriptAtomic javascript
Atomic javascript
 
Room service (worker) please!
Room service (worker) please!Room service (worker) please!
Room service (worker) please!
 
From MVC to Component Based Architecture
From MVC to Component Based ArchitectureFrom MVC to Component Based Architecture
From MVC to Component Based Architecture
 
Js: master prototypes
Js: master prototypesJs: master prototypes
Js: master prototypes
 
Understanding the flex layout
Understanding the flex layoutUnderstanding the flex layout
Understanding the flex layout
 

Recently uploaded

不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
bseovas
 
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
ysasp1
 
7 Best Cloud Hosting Services to Try Out in 2024
7 Best Cloud Hosting Services to Try Out in 20247 Best Cloud Hosting Services to Try Out in 2024
7 Best Cloud Hosting Services to Try Out in 2024
Danica Gill
 
[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024
hackersuli
 
Discover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to IndiaDiscover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to India
davidjhones387
 
留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理
留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理
留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理
bseovas
 
国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理
国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理
国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理
zoowe
 
Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?
Paul Walk
 
Explore-Insanony: Watch Instagram Stories Secretly
Explore-Insanony: Watch Instagram Stories SecretlyExplore-Insanony: Watch Instagram Stories Secretly
Explore-Insanony: Watch Instagram Stories Secretly
Trending Blogers
 
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
uehowe
 
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
vmemo1
 
Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!
Toptal Tech
 
Gen Z and the marketplaces - let's translate their needs
Gen Z and the marketplaces - let's translate their needsGen Z and the marketplaces - let's translate their needs
Gen Z and the marketplaces - let's translate their needs
Laura Szabó
 
Understanding User Behavior with Google Analytics.pdf
Understanding User Behavior with Google Analytics.pdfUnderstanding User Behavior with Google Analytics.pdf
Understanding User Behavior with Google Analytics.pdf
SEO Article Boost
 
学位认证网(DU毕业证)迪肯大学毕业证成绩单一比一原版制作
学位认证网(DU毕业证)迪肯大学毕业证成绩单一比一原版制作学位认证网(DU毕业证)迪肯大学毕业证成绩单一比一原版制作
学位认证网(DU毕业证)迪肯大学毕业证成绩单一比一原版制作
zyfovom
 
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
uehowe
 
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
uehowe
 
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdfMeet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Florence Consulting
 
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
xjq03c34
 
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmanuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
wolfsoftcompanyco
 

Recently uploaded (20)

不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
 
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
成绩单ps(UST毕业证)圣托马斯大学毕业证成绩单快速办理
 
7 Best Cloud Hosting Services to Try Out in 2024
7 Best Cloud Hosting Services to Try Out in 20247 Best Cloud Hosting Services to Try Out in 2024
7 Best Cloud Hosting Services to Try Out in 2024
 
[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024
 
Discover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to IndiaDiscover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to India
 
留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理
留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理
留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理
 
国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理
国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理
国外证书(Lincoln毕业证)新西兰林肯大学毕业证成绩单不能毕业办理
 
Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?
 
Explore-Insanony: Watch Instagram Stories Secretly
Explore-Insanony: Watch Instagram Stories SecretlyExplore-Insanony: Watch Instagram Stories Secretly
Explore-Insanony: Watch Instagram Stories Secretly
 
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
 
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
 
Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!Ready to Unlock the Power of Blockchain!
Ready to Unlock the Power of Blockchain!
 
Gen Z and the marketplaces - let's translate their needs
Gen Z and the marketplaces - let's translate their needsGen Z and the marketplaces - let's translate their needs
Gen Z and the marketplaces - let's translate their needs
 
Understanding User Behavior with Google Analytics.pdf
Understanding User Behavior with Google Analytics.pdfUnderstanding User Behavior with Google Analytics.pdf
Understanding User Behavior with Google Analytics.pdf
 
学位认证网(DU毕业证)迪肯大学毕业证成绩单一比一原版制作
学位认证网(DU毕业证)迪肯大学毕业证成绩单一比一原版制作学位认证网(DU毕业证)迪肯大学毕业证成绩单一比一原版制作
学位认证网(DU毕业证)迪肯大学毕业证成绩单一比一原版制作
 
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
 
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
 
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdfMeet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
 
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
 
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmanuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
 

Fundamentals of Browser Rendering Css Overview PT 2

  • 1. The fundamentals of browser rendering - part 2 By Barak Drechsler
  • 2.
  • 3. Recap from our previous session... 1. We discussed the browser rendering flow DOM + CSSOM => Render Tree => Layout => paint. 2. Each element is box. 3. It size is defined by it box-model, either (content-box or border-box). 4. We have a different types of boxes which affect element behaviour (inline, block, inline- block, flex, none)
  • 4. Today's Agenda We now have to see, how can we define how elements are aligned together. Floats Positioning Collapsing Margins Z-index and elements visibility order (stacking context).
  • 5. Float The float CSS property specifies that an element should be taken from the normal flow and placed along the left or right side of its container, where text and inline elements will wrap around it. A floating element is one where the computed value of float is not none. (MDN)
  • 6. Play Time - floats https://plnkr.co/edit/Bz4hS2E9e mkbr3fvts16?p=preview Floats Tricky Parts
  • 7. Float - summary 1. When an element is floated it is taken out of the normal flow of the document. It is shifted to the left or right until it touches the edge of its containing box or another floated element. 2. Float elements must be given explicit width, otherwise unexpected behavior could occur. 3. Non-positioned, non-floated, block-level elements act as if the floated element is not there. 4. To make parent of floats take it height, we must clear the floats, a trick could be adding overflow: auto. 5. Bootstrap grid system is float based.
  • 8. Positioning 1. Positioning allows you to move an element around the document. 2. We have 4 types of Positioning: a. Static (default) b. Relative c. Absolute d. Fixed
  • 9. Static The default value for all elements, represents the normal document flow. Position related attributes have no effect (top, left, etc…) Only usage is to override and reset other position values. Isn’t affected by z-index.
  • 10. Relative This keyword lays out all elements as though the element were not positioned, and then adjust the element's position, without changing layout (and thus leaving a gap for the element where it would have been had it not been positioned (MDN) - The Element starts where it should have been - You can move it from it’s original position via (top, left, etc…) - limits the scope of absolutely positioned child elements
  • 11. Absolute Do not leave space for the element. Instead, position it at a specified position relative to its closest positioned ancestor if any, or otherwise relative to the initial containing block. Absolutely positioned boxes can have margins, and they do not collapse with any other margins. (MDN) - The element is out of the browser flow, meaning it won’t be affected by other elements. - Also it won’t affect other elements flow. - Will be relative to it closest non static parent, or to the html tag itself if none found.
  • 12. Fixed Do not leave space for the element. Instead, position it at a specified position relative to the screen's viewport and don't move it when scrolled. When printing, position it at that fixed position on every page. This value always create a new stacking context (MDN).
  • 13. Play Time - Positions https://plnkr.co/edit/JosJySkjT7V Ah4xuTp98?p=preview
  • 14. Collapsing Margins CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin. (W3C SPEC) In short: when two topbottom margins touch each other they collapse 1. The higher margin is taken, and the smaller collapse to 0. 2. Negative and positive margins are added up rather than collapse. 3. Two Negative margins collapse, with the smaller one taken.
  • 15. Play Time - Collapsing Margins https://plnkr.co/edit/wvaBa6A7R 9snWJVxoIOP?p=preview
  • 16. Stacking context is the three-dimensional conceptualization of HTML elements along an imaginary z-axis relative to the user who is assumed to be facing the viewport or the webpage. HTML elements occupy this space in priority order based on element attributes. (MDN) Elements Order without z-index, are formed by the HTML hierarchy: 1. Last elements in HTML order will be displayed on top. 2. Positioned Elements have higher priority over static elements, which is strong then html order. 3rd Dimension, stacking context
  • 17. Play Time - Stacking Context no z-index. https://plnkr.co/edit/9ygOvS1BiB qsyatxcCML?p=preview
  • 18. Positioning and assigning a z-index value to an HTML element creates a stacking context, (as does assigning non-full opacity). Stacking contexts can be contained in other stacking contexts, and together create a hierarchy of stacking contexts. Each stacking context is completely independent from its siblings: only descendant elements are considered when stacking is processed. Each stacking context is self-contained: after the element's contents are stacked, the whole element is considered in the stacking order of the parent stacking context. Z-index & multi hierarchy stacks
  • 19. Play Time - Stacking Contexts https://plnkr.co/edit/tzW4NsFWA Hu0TLjAWEO1?p=preview MDN menu example
  • 20. Stacking Context - Summary 1. Stacking Context, is the concept of how the browser renders elements in the 3rd dimension (facing browser window). 2. By default context order is derived from html order (last element on top) 3. Default order gives priority for positioned elements over static elements. 4. Each context can host more contexts, each stacking context is self-contained. 5. New contexts are created by certain conditions a. HTML root
  • 21. Other Worth Knowing... 1. Css Transform 2. Css Pseudo Elements, check this 3. MDN, anytime you are not sure about what's going on...

Editor's Notes

  1. https://github.com/vasanthk/css-refresher-notes
  2. https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Introduction
  3. https://developer.mozilla.org/en-US/docs/Web/CSS/float
  4. https://developer.mozilla.org/en-US/docs/Web/CSS/float
  5. https://github.com/vasanthk/css-refresher-notes#positioning https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Introduction
  6. https://github.com/vasanthk/css-refresher-notes#positioning https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Introduction
  7. https://github.com/vasanthk/css-refresher-notes#positioning https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Introduction
  8. https://github.com/vasanthk/css-refresher-notes#positioning https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Introduction
  9. https://github.com/vasanthk/css-refresher-notes#positioning https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Introduction
  10. https://github.com/vasanthk/css-refresher-notes#positioning https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Introduction
  11. https://github.com/vasanthk/css-refresher-notes#positioning https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Introduction
  12. https://developer.mozilla.org/en-US/docs/Web/CSS/float