ADVANCED CSS

Maksym Kolisnyk

EPAM

Agenda

  • CSS Selectors
  • CSS Variables
  • CSS Units
  • Round borders
  • Background attributes in CSS3
  • Shadows and Gradients
  • CSS Filters
  • Transformation with CSS in 2D...and 3D!
  • Transition/Animation
  • Irregular Shapes
  • Scroll Snap Points
  • Support queries
  • Will Change Module
  • Your questions...

CSS Basics

CSS Selectors Level 1

E { /* rules */ }
E.class { /* rules */ }
E#id { /* rules */ }

E:link { /* rules */ }
E:visited { /* rules */ }
E:active { /* rules */ }

E F { /* rules */ }

CSS Selectors Level 2

* { /* rules */ }
E[foo] { /* rules */ }
E[foo="bar"] { /* rules */ }
E[foo~="bar"] { /* rules */ }
E[foo|="en"] { /* rules */ }
E:lang() { /* rules */ }

E:hover { /* rules */ }
E:focus { /* rules */ }
E:first-child { /* rules */ }
E > F { /* rules */ }
E + F { /* rules */ }

CSS Selectors Level 3

E[foo^="bar"] { /* rules */ }
E[foo$="bar"] { /* rules */ }
E[foo*="bar"] { /* rules */ }

E:enabled { /* rules */ }
E:disabled { /* rules */ }
E:checked { /* rules */ }
E:empty { /* rules */ }

E ~ F { /* rules */ }

:root { /* rules */ }
E:nth-child(n) { /* rules */ }
E:last-child { /* rules */ }
E:nth-last-child(n) { /* rules */ }
E:only-child { /* rules */ }

E:first-of-type { /* rules */ }
E:nth-of-type(n) { /* rules */ }
E:last-of-type { /* rules */ }
E:nth-last-of-type(n) { /* rules */ }
E:only-of-type { /* rules */ }

CSS Selevtors Level 3/4

E:not(s) { /* rules */ }
E:is(s) /*renamed :matches*/ { /* rules */ }

E:default { /* rules */ }

E:in-range { /* rules */ }
E:out-of-range { /* rules */ }
E:required { /* rules */ }
E:optional { /* rules */ }

E:read-only { /* rules */ }
E:read-write { /* rules */ }

CSS Selectors Level 4

E:dir(ltr) { /* rules */ }
E:any-link { /* rules */ }
E:local-link { /* rules */ }
E:local-link(0) { /* rules */ }

E:indeterminate { /* rules */ }

E:nth-match(n of s) { /* rules */ }
E:nth-last-match(n of s) { /* rules */ }

E:column(s) { /* rules */ }
E:nth-col(n) { /* rules */ }
E:nth-last-col(n) { /* rules */ }

CSS Variables

Scheme of use

/* Declare somewhere a variable */
--variable-name: value;

/* Use variable in rule */
rule: var(--variable-name);

/* Doesn't matter where you declare, before/after using, it all the same works */

Variable usage

:root {
	--color: #39c2d7;
}

div {
	background-color: var(--color);
}

Variable scope

<div class="one">
  <div class="two">
    <div class="three"> </div>

    <div class="four"> </div>
  </div>
</div>
.two {
    --color: #a3c644; /* green */
}

.three {
    --color: #b22746; /* red */
}

.one, .three and .four divs have "background-color: --color;" rule

what backgrounds will we have in result?

3
4

CSS Units

Relative Lengths

em	/* relative to the font-size of the parent element */
ex	/* relative to height of the "x" in lowercase */
ch	/* relative to width of the "0" */
rem	/* relative to the font-size of root element */

vw	/* relative to 1% of the width of the viewport */
vh	/* relative to 1% of the height of the viewport */
vmin	/* relative to 1% of viewport's* smaller dimension */
vmax	/* relative to 1% of viewport's* larger dimension */

%	/* relative to the same property of the parent element */

Absolute Lengths

cm	/* centimeters */
mm	/* millimeters */

