โœจ CSS

by Anatoliy Belobrovik

๐Ÿ“ Lecture plan

CSS Cascade

CSS Specificity Quiz 1/8

                    section p {
                      color:  red;
                    }
                    .p1 {
                      color:  green;
                    }
            
                    <section class="section" id="section">
                      <h1>Title</h1>
                      <p class="p1">Paragraph 1</p>
                      <p class="p2">Paragraph 2</p>
                    </section>
            

๐Ÿ’ก The specificity of section p is 0.0.0.0.2 and .p1 is 0.0.0.1.0

CSS Specificity Quiz 2/8

                    .section p {
                      color:  red;
                    }
                    .p1 {
                      color:  green;
                    }
            
                    <section class="section" id="section">
                      <h1>Title</h1>
                      <p class="p1">Paragraph 1</p>
                      <p class="p2">Paragraph 2</p>
                    </section>
            

๐Ÿ’ก The specificity of .section1 p is 0.0.0.1.1 and .p1 is 0.0.0.1.0

CSS Specificity Quiz 3/8

                #section {
                  color:  red;
                }
                .p1 {
                  color:  green;
                }
        
                <section class="section" id="section">
                  <h1>Title</h1>
                  <p class="p1">Paragraph 1</p>
                  <p class="p2">Paragraph 2</p>
                </section>
        

๐Ÿ’ก p would inherit its parent's color only if there is no color applied directly to paragraphs

CSS Specificity Quiz 4/8

                #section * {
                  color:  red;
                }
                .p1 {
                  color:  green;
                }
        
                <section class="section" id="section">
                  <h1>Title</h1>
                  <p class="p1">Paragraph 1</p>
                  <p class="p2">Paragraph 2</p>
                </section>
        

๐Ÿ’ก The specificity of #section1 * is 0.0.1.0.0 and .p1 is 0.0.0.1.0

CSS Specificity Quiz 5/8

                    section * {
                      color:  red;
                    }
                    p {
                      color:  green;
                    }
            
                    <section class="section" id="section">
                      <h1>Title</h1>
                      <p class="p1">Paragraph 1</p>
                      <p class="p2">Paragraph 2</p>
                    </section>
            

๐Ÿ’ก The specificity of section * is 0.0.0.0.1. The specificity of p is also 0.0.0.0.1 but it comes later in the stylesheet.

CSS Specificity Quiz 6/8

                    section .p1 {
                      color:  red;
                    }
                    section :nth-child(2) {
                      color:  green;
                    }
            
                    <section class="section" id="section">
                      <h1>Title</h1>
                      <p class="p1">Paragraph 1</p>
                      <p class="p2">Paragraph 2</p>
                    </section>
            

๐Ÿ’ก The specificity of section .p1 is 0.0.0.1.1. The specificity of section :nth-child(2) is also 0.0.0.1.1 but it comes later in the stylesheet.

CSS Specificity Quiz 7/8

                    section > p {
                      color:  red;
                    }
                    section p {
                      color:  green;
                    }
            
                    <section class="section" id="section">
                      <h1>Title</h1>
                      <p class="p1">Paragraph 1</p>
                      <p class="p2">Paragraph 2</p>
                    </section>
            

๐Ÿ’ก The specificity of section > p is 0.0.0.0.2. The specificity of section p is also 0.0.0.0.2 but it comes later in the stylesheet.

CSS Specificity Quiz 8/8

                    .red {
                      color:  red;
                    }
                    .green {
                      color:  green;
                    }
            
                    <section class="section" id="section">
                      <p class="red green">Paragraph</p>
                    </section>
            

๐Ÿ’ก The specificity of .red is 0.0.0.1.0. The specificity of .green is also 0.0.0.1.0 but it comes later in the stylesheet.

๐Ÿค“ CSS Cascade definition

The cascade takes an unordered list of declared values for a given property on a given element, sorts them by their declarationโ€™s precedence as determined below, and outputs a single cascaded value.

CSS WG

Cascade sorting order

ยซะะธะบั‚ะพ ะฝะต ะทะฝะฐะตั‚ CSS: ัะฟะตั†ะธั„ะธั‡ะฝะพัั‚ัŒ โ€” ะฝะต ะบะฐัะบะฐะดยป โ€” ะ˜ะปัŒั ะกั‚ั€ะตะปัŒั†ะธะฝ

Origin and Importance

  1. Transition declarations
  2. !important user agent declarations
  3. !important user declarations
  4. !important author declarations
  5. Animation declarations
  6. Normal author declarations
  7. Normal user declarations
  8. Normal user agent declarations

Specificity

Specificity Calculator

Order of Appearance

The last declaration in document order wins.

CSS Modules

๐Ÿ™… ๐Ÿš“๐Ÿ‘ฎ

            
                body { ... }
                body div.content div.container { ... }
                body div.content div.container div.articles { ... }
                body div.content div.container div.articles > div.post { ... }
                body div.content div.container div.articles > div.post div.title { ... }
                body div.content div.container div.articles > div.post div.title h1 { ... }
                body div.content div.container div.articles > div.post div.content { ... }
            
        

