CSS Layouts
class: center, middle .title[ Front-end training # CSS Layouts ] --- class: center # CSS Layouts: Intent
--- class: center, top # Box Model
--- class: center, top # Box Model
--- class: left, top # Box Model: width & height The size of the content box of: - text inside the box - size of children' boxes
max-width
max-height
min-width
min-height
--- class: left, top # Box Model: paddings
padding: 10px 20px 30px 40px;
padding-top: 10px;
padding-right: 20px;
padding-bottom: 30px;
padding-left: 40px;
--- class: left, top # Box Model: border
border: 1px solid black;
border-width: 1px;
border-style: solid;
border-color: black;
border-top: 1px solid black;
border-top-width: 1px;
border-top-style: solid;
border-top-color: black;
...
--- class: left, top # Box Model: margin
margin: 10px 20px 30px 40px;
margin-top: 10px;
margin-right: 20px;
margin-bottom: 30px;
margin-left: 40px;
--- #Margin collapsing: Adjacent siblings ```html
Margin between these two containers will be...
... 30px?
``` --- #Margin collapsing: Adjacent siblings ```html
bottom margin of this container...
...will be combined with top margin of this one.
```
--- #Margin collapsing: Adjacent siblings ```html
bottom margin of this container...
...will be combined with top margin of this one.
```
--- #Margin collapsing: Parent and first/last child ```html
The top margin of #child and #parent will be combined into one margin of 10px.
```
--- #When the parent and first/child margin collapsing will not apply? - border, padding, inline content or clearance on top - border, padding, inline-content, height, min-height, max-height on bottom
--- #Margin collapsing: Empty blocks ```html
```
--- #Margin collapsing: Empty blocks ```html
```
##When the empty blocks margin collapsing will not apply? - border - padding - inline content - height - min-height --- #Overflow ```html
``` ```css .parent { border: 4px red dotted; width: 500px; height: 500px; } .child { border: 4px solid green; height: 400px; width: 700px; } ```
--- #Overflow: visible (default) ```html
``` ```css .parent { border: 4px red dotted; width: 500px; height: 500px; overflow: visible; } .child { border: 4px solid green; height: 400px; width: 700px; } ```
--- #Overflow: hidden ```html
``` ```css .parent { border: 4px red dotted; width: 500px; height: 500px; overflow: hidden; } .child { border: 4px solid green; height: 400px; width: 700px; } ```
--- #Overflow: auto ```html
``` ```css .parent { border: 4px red dotted; width: 500px; height: 500px; overflow: auto; } .child { border: 4px solid green; height: 400px; width: 700px; } ```
--- #Overflow: scroll ```html
``` ```css .parent { border: 4px red dotted; width: 500px; height: 500px; overflow: scroll; } .child { border: 4px solid green; height: 400px; width: 700px; } ```
--- #Outline ```css padding: 50px; border: 50px solid green; margin: 50px; outline: 25px solid red; ```
--- #Types of CSS boxes: block ```html
I am text before a child
I am a child
I am text after child
``` ```css div > div { display: block; /* default */ width: 200px; height: 80px; padding: 20px; margin: 50px; border: 2px blue solid; background-color: yellow; } ```
I am text before a child
I am a child
I am text after child
--- #Types of CSS boxes: inline ```html
I am text before a child
I am a child
I am text after child
``` ```css div > div { display: inline; width: 200px; height: 80px; padding: 20px; margin: 50px; border: 2px blue solid; background-color: yellow; } ```
I am text before a child
I am a child
I am text after child
--- #Types of CSS boxes: inline-block ```html
I am text before a child
I am a child
I am text after child
``` ```css div > div { display: inline-block width: 200px; height: 80px; padding: 20px; margin: 50px; border: 2px blue solid; background-color: yellow; } ```
I am text before a child
I am a child
I am text after child
---
Block HTML elements
p
h1, h2, h3, h4, h5, h6
ol, ul
pre
address
blockquote
dl
div
fieldset
form
hr
noscript
table
Inline HTML elements
b, big, i, small, tt
abbr, acronym, cite, code, dfn, em, kbd, strong, samp, var
a, bdo, br, img, map, object, q, script, span, sub, sup
button, input, label, select, textarea
--- #box-sizing: content-box | border-box
--- #Floating ```css img { float: left; margin: 0 30px 0 0; } ```
--- #Floating: two columns .columns[.col-50[ ```css div:nth-of-type(1) { width: 48%; float: left; } ``` ].col-50[ ```css div:nth-of-type(2) { width: 48%; float: right; } ``` ]]
--- #Floating: three columns .columns[.col-30[ ```css div:nth-of-type(1) { width: 36%; float: left; } ``` ].col-30[ ```css div:nth-of-type(2) { width: 36%; float: left; } ``` ].col-40[ ```css div:nth-of-type(3) { width: 26%; float: right; } ``` ]]
--- #Clearing floats - left - right - both --- class: left # Floating specifics The floated element automatically asumess **display: block**. If the parent element contains only floated elements, it's **height will equal to 0**
--- #Normal document flow - 100% parent's width and content's height (block) - content's width and content's height (inline) - add layers of box model - vertically - block - horizontally or wrapped - inline - margin collapsing --- #Positioning - override normal flow - change element's position - place one element over another one - fix element's position in a browser window when a page is scrolled --- #position: static (default) ```css div { position: static; /* normal flow */ } ``` .columns[.col-50[ ```html
```].col-50[ ```css div { width: 400px; height: 50px; border: 1px red solid; margin: 10px; } ```]]
--- #position: relative ```css div:nth-of-type(2){ position: relative; border: 1px dotted green; } ```
--- #position: relative ```css div:nth-of-type(2){ position: relative; border: 1px dotted green; top: 20px; left: 20px; /** bottom: Xpx; right: Ypx; **/ } ```
--- #position: absolute ```css div:nth-of-type(2){ position: absolute; border: 2px dotted green; } ```
--- #position: absolute (oops...) ```css div:nth-of-type(2){ position: absolute; border: 2px dotted green; top: 20px; left: 20px; } ```
--- #position: absolute (oops...) ```css div:nth-of-type(2){ position: absolute; border: 2px dotted green; top: 20px; right: 20px; } ```
--- #position: absolute - removed from normal flow - gets start of coordinates from the relatively positioned element - saves margins - margin collapsing does not apply --- #position: absolute - overlapping ```html
```
--- #z-index ```html
```
--- #z-index ```html
```
--- #position: fixed ```html
Fixed element
Simple element 1
Simple element 2
Simple element 3
...
```
Fixed element
Simple element 1
Simple element 2
Simple element 3
Simple element 4
Simple element 5
Simple element 6
Simple element 7
Simple element 8
Simple element 9
Simple element 10
Simple element 11
Simple element 12
--- #position: sticky ```html
E
Explosions In The Sky
T
Ted Leo & The Pharmacists
...
``` .demo.demo-1[
A
Andrew W.K.
Apparat
Arcade Fire
At The Drive-In
Aziz Ansari
C
Chromeo
Common
Converge
Crystal Castles
Cursive
E
Explosions In The Sky
T
Ted Leo & The Pharmacists
T-Pain
Thrice
TV On The Radio
Two Gallants
] --- #Modern layouts: intent - size - order
may depend on
- content - screen resolution - screen orientation --- # Flexboxes Flexbox model is a 1-dimensional system, that operates with: - flex items, the elements being controlled in size and way of alignment - flex container, which contains flex items - flow direction, which determines the layout direction of flex items
[Extended guide to Flexbox](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) --- # Grid Grid model is a 2-dimensional system, that operates with: - grid container, parent element which contains grid items - grid items, direct descendants of the grid container - grid line: vertical ("column grid lines") or horizontal ("row grid lines") - grid track, space between two adjacent grid lines(columns or rows) - grid cell, space between two adjacent row and two adjacent column grid lines - grid are, the total space surrounded by four grid lines
[Extended guide to Grid](https://css-tricks.com/snippets/css/complete-guide-grid/) --- # Useful links ## General - [CSS-Tricks Almanac](http://css-tricks.com/almanac/) - [Learn Layout](http://learnlayout.com) - [.clear-fix](https://css-tricks.com/snippets/css/clear-fix/) ## Flex Module - [FlexBox Cheatsheet](http://vudav.github.io/flexbox-cheatsheet/) - [Flexbox Playground](http://the-echoplex.net/flexyboxes/) - [Practical tricks](https://hackernoon.com/11-things-i-learned-reading-the-flexbox-spec-5f0c799c776b) ## Grid Module - [Grid Cheatsheet](https://alialaa.github.io/css-grid-cheat-sheet/) - [Grid by Example](https://gridbyexample.com/) - [CSS Fractional Unit in a simple way](https://medium.com/flexbox-and-grids/the-css-fractional-unit-fr-in-approachable-plain-language-fdc47bd387f7) - [Grid Layout examples+](http://labs.jensimmons.com/)