Introduction to Modern CSS

A presentation at Frontstack in February 2019 in Lagos, Nigeria by Emmanuel Aina

Slide 1

Slide 1

Introduction to modern CSS Emmanuel Aina @horlah_codes Frontend Dev, BuyCoins

Slide 2

Slide 2

This talk is basically about the new technologies that have been introduced into CSS, with the aim for faster development process and accessibility

Slide 3

Slide 3

Modern CSS is simply how we have chosen to write CSS as a standard, utilizing the technologies being introduced and making sure they’re being used as expected for the purpose at which they are being created

Slide 4

Slide 4

Technologies being introduced ● CSS Flexbox ● CSS pre- and post-processors ● CSS Grid ● Feature Queries ● CSS Variables ● CSS Animation ● CSS Functions ● New Units - e.g. vw, vh, vmin, vmax ● CSS Methodologies

Slide 5

Slide 5

Let’s pick them one after the other

Slide 6

Slide 6

CSS Flexbox m av u …

Slide 7

Slide 7

CSS Flexbox CSS Flexbox was proposed in 2009 but did not get implemented in browser spread adoption until 2015. Flexbox was designed to define how space is distributed across a single column or row, which makes it a better candidate for defining layout compared to using floats - Peter Jang, Dean of Instruction @Actualize

Slide 8

Slide 8

Slide 9

Slide 9

Using Flexbox for the first the first time Structure of our html

Slide 10

Slide 10

Basic CSS without Flexbox

Slide 11

Slide 11

Expected output

Slide 12

Slide 12

Basic CSS with Flexbox

Slide 13

Slide 13

Output Default properties: flex-direction : row; flex-wrap: nowrap; justify-content : flex-start;

Slide 14

Slide 14

Understanding Flexbox The container The items

Slide 15

Slide 15

Understanding Flexbox .container { display: flex; }

Slide 16

Slide 16

Understanding Flexbox .container { display: flex; justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly; }

Slide 17

Slide 17

Understanding Flexbox .container { display: flex; flex-wrap: nowrap | wrap | wrap-reverse; }

Slide 18

Slide 18

Understanding Flexbox .container { display: flex; align-items: stretch | flex-start | flex-end | center | baseline;. }

Slide 19

Slide 19

Understanding Flexbox .container { display: flex; align-content : flex-start | flex-end | center | space-between | space-around | stretch; }

Slide 20

Slide 20

Understanding Flexbox .items { align-self: auto | flex-start | flex-end | center | baseline | stretch; }

Slide 21

Slide 21

Understanding Flexbox .items { flex-grow: <number>; }

Slide 22

Slide 22

CSS Grid The s…

Slide 23

Slide 23

CSS Grid CSS Grid was proposed in 2011. It started getting adopted into browsers in 2018. CSS Grid Layout (aka “Grid”), is a two-dimensional grid-based layout system that aims to do nothing less than completely change the way we design grid-based user interfaces. - css-tricks.com

Slide 24

Slide 24

.container { display: grid | inline-grid; }

Slide 25

Slide 25

.container { grid-template-columns: <track-size>; grid-template-rows: <track-size>; }

Slide 26

Slide 26

.container { grid-template-areas: “<grid-area-name> <grid-area-name> <grid-area-name>” “…”; }

Slide 27

Slide 27

.item-a { grid-area: } .item-b { grid-area: } .item-c { grid-area: } .item-d { grid-area: } header; main; sidebar; footer; .container { display: grid; grid-template-columns: 50px 50px 50px 50px; grid-template-rows: auto; grid-template-areas: “header header header header” “main main . sidebar” “footer footer footer footer”; }

Slide 28

Slide 28

.container { grid-column-gap: <line-size>; grid-row-gap: <line-size>; }

Slide 29

Slide 29

CSS Variables sa , re i …

Slide 30

Slide 30

CSS Variables CSS Variables are simple entities that are used to save values for repeated use within a CSS document. Think about it like Javascript variable when you save a string, int or number and call it for use somewhere else, only that the declaration and usage is different.

Slide 31

Slide 31

Example: Declaration —main-color: black; Usage var(—main-color);

Slide 32

Slide 32

CSS Variables with Javascript We might want to get our CSS variable value from javascript for some interesting DOM manipulation, here is how:

Slide 33

Slide 33

// get variable from inline style element.style.getPropertyValue(“—my-var”); // get variable from wherever getComputedStyle(element).getPropertyValue(“—my-var”); // set variable on inline style element.style.setProperty(“—my-var”, jsVar + 4);

Slide 34

Slide 34

CSS Functions re s es s …

Slide 35

Slide 35

CSS Functions CSS Functions, yeah functions. They are readily built methods that makes implementing and achieving some functionalities easy. Don’t be confused, they are things we already know and make use of. Yes!

Slide 36

Slide 36

CSS Functions: rgba() rgb() calc() linear-gradient() repeat() and more…

Slide 37

Slide 37

CSS Methodologies For t tut g…

Slide 38

Slide 38

CSS Methodologies We often have our personal way of implementing our CSS styles, naming our class and id. A lot of complications arrive when it comes to sharing our code or having a new dev on the team who also has a personal preference of CSS.

Slide 39

Slide 39

Some already existing CSS methodologies include: ● OOCSS ● SMACSS ● BEM ● Atomic CSS

Slide 40

Slide 40

let’s pick one…

Slide 41

Slide 41

SMACSS - Scalable and Modular Architecture of CSS Every project need some organisation. We should not throw styles on any elements the way we please no matter how small. SMACSS methodology is about breaking down your styles into scalable modules which will ease development process and updates deployment.

Slide 42

Slide 42

