All language subtitles for 5. Creating Cool CSS Animations

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

1 1

It's now time for a very cool lecture 2

2

and one that I'm really excited about 3

3

because we're gonna talk about 4

4

how to implement CSS animations. 5

5

So, as I said, you're gonna learn in this lecture 6

6

how to create CSS animations using the keyframes at-rule 7

7

and the animation property. 8

8

So what we're trying to do 9

9

is this animation that we're gonna see here. 10

10

So watch carefully at these two parts here, 11

11

or H1 element. 12

12

So this one fades in, 13

13

and it also moves in from the right side here. 14

14

Right? 15

15

And this one moves in from the left side 16

16

and it also fades in. 17

17

So let's take a look again. 18

18

So, that's it. 19

19

This one fades from the left, 20

20

and this one fades in from the right. 21

21

So let's code these animations in CSS. 22

22

And it's really cool, believe me. 23

23

Now there are generally two types of animations in CSS. 24

24

The first one, which is also the easier one, 25

25

is to just use the transition property, 26

26

and then change the properties 27

27

that you want to animate on an event, 28

28

like when we hover the element. 29

29

And I'm sure that you're familiar with this method, 30

30

and if not, don't worry 31

31

because we're gonna use in the next lecture. 32

32

But what we're doing in this lecture is a bit different 33

33

and actually a bit more advanced and cooler. 34

34

Because these animations allow us to put more options 35

35

and so they are a bit more complex. 36

36

So let me show you how it works. 37

37

So we start by creating an animation 38

38

that looks exactly like we want it to be. 39

39

And in order to do that, we use the keyframes at-rule. 40

40

So we write @keyframes, 41

41

@keyframes, and then we give the animation a name. 42

42

And we're gonna start with the first part 43

43

so, the heading primary main. 44

44

Which remember, moves in from the left. 45

45

So I'm gonna call this animation moveInLeft. 46

46

Okay. 47

47

And now in here, I can specify what I want to happen 48

48

in each moment of time of the animation. 49

49

So we start with 0% which is before 50

50

the animation actually starts. 51

51

So this can also be called start. 52

52

Then the finish is at 100%, 53

53

which is when the animation finishes. 54

54

But we can put other steps here in the middle. 55

55

So for example, if we want something to happen 56

56

at the moment where the animation is at 80%, for example, 57

57

and we could do 50, we could do 36, 58

58

we could do any percentage here that we want. 59

59

But let's just start with 0%, so the initial state, 60

60

and then 100%, which is the final state. 61

61

So how do we want the element to look like 62

62

before it starts to move in? 63

63

First, we want it to be invisible. 64

64

Right? 65

65

So what we can do is opacity zero. 66

66

So at the beginning of our animation, 67

67

the opacity will be zero and at the end of course, 68

68

we want it to be back to one. 69

69

So opacity, one. 70

70

So that's one of the properties that we're animating here. 71

71

Now for the browser performance, 72

72

it's best to only ever animate two different properties. 73

73

One is opacity, which is the one that we're using here, 74

74

and the other one is the transform property. 75

75

That's what the browsers are optimized for, 76

76

for these two properties. 77

77

But with transform, we can do a whole lot. 78

78

And so it's everything that we need actually 79

79

to make all these cool animations that you can imagine. 80

80

So remember that the element comes in from the left. 81

81

So where do we want it to be in the beginning, 82

82

before the animation actually starts? 83

83

We want it to be more on the left than in the beginning. 84

84

Right? 85

85

And so we can use 86

86

transform 87

87

and then translateX. 88

88

And why x? 89

89

Well, because we want it to animate in the x-direction. 90

90

So remember, the x-direction is like this, 91

91

go from left to right, 92

92

and the y-direction, or axis, 93

93

goes from the top to the bottom. 94

94

So we want to translate it in the x-direction, 95

95

and we want it to be more on the left than in the beginning. 96

96

And so it's a negative value. 97

97

So let's say 100 pixels minus. 98

98

Okay? 99

99

Because, remember that the direction goes 100

100

from left to right. 101

101

So a positive value means it goes to the right, 102

102

and negative value means it goes to the left. 103

103

So just like in basic mathematics. 104

104

Alright, but moving back here. 105

105

How do we want it to be when we are at 100%? 106

106

We simply want it to look the way that it looks now, right? 107

107

And so we say translate 108

108

zero. 109

109

And why zero? 110

110

Well because, if we translate at zero, 111

111

then it's gonna look exactly the way 112

112