in	/* inches (1in = 96px = 2.54cm) */
px	/* pixels (1px = 1/96th of 1in) */

pt	/* points (1pt = 1/72 of 1in) */
pc	/* picas (1pc = 12 pt) */

10vh and 10vmin

96px and 1in

2em and 4.75rem and 200%

4pc and 48pt

Block Styling

Round Borders

border-radius rule

border-radius: all;

border-radius: tl-br tr-bl;

border-radius: tl tr-bl br;

border-radius: tl tr br bl;
tl tr br bl border-radius: 15px;
border-radius: 50px;
border-radius: 100% 0;
border-radius: 30% 20px 100%;
border-radius: 10% 20% 30% 40%;

different strength of rounding

border-radius: x/y;

border-top-left-radius: x y;
...
border-bottom-left-radius: x y;
border-top-left-radius: 70% 40%;

Background attributes

background: background-color
	    background-image
	    background-repeat
	    background-attachment
	    background-position;
		
	    background-clip
	    background-origin
	    background-size

background-attachment

background-attachment: fixed | scroll | local;
f i x e d
s c r o l l
l o c a l

background-clip

background-clip: padding-box | border-box | content-box;
padding-box
border-box
content-box

background-origin

background-origin: padding-box | border-box | content-box;
padding-box
border-box
content-box

background-size

background-size: px | % | auto | contain | cover;

/* or as short rule of background */
background: ... position/size ...;
background-size: 3em 10px;
background-size: contain;
background-size: cover;

Shadows

Box shadow

box-shadow: x y blur spread color inset;
w/ inset
w/o inset
(x; y) Padding-box shape Spread distance Border-box shape Spread distance Blur applied
box-shadow: 0 -50px 5px 0 rgba(255,0,0,0.5),
	    25px -40px 5px 0 rgba(255,127,0,0.5),
	    40px -25px 5px 0 rgba(255,255,0,0.5),
		... another 8 shadows ...
	    -40px -25px 5px 0 rgba(255,0,255,0.5),
	    -25px -40px 5px 0 rgba(255,0,127,0.5);

Text shadow

text-shadow: x y blur color;

Lorem ipsum

text-shadow: 0 0 1px #464547, 5px 5px 10px #1a9cb0;

Blur your text!

/* default font */
color: #39c2d7;

/* on hover */
color: transparent;
text-shadow: 0 0 20px #39c2d7;

HOVER ME!

Gradients

you can use linear or radial...

...and repeat it if you need

Linear

background-image: linear-gradient(dir, stop-color1, stop-color2, ...);

direction

to left
to bottom right
60deg
-135deg

colors

Multi-color
Transparency

color position