Breaking down styles shouldn’t be just as the spirit leads, but rather how it can be understood and maintained. SMACSS has some core categorization rules with patterns that help in structuring styles: 1. 2. 3. 4. 5. Base Layout Module State Theme

Slide 43

Slide 43

Base They are the default styles. They are more of styles we reset some default styles exclusive to certain HTML elements, the other part of it are styles we want to apply to elements generally (platform wide).

Slide 44

Slide 44

Layout They are styles that are used to structure layouts. They are styles that are applied to section, header, footer, aside e.tc to define their placement on browsers.

Slide 45

Slide 45

Modules They are the reusable styles. Modules contain styles for elements like buttons, lists, links e.tc that can be used and reused in different places all through the project

Slide 46

Slide 46

State They are styles that are used to determine how layouts and modules will look in different states e.g. collapsed menu, desktop view, tablet view e.t.c

Slide 47

Slide 47

Theme This is very similar to the state rules in that it also determine the looks of modules and layout but instead of also focusing on the state of elements, theme styles are about more of the looks like the color variables and it’s likes, 😉.

Slide 48

Slide 48

Naming system is also as important as structuring the styles, with SMACSS prefixes + hyphens ( - ) are used to name classes and ids while the prefixes are based on categories e.g For layouts: layout-, l- e.t.c.

Slide 49

Slide 49

CSS Pre and Post Processors we m o h a l …

Slide 50

Slide 50

CSS Preprocessors and Postprocessors Writing CSS got easier with preprocessors and postprocessors.

Slide 51

Slide 51

CSS preprocessors controls how the CSS file will look like, they have their own structure and pattern. Their syntax is usually simpler, easier, structural, and they provide more functions. They make implementing CSS methodology easier because they support breaking down styles into modules seamless.

Slide 52

Slide 52

CSS preprocessors pickup files with their file extension, process them to be sure none there syntax rules are disobeyed, then converts them to standard CSS files that are understood by our browsers. Simple!

Slide 53

Slide 53

They are configurable too for, so you have total power over it including how you want the processor file to be converted, which file should the processor watch out for, enable auto conversion, what your converted CSS should be named and many more Some of the preprocessor we have includes: ● SASS ● LESS ● Stylus

Slide 54

Slide 54

CSS postprocessors are processors that help to optimise CSS code by, for example, adding backwards compatibility support (for older browsers) and CSS property prefixes to properties yet to be supported by some browsers. When you write your CSS normally as you should, the processor converts them into a much more cross-browser compatible CSS code all according your installed configuration.

Slide 55

Slide 55

Postprocessors help to ease the stress of checking out auto prefixes for certain CSS properties, once you save your CSS file the post-processor picks it, add the necessities you might have missed and renders the output as your file name configure.

Slide 56

Slide 56

Feature Queries if t e t c …

Slide 57

Slide 57

Feature Queries Think about it like writing if statements in Js, you want to carry out an action if and only if a certain condition is met. Yeah that’s now possible with CSS, the conditions are based on if certain features are available for use within that browser.

Slide 58

Slide 58

An example of feature query in CSS, letting CSS load a style if initial-letter is supported by the browser @supports (initial-letter: 4) or (-webkit-initial-letter: 4) { p::first-letter { -webkit-initial-letter: 4; initial-letter: 4; color: #FE742F; font-weight: bold; margin-right: 0.5em; } }

Slide 59

Slide 59

And what if we want to execute another CSS if the feature is not supported @supports not(initial-letter: 4) { p::first-letter { color: #FE742F; font-weight: bold; margin-right: 0.5em; } }

Slide 60

Slide 60

CSS Animations ma g s u n o n…

Slide 61

Slide 61

CSS Animations This is simply about making the style of a certain element(s) change from one state to another in a way that makes a platform interesting and attractive. Although not all CSS properties supports animation, but still CSS animation is a super power in the hands of developers. 😉

Slide 62

Slide 62

CSS animations to be applied on an element or set of elements are stored in keyframes with a unique name as there can be more than one keyframe in one CSS document. The keyframe is then called on the animation CSS property of the HTML element(s). An example CSS keyframe: @keyframes example { from {background-color: red;} to {background-color: yellow;} }

Slide 63

Slide 63

Using the example keyframe in an animation can be as easy as: div { width: 100px; height: 100px; background-color: red; animation-name: example; animation-duration: 4s; }

Slide 64

Slide 64

You can do more… div { width: 100px; height: 100px; background-color: red; animation-name: example; animation-duration: 4s; animation-timing-function: linear; animation-delay: 2s; animation-iteration-count: infinite; animation-direction: alternate; }

Slide 65

Slide 65

CSS Units re n en j go r i r in …

Slide 66

Slide 66

CSS Units This is one very important part writing CSS. Using the right unit and values for the right element. “Using the right unit saves and reduces the number of media queries you’ll have to write.”

Slide 67

Slide 67

Absolute Units Relative Units They are units that are not relative to any element or body. They can be further understood as independent unit as whatever they are being attributed to doesn’t depend on anything to attribute it’s length, width or size. They are units that are relative to their parent body or element. They can be further understood as a dependent unit as whatever they are being attributed to depends on its parent properties to determine its own properties. Elements they are applied to are always responsive. Examples of this include: px, cm, mm, in, pt Examples of this include: em, rem, %(percentage), vh, vw, vmin, vmax e.t.c

Slide 68

Slide 68

New Units vh - Virtual height, relative to the font-size of the element vw - Virtual width, relative to 1% of the width of the viewport vmin - Virtual minimum, relative to 1% of viewport, smaller dimension vmax - Virtual maximum, relative to 1% of viewport, larger dimension rem - Relative with the root font size of the page em - Relative with the font size of the element

Slide 69

Slide 69

Thank you! @horlah_codes