that it looks right now. 113

113

Make sense? 114

114

So this is the very basic animation just with the initial 115

115

and the final state. 116

116

So let's now actually apply this animation. 117

117

And so for an animation to work, 118

118

there are only two properties 119

119

that we have to really specify. 120

120

Which are called animation-name, 121

121

so animation-name which is the name 122

122

of the animation we just wrote. 123

123

And so it's moveInLeft 124

124

moveInLeft 125

125

and then the animation duration. 126

126

Okay, 127

127

animation-duration. 128

128

And so this the time that the animation should take. 129

129

Just to see it in the beginning, let's put three seconds 130

130

so it takes a longer time 131

131

so we can see if it's actually working. 132

132

We can actually put five seconds 133

133

and it takes even a bit longer. 134

134

So these are the only properties that are required 135

135

for an animation to work. 136

136

Now, there are a lot more properties, 137

137

but I'm gonna show them to you in a second. 138

138

Now let's just see if this actually works. 139

139

And so we need to reload our page now. 140

140

And yeah, you see? It's working. Great! 141

141

Amazing, right? 142

142

This is really fantastic, so let me play it again. 143

143

So it's happening really slow because we 144

144

specified it for five seconds but we can then change that. 145

145

Now let me play the original one again. 146

146

And you see that in this version it actually first goes 147

147

a bit in that direction and then it moves back, right? 148

148

Let me play it again. 149

149

So it's this, this small movement in this direction. 150

150

So how do you think we are going to do that? 151

151

What do you think? 152

152

And that's right, we're going to use this 80% here. 153

153

So what we can say here is that we want it to be 154

154

at this point, translateX to be 155

155

20 pixels, for example. 156

156

Because remember if we're specifying a positive value here 157

157

it will go to the right side. 158

158

So the final position is zero, 159

159

the initial position is a lot more to the left, 160

160

and then this intermediate position, let's say, 161

161

is a bit more to the right. 162

162

So let's check it out. 163

163

And yeah, nice! That's really nice. 164

164

Let's just put it a bit slower now that we have 165

165

this animation really going on already. 166

166

So just in one second. 167

167

Alright. Maybe that's a bit too much. 168

168

I will leave it just at ten pixels. 169

169

Great. So this already works. 170

170

Now I told you that there are other properties 171

171

for animations that we can also specify as. 172

172

So let's take a look at them. 173

173

The first one is that we can set a delay. 174

174

So animation-delay, let's say three seconds 175

175

and so now before this animation starts to play, 176

176

it will first wait three seconds. 177

177

So let's take a look. 178

178

Alright, so here we go. 179

179

So it waited three seconds. So one, two, three, 180

180

and there it goes. 181

181

Okay, so that's the animation delay. 182

182

Now we can also do something else, 183

183

I'm actually going to comment out 184

184

this part that we don't want, 185

185

because this is just to show you 186

186

but I will not delete it so that you can still see it 187

187

because it's properties can be very useful sometimes. 188

188

Okay. Another one that we can do is an animation count, 189

189

animation-iteration-count. 190

190

So if we set this to, let's say, three, 191

191

then the animation will simply happen three times. 192

192

So let's check that out as well. 193

193

So one, two, three. 194

194

Okay? 195

195

But we also don't want this one to happen 196

196

so I'm putting it here in the commented code. 197

197

And then one that is actually quite important 198

198

which is the animation-timing-function. 199

199

So this is a function which defines 200

200

how the animation will proceed. 201

201

So, how fast or how slow these parameters 202

202

that we specified here happen over time. 203

203

And there are different built-in animation functions 204

204

for example, ease-out, so ease-out for example. 205

205

Let's take a look at it now. 206

206

Probably gonna look a bit similar, 207

207

but they are different timing functions. 208

208

So I already have this one here open. 209

209

So we have ease, ease-in, ease-out, 210

210

and then all of these built-in animation timing functions. 211

211

So for example with the ease-in function, 212

212

the animation will start slower 213

213

and then accelerate over time. 214

214

And with ease-out, it's the opposite. 215

215

So it will start faster 216

216

and then it will become slower by the end. 217

217

So this might sound a bit complicated 218

218

but we're gonna look at animation timing functions 219

219

again in another lecture. 220

220

We're also going to look at this cubic-bezier function here 221

221

which basically allows us to write a custom function. 222

222

So a custom timing function different from 223

223

these predefined functions. 224

224

And this can actually make all the difference 225

225

in how the animation feels to the user. 226