linear-gradient(to right, #39c2d7 10%, transparent 40%, transparent 60%, #b22746 90%);

Radial

background: radial-gradient(shape size at position, colors as in linear);

Shape

ellipse(default)
circle

Size

closest-side
farthest-side
closest-corner
farthest-corner

Repeating gradients

repeating-linear-gradient(to right, #464547, #464547 5%, #1a9cb0 15%, #39c2d7 20%);
repeating-linear-gradient(to right, #464547, #464547 5%, #1a9cb0 15%, #39c2d7 20%);

Patterns

background-color: #464547 no-repeat;
background-image: linear-gradient(to right, transparent 50%, #39c2d7 50%);
background-size: 5% 100%;
background-color: #464547;
background-image: linear-gradient(to right, transparent 50%, #39c2d7 50%);
background-size: 5% 100%;

CSS Filters

Filter rule

... or -webkit-filter

filter: blur(px)
	brightness(%)
	contrast(%)
	drop-shadow(x y blur spread color)
	grayscale(%)
	hue-rotate(deg)
	invert(%)
	opacity(%)
	saturate(%)
	sepia(%)
	url(svg-url#id);
filter: blur(5px);
filter: brightness(0.3);
filter: grayscale(1);
filter: contrast(2.3);
filter: hue-rotate(180deg);
filter: invert(0.7);
filter: saturate(0.3);
filter: sepia(1);

Blend Mode

mix blend mode values

normal;			multiply;
screen;			overlay;
darken;			lighten;
color-dodge;		color-burn;
hard-light;		soft-light;
difference;		exclusion;
hue;			saturation;
color;			luminosity;
mix-blend-mode: normal;
LOREM
mix-blend-mode: multiply;
LOREM
mix-blend-mode: screen;
LOREM
mix-blend-mode: overlay;
LOREM
mix-blend-mode: darken;
LOREM
mix-blend-mode: lighten;
LOREM
mix-blend-mode: color-dodge;
LOREM
mix-blend-mode: color-burn;
LOREM
mix-blend-mode: hard-light;
LOREM
mix-blend-mode: soft-light;
LOREM
mix-blend-mode: difference;
LOREM
mix-blend-mode: exclusion;
LOREM
mix-blend-mode: hue;
LOREM
mix-blend-mode: saturation;
LOREM
mix-blend-mode: color;
LOREM
mix-blend-mode: luminosity;
LOREM

Tranform/Animate

Transformations

Transform rule

transform: translate | scale | skew | rotate | matrix

Translate

transform: translate(X,Y); /* or traslateX(X) | translateY(Y) */
origin
translated

Scale

transform: scale(sX,sY); /* or scaleX(sx) | scaleY(sy) */
origin
scaled

Skew

transform: skew(Xdeg,Ydeg); /* or skewX(x) | skewY(y) */
origin
skewedX
origin
skewedY

Rotate

transform: rotate(deg); /* or rotateX(deg) | rotateY(deg) */
origin
rotate
origin
rotateX
origin
rotateY

Matrix

transform: matrix(
	   a,  b, /* scaleX and skewY */			| a  b  0 |
	   c,  d, /* skewY and scaleY */			| c  d  0 |
	   tx, ty /* translateX and translateY */		| tx ty 1 |
	   );

  /* only numerals, scale in ratio | skew in rads | translate in px */
origin
MATRIX

Transform fulcrum

transform-origin: x y z;
		/* x as [left | center | right]*/
		/* y as [top | center | bottom]*/
		/* z as [length]*/
origin
default
origin
left top

Transform in 3D

transform: translate3D | scale3D | rotate3D | matrix3D

Transform plane

transform-style: flat(default) | preserve-3d;
origin
flat
origin
preserve-3D

Perspective

perspective: [length];
/* or apply to transform like transform: perspective(100px) rotateX(10deg); */
1
6
4
3
5
2

Perspective origin

perspective-origin: x y;
1
6
4
3
5
2

Backface

backface-visibility: visible(default) | hidden;
visible
hidden

Translate3D

transform: translate3D(X,Y,Z); /* also can use only translateZ(Z) for Z-axis */
origin
translated

Scale3D

transform: scale3D(sX,sY,sZ); /* scaleZ(Z) */
origin
scaled

Rotate3D

transform: rotate3D(X,Y,Z,angle);
origin
rotate
origin
( 0, 0, -1, 180deg)
origin
( 0, 0, 1, 180deg)

Matrix3D

transform: matrix3D(
	a1, b1, c1, d1,					| sX 0  0  0 |
	a2, sy, c2, d2,					| 0  sY 0  0 |
	a3, b3, sz, d3,					| 0  0  sZ 0 |
	a4, b4, c4, d4					| tx ty tz 1 |
);
origin
MATRIX

TRANSITIONS

Transition rule

transition: transition-property /* all | single-css-animatable-property */
	    transition-duration /* s | ms */
	    transition-timing-function /* standart | steps | cubic-bezier */
	    transition-delay; /* s | ms */

Timing functions

transition-timing-function: linear /* cubic-bezier(0,0,1,1) */
			    ease /* cubic-bezier(0.25,0.1,0.25,1) */
			    ease-in /* cubic-bezier(0.42,0,1,1) */
			    ease-out /* cubic-bezier(0,0,0.58,1)) */
			    ease-in-out /* cubic-bezier(0.42,0,0.58,1)) */
			    steps(int, start|end)
			    step-start /* steps(1, start) */
			    step-end /* steps(1, end) */
			    cubic-bezier(x1,y1,x2,y2) /* X must be in [0, 1] */
linear
ease
ease-in
ease-out
ease-in-out
steps(4,start)
steps(4,end)
step-start
step-end
cubic-bezier(1,0,0,1)
cubic-bezier(1,-1,0.3,2)
only background
delay 2s
cubic-bezier(0.3, -5, 0.7, 5)
cubic-bezier(0, 1, 1, 0)
    default
  • link
  • button
  • button
  • button
  • button
    with transition
  • link
  • button
  • button
  • button
  • button

ANIMATIONS

Animation rule

animation: animation-name
	   animation-duration
	   animation-timing-function
	   animation-delay
	   animation-iteration-count /* number | infinite */
	   animation-direction
	   animation-fill-mode
	   animation-play-state; /* running | paused */

@keyframes rule

@keyframes animation-name {
	from(0%) {
		/* start rules here */
	}
	
	/* any keyframes here by % of animation flow */

	to(100%) {
		/* end rules here */	
	}
}

@keyframes rule

@keyframes colorChange {
	from { background: #a3c644; color: #464547; }
	33% { background: #7f993a; }
	66% { color: #7f993a; }
	to { background: #464547; color: #a3c644; }
}

div {
	animation: colorChange 4s linear infinite;
}
LOREM IPSUM
LOREM IPSUM

Animation direction

animation-direction: normal | reverse | alternate | alternate-reverse;
normal
reverse
alternate
alternate-reverse

Animation fill mode

animation-fill-mode: none | forwards | backwards | both;
none
forwards
backwards
both

Animation play state

animation-play-state: running | paused;

Irregular Shapes

Simple figures

.triangle-up {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 75px solid #D35D47;
}
.triangle-down {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-top: 75px solid #008ACE;
}
.trapezium {
border-bottom: 100px solid #39C2D7;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
height: 0;
width: 100px;
}
.oval {
height: 100px;
width: 170px;
background-color: #263852;
border-radius: 50%;
}
                        

Complex figures

practical use

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Officiis impedit libero esse odio excepturi fuga est ut itaque a quod suscipit, rerum asperiores. Consequuntur voluptates illo rerum recusandae pariatur asperiores, aspernatur, saepe ipsum error dolorem, quod inventore possimus modi deleniti tenetur et officiis. Nemo ab in totam ratione sequi error, ea dolorum repudiandae omnis, eaque facere impedit fugiat. Dolorum distinctio autem sequi enim quidem esse accusamus repudiandae voluptatum nobis, velit, molestiae. Eaque maiores, harum.

Support queries

Syntax

@supports (display: grid) {
    div {
        display: grid;
    }
}
@supports not (display: grid) {
  div {
    float: right;
  }
}

and/or operators

@supports (display: table-cell) and ((display: list-item) and (display:run-in))
@supports (transform-style: preserve) or (-moz-transform-style: preserve)

will-change property

Old school

.accelerate {
    transform: translate3d(0, 0, 0);
}

Example

.will-change:active {
    will-change: transform;
    transition: transform 0.3s;
    transform: scale(1.5);
}
.will-change {
    transition: transform 0.3s;
}
.will-change:hover {
    will-change: transform;
}
.will-change:active {
    transform: scale(1.5);
}

Will-change on 'hover' event

.will-change-parent:hover .will-change {
    will-change: transform;
}
.will-change {
    transition: transform 0.3s;
}
.will-change:hover {
    transform: scale(1.5);
}

Don't forget to remove property

var el = document.querySelector('.element');

el.addEventListener('mouseenter', hintBrowser);
el.addEventListener('animationEnd', removeHint);

function hintBrowser() {
    this.style.willChange = 'transform';
}

function removeHint() {
    this.style.willChange = 'auto';
}

THE END

thank you!

...and your questions start here