โ˜„๏ธ Existing solutions

CSS Modules & CSS-in-JS

ยซะ’ ะฟะพะธัะบะฐั… ะกั‚ะธะปะตะฒะพะณะพ ะ“ั€ะฐะฐะปัยป โ€” ะั€ั‚ัƒั€ ะšะตะฝะถะฐะตะฒ

CSS Modules

... are local by default ๐Ÿ“ฆ

            .Block { background-color:  plum; } // declare css

            import styles from "./styles.module.css" // import a css module
            function Block() { // apply the class name
              return <div class={styles.Block}>Component</div>
            }
            
            <div class="Block-9gbnd5">Component</div>// result
        

CSS Modules

Styled Components

            import styled from "styled-components"
            let Block = styled.div`
              background-color:  plum;
            `
            function Component() {
              return <Block>Component</Block>
            }
            
            <div class="Block-9gbnd5">Component</div>// result
        

... all might of JS ๐Ÿ’ช

            import styled, { css } from "styled-components"

            let Button = styled.button`
              background:  black;
              color:  white;
              ${props => props.primary && css`
                background:  white;
                color:  black;
              `}
            `
        

Styled Components

CSS Custom Properties

CSS Custom Properties

            :root {
              --bg-color:  plum; // declare a custom variable
              --base-padding: 0.5rem;
            }

            .Component {
              background-color: var(--bg-color); // use the custom variable
              padding: calc(2 * var(--base-padding));
            }
        

๐Ÿ‘จโ€๐Ÿ’ป [Practice] CSS Custom Properties

  1. Convert it to CSS Custom Properties
  2. (optional) Make it responsive - change base font size on different screen sizes

type-scale.com

Comparison to CSS preprocessors

Preprocessors variables are...

  • Both global & local
  • Static
  • Not flexible

CSS Custom Properties are...

  • Global
  • Dynamic
  • Accessible from JS land
  • CSS as API

CSS Custom Properties usecases

JavaScript API

            let element = document.querySelector("#element")
            let styles = getComputedStyle(element)
            let size = +styles.getPropertyValue("--base-size")
            element.style.setProperty("--base-size", size + 4)
        

CSS Custom Properties

CSS Layouts Flexbox & Grid

Responsive Design could be achived with...

CSS Flexbox API

A Guide to Flexbox

initial state

            <div class="container">
                <div class="item">First</div>
                <div class="item">Second</div>
                <div class="item">Third</div>
                <div class="item">Fourth</div>
            </div>
        

initial state

First
Second
Third
Fourth

display: flex

            .container {
                    display: flex;
            }
        

display: flex

First
Second
Third
Fourth

flex-direction: row-reverse

            .container {
                display: flex;
                flex-direction: row-reverse;
            }
        

flex-direction: row-reverse

First
Second
Third
Fourth

flex-direction: column

            .container {
                display: flex;
                flex-direction: column;
            }
        

flex-direction: column

First
Second
Third
Fourth

flex-direction: column-reverse

            .container {
                display: flex;
                flex-direction: column-reverse;
            }
        

flex-direction: column-reverse

First
Second
Third
Fourth

justify-content: flex-start (default)

            .container {
                display: flex;
                justify-content: flex-start;
            }
        

justify-content: flex-start (default)

First
Second
Third
Fourth

justify-content: center

            .container {
                display: flex;
                justify-content: center;
            }
        

justify-content: center

First
Second
Third
Fourth

justify-content: flex-end

            .container {
                display: flex;
                justify-content: flex-end;
            }
        

justify-content: flex-end

First
Second
Third
Fourth

justify-content: space-between

            .container {
                display: flex;
                justify-content: space-between;
            }
        

justify-content: space-between

First
Second
Third
Fourth

justify-content: space-around

            .container {
                display: flex;
                justify-content: space-around;
            }
        

justify-content: space-around

First
Second
Third
Fourth

justify-content: space-evenly

            .container {
                display: flex;
                justify-content: space-evenly;
            }
        

justify-content: space-evenly

First
Second
Third
Fourth

align-items: stretch (default)

            .container {
                display: flex;
                align-items: stretch;
            }
        

align-items: stretch (default)

First
Second
Third
Fourth

align-items: flex-start

            .container {
                display: flex;
                align-items: flex-start;
            }
        

align-items: flex-start

First
Second
Third
Fourth

align-items: center

            .container {
                display: flex;
                align-items: center;
            }
        

align-items: center

First
Second
Third
Fourth

align-items: flex-end

            .container {
                display: flex;
                align-items: flex-end;
            }
        

align-items: flex-end

First
Second
Third
Fourth
                .container {
                    display: flex;
                    align-items: center;
                }
                .item:nth-child(1) {
                    align-self: flex-start;
                }
                .item:nth-child(4) {
                    align-self: flex-end;
                }
        

align-self

First
Second
Third
Fourth

Order

                .item {
                    order: 1;
                }
                .item:nth-child(1) {
                    order: 2;
                }
                .item:nth-child(4) {
                    order: -1;
                }
        