226

So we used, remember, ease-out. 227

227

So let's play it again. 228

228

And this is how ease-in, for example, looks like. 229

229

So it was slower towards the beginning, 230

230

and then faster at the end. See? 231

231

So at the end it happened a lot faster. 232

232

So let's stick with ease-out like we had at the beginning. 233

233

Okay, and these are the most essential animation properties. 234

234

There are actually more, 235

235

but they are not really so important. 236

236

We can take a look at them if you're really interested. 237

237

So this is the moveInLeft animation 238

238

but we can now also build the moveInRight. 239

239

So let's just copy this part of the code 240

240

and call it moveInRight. 241

241

So, how do you think this one will look like? 242

242

So I want you to pause the video now 243

243

and try to change these values here 244

244

so that the animation instead of going in from the left, 245

245

it goes in from the right. 246

246

So take a second and try that out. 247

247

Alright, did you manage to do it? 248

248

Well let's see. 249

249

What do you think is the most logical way of doing it? 250

250

So all we have to do, is to actually remove this minus here, 251

251

and put it here. 252

252

And why is that? 253

253

Because as I mentioned, this 100 pixels here, 254

254

if it's a plus value, it moves the element to the right. 255

255

Because it goes forward in the x-direction. 256

256

And so it's the opposite as we had before, 257

257

and then the same thing, of course, here. 258

258

Because now we want it to move a little bit to the left, 259

259

before it goes back to it's original position at zero. 260

260

Right? 261

261

Okay. So now all we have to do is to add it here. 262

262

Now instead of specifying all of 263

263

these properties here one-by-one, 264

264

animation-name, duration, timing-function, delay, et cetera, 265

265

we can simply use the shortened property 266

266

and just say animation. 267

267

And so you can put all of these together, 268

268

and then CSS will figure out what means what. 269

269

So moveInRight over the duration of one second, 270

270

and with the ease-out function. 271

271

And that should work now. 272

272

So let's take a look. 273

273

And, yep! So it's exactly what we had before. 274

274

Great. 275

275

Now maybe you have already noticing that here in the end, 276

276

take a look here at this text, 277

277

right before the animation ends you will probably 278

278

see a little shake here, okay? 279

279

So let's take a close look at the end. 280

280

There it was. 281

281

So it moved a little bit like to the top. 282

282

Did you see it? 283

283

Let's take a look again. 284

284

So you saw it moving? So it moved a little bit, 285

285

so it's a bit shaky, 286

286

and sometimes this happens in animations. 287

287

And to be honest, 288

288

no one really knows actually why this happens, 289

289

but we, actually, we have a fix for this. 290

290

So there is something that we can do 291

291

and what we have to do in this case, 292

292

is to simply declare the backface-visibility property, 293

293

backface-visibility, and set it to hidden. 294

294

So the entire heading-primary element 295

295

because you see actually this entire element 296

296

is what moves on this animation. 297

297

So it's a bit shaky, 298

298

and its the entire heading-primary element. 299

299

This backface-visibility property determines 300

300

if the back part of the element when we transform it 301

301

is visible or hidden for the user. 302

302

So imagine we have an element and we rotate it 180 degrees. 303

303

So it would be logical that we then see the back part 304

304

of that element, right? 305

305

But if we use backface-visibility hidden, 306

306

then that back part, 307

307

so that part that's behind the element, let's say, 308

308

that part actually gets hidden. 309

309

Now in this case, 310

310

we're not rotating anything, 311

311

we're not doing anything like that 312

312

but we still use this fix here, 313

313

like this hack in order to fix this little shaking 314

314

that we see in the animation. 315

315

And again, 316

316

no one really seems to know why the shaking happens 317

317

and why this fixes it, but as long as it works, it's okay. 318

318

And so now, let's take a look at it again 319

319

and look really closely here, really close. 320

320

And it's gone, right? 321

321

It's no longer shaking 322

322

and everything appears to be working just fine. 323

323

And so yeah, that's how animations work. 324

324

So this is a really powerful thing. 325

325

And we could actually go really crazy with this. 326

326

Imagine we would do something like put a 60% here 327

327

and say transform at this point should be rotate, 328

328

let's say, just something like 120 degrees. 329

329

So degree is a unit we use for rotations. 330

330

And we can also say that in the beginning it should be 331

331

rotate 332

332

minus 50 degrees. 333

333

So just some random numbers here to try out 334

334

to see what happens. Okay? 335

335

Not really sure how it's gonna look like. 336

336