Order

First
Second
Third
Fourth

flex-grow

            .container {
                display: flex;
            }
            .item {
                flex-grow: 0; /* By default */
                flex-grow: 1;
            }
        

flex-grow

First
Second
Third
Fourth
            .container {
                display: flex;
            }
            .item {
                flex-grow: 1;
            }
            .item:nth-child(1) {
                flex-grow: 3;
            }
        

flex-grow

First
Second
Third
Fourth

flex-shrink

Codesandbox

flex-basis

Codesandbox

Difference between flex-basis and width

flex-wrap: nowrap

First
Second
Third
Fourth
Fifth
Sixth
Seventh
Eigth

flex-wrap: wrap

            .container {
                display: flex;
                align-items: flex-start;
                flex-wrap: wrap;
            }
        

flex-wrap: wrap

First
Second
Third
Fourth
Fifth
Sixth
Seventh
Eigth

align-content

            .container {
                display: flex;
                align-items: flex-start;
                flex-wrap: wrap;
                align-content: center;
            }
        

align-content

First
Second
Third
Fourth
Fifth
Sixth
Seventh
Eigth

๐Ÿ‘จโ€๐Ÿ’ป [Practice] Flexbox

๐Ÿ‘‰ Implement Calendar component with CSS Flexbox

Codesandbox

CSS Grid API

Complete guide to Grid

CSS Grid Terminology

Grid lines

Grid row (track)

Grid column (track)

Grid area

Grid cell

initial state

            <div class="container">
                <div class="item">First</div>
                <div class="item">Second</div>
                <div class="item">Third</div>
            </div>
        

initial state

First
Second
Third

display: grid

            .container {
                display: grid;
                gap: 0.5rem; /* row-gap column-gap */
            }
        

display: grid

First
Second
Third

grid-template-columns: 1fr 1fr

            .container {
                display: grid;
                grid-template-columns: 1fr 1fr;
            }
        

grid-template-columns: 1fr 1fr

First
Second
Third

grid-template-rows: 1fr 1.5fr 2fr

            .container {
                display: grid;
                grid-template-rows: 1fr 1.5fr 2fr;
            }
        

grid-template-rows: 1fr 1.5fr 2fr

First
Second
Third
            .container {
                display: grid;
                grid-template-rows: 1fr 1.5fr;
                grid-template-columns: 1fr 2fr 1fr;
            }
        
First
Second
Third
Fourth

grid-rows / grid-columns

            .container {
                display: grid;
                grid-template-rows: repeat(4, 1fr);
                grid-template-columns: 1fr 1fr;
            }
            .item:nth-child(1) {
                grid-row: 1 / 3; /* grid-row-start / grid-row-end */
                grid-column: span 2; /* grid-column-start / grid-column-end */
            }
        

grid-rows / grid-columns

First
Second
Third

grid-template-area

Header
Sidebar
Content
Footer
                .container {
                    display: grid;
                    grid-template-areas:
                    "header header header"
                    "content content sidebar"
                    "content content sidebar"
                    "footer footer footer";
                }
            
                .header {
                    grid-area: header;
                }
                .sidebar {
                    grid-area: sidebar;
                }
                .content {
                    grid-area: content;
                }
            

grid-template-area

Header
Sidebar
Content
Footer

Implicit grid

When grid items are positioned outside of these bounds, the grid container generates implicit grid tracks by adding implicit grid lines to the grid. These lines together with the explicit grid form the implicit grid. The grid-auto-rows and grid-auto-columns properties size these implicit grid tracks, as well as any explicit grid tracks created by grid-template-areas but not explicitly sized by grid-template-rows or grid-template-columns

CSS WG

Implicit grid

Codesandbox 1

Codesandbox 2

๐Ÿ‘จโ€๐Ÿ’ป [Practice] Grid

๐Ÿ‘‰ Implement Calendar component with CSS Grid

Codesandbox

CSS Grid vs CSS Flexbox

Flexbox layout CSS Grid layout

Useful links

Games

  1. Flexbox Froggy
  2. Grid Garden

Articles

  1. Grid by Example by Rachel Andrew
  2. Understanding CSS Grid Container
  3. Masonry with CSS by Tobias Ahlin Bjerrome
  4. Blog by Ahmad Shadeed

PostCSS

PostCSS is a tool for transforming CSS

A searchable catalog of PostCSS plugins

Tools built with PostCSS

... and many more!

Autoprefixer

Adds vendor prefixes to CSS rules using values from Can I Use

Stylelint

A mighty, modern linter that helps you avoid errors and enforce conventions in your styles.

ะะฝะดั€ะตะน ะกะธั‚ะฝะธะบ - Stylelint: ะšะฐะบ ะธ ะทะฐั‡ะตะผ ะปะธะฝั‚ะธั‚ัŒ CSS

TailwindCSS

PostCSS Preset Env

Lets you convert modern CSS into something most browsers can understand, determining the polyfills you need based on your targeted browsers or runtime environments, using cssdb

The End