So it's this crazy thing, you see? 337

337

Let me play it again to you. 338

338

So this is just something crazy just to show you 339

339

the power that we have here with these animations. 340

340

To make it a bit more realistic, 341

341

let's say we rotate it 360 degrees. 342

342

Well that didn't really work, but let's try 180 degrees. 343

343

Now it should do something. 344

344

Yeah. 345

345

So it starts just right, and it rotates until 70% I believe, 346

346

or was it, oh yeah 60% 347

347

and then it went back to the initial position 348

348

because we didn't specify anything here. 349

349

We could actually put this one here as well. 350

350

So we're just playing around here with the numbers, 351

351

you don't really have to follow this. 352

352

I just wanted to show you that there's not really a limit. 353

353

So this actually looks the same, 354

354

let me try it again. 355

355

180 this time, 356

356

and, 357

357

well I guess it didn't work exactly 358

358

because we have to put it here in this step as well. 359

359

So let's put here as well just to play around a little bit. 360

360

And here we go. Let me close this one, 361

361

and of course, delete this little thing that we did here. 362

362

Because this is just like we want it to be. 363

363

You saw I selected two things at the same time. 364

364

You saw that? 365

365

I'm sure you can do that as well in your code editor 366

366

and sometimes it's really handy. 367

367

So for example, I have this transform. 368

368

If I wanna select the next one, I just have to hit Command+D 369

369

here in this Visual Studio code 370

370

but the same thing works in Atom or Brackets, or whatever. 371

371

So if I just hit Command+D, and D, and D, 372

372

then I have all of these transforms selected 373

373

and you see that the cursor is blinking here 374

374

and so then I can delete, 375

375

and I can write something else if I want, 376

376

and it's happening in all these places at the same time. 377

377

Alright? 378

378

So this is not really CSS, 379

379

but it's a nice trick that you can use in your text editor. 380

380

Also, I can put multiple cursors just by clicking it 381

381

then hitting Command, or Control maybe on Windows 382

382

and then just keep clicking and it will add multiple cursors 383

383

all over the place. 384

384

You see, like this. 385

385

And now if I start writing A-B-C, 386

386

it's writing A-B-C everywhere. 387

387

So in this case, not really useful but you can do it. 388

388

And sometimes it's pretty useful 389

389

like to change multiple things at the same time, okay? 390

390

And since we're talking about text editors, 391

391

I'm sure that yours also has this functionality here 392

392

which is called Minimap, 393

393

which shows us a very small version of our code. 394

394

So we can use this to drive around in our code, 395

395

or to move around. 396

396

So imagine we wanna go to the beginning 397

397

and then we just need to click here, 398

398

and then it goes exactly wherever you want to. 399

399

Or, imagine that I'm working here 400

400

and I want to go to the animations 401

401

and here I can see that these here look like the animations 402

402

and I just have to click here 403

403

and then I'm here at the place where I wrote the animations. 404

404

So instead of scrolling all the way up and down, 405

405

imagine we have 500 lines of CSS 406

406

then this becomes really useful. 407

407

And so once again, this is also in your code editor, 408

408

you just have to turn it on 409

409

if it's not turned on by default. 410

410

Only if you like it, of course. 411

411

But I think it's pretty helpful sometimes. 412

412

Okay, and that was the goal that I had for this lecture. 413

413

So I showed you how animations work 414

414

with the keyframes at-rule, 415

415

and I showed you that we can build 416

416

really complex animations with this. 417

417

Oh, but before we stop, 418

418

I can actually show you something else 419

419

which is, imagine that we don't just want this to happen 420

420

when we reload the page. 421

421

So imagine we want this to happen, this animation to happen 422

422

when we hover, for example, something. 423

423

So let's say we want this animation to happen to the logo 424

424

when I hover it. 425

425

So let's say, logo 426

426

hover, 427

427

and so you see I can actually use this animation here 428

428

in multiple places. 429

429

So I can just define moveInRight in one place 430

430

and then use it all over the place. Okay? 431

431

And so now this will only happen as soon as they hover. 432

432

So let's reload this 433

433

and here we go. 434

434

You see? There it is again. 435

435

So, this animation does not only have to happen 436

436

with the page load like we have these ones here, 437

437

see these ones are on page load, but this one is on hover. 438

438

Okay well this was just to show you, 439

439

we don't really want it to be here, right? 440

440

Okay, but this was now really all I had 441

441

to show you for this lecture. 442

442

So let's now move on to the next video. See you there.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.