We can now transition to and from display: none

Kevin Powell
6 Jun 202421:19

Summary

TLDRThis script explores the intricacies of animating elements like modals and popovers in CSS, traditionally a complex task. The speaker demonstrates how recent advancements in CSS, such as keyframes and transitions, simplify this process. They address the challenges of animating properties like 'display' and introduce new CSS properties like 'transition-behavior' and 'start-style' to create smoother and more controlled animations. The script also highlights the importance of understanding CSS fundamentals for effective web development and touches on browser support for these features.

Takeaways

  • πŸ˜€ CSS animations and transitions can now handle complex behaviors like fading in and out, which were previously challenging without JavaScript.
  • 🎨 The speaker demonstrates animating a modal dialog box, showcasing how to use CSS for both opening and closing animations without relying on JavaScript.
  • πŸ”‘ The CSS `animation` property is used for creating keyframes, allowing for smooth transitions between states, like opacity changes.
  • 🚫 A common issue arises when trying to animate properties considered 'discrete', such as `display: none`, which do not transition smoothly.
  • πŸ†• CSS properties like `transition-property`, `transition-duration`, and `transition-behavior` are introduced to handle smooth transitions, including those with discrete animations.
  • πŸ›  The `@apply` rule in CSS, which allows for nesting styles, simplifies the process of applying multiple styles to an element, but cannot be used within pseudo-elements.
  • 🌐 Browser support for these new CSS features is improving but is not yet universal, with Chrome currently leading in support for these advanced features.
  • 🌈 The script covers how to create visually appealing effects, such as gradients for backdrops, to enhance the user experience with modal dialogs.
  • πŸ“š The importance of understanding CSS fundamentals is highlighted to overcome frustrations and write more confident code, with a mention of a course that could provide deeper insights.
  • πŸ”„ The concept of 'starting style' is introduced to define the initial state of an animation, which is crucial for creating bidirectional transitions.
  • πŸ’» The speaker emphasizes the use of progressive enhancement in web development, ensuring that all users have a functional experience while those with newer browsers get an enhanced visual experience.

Q & A

  • What challenges did the speaker originally face when trying to animate elements like modals or popovers?

    -The speaker originally faced difficulties with animating elements such as modals or popovers, finding it harder than expected. They had to resort to using JavaScript, event listeners, or set timeouts to manage the transitions from 'display none' to 'display block', 'grid', 'flex', etc.

  • How has the introduction of CSS animations and transitions by the CSS working group simplified the process of animating elements?

    -The introduction of CSS animations and transitions has made it much simpler to animate elements. It allows for effects like fading in and out and sliding without the need for JavaScript, by just using CSS properties and keyframes or transitions.

  • What is the default state of the 'dialog' element when it is not being displayed?

    -The default state of the 'dialog' element when it is not being displayed is 'display: none', which is a user agent style that gets toggled to 'display block' when the element is clicked on.

  • Why is it recommended to declare 'display: none' and 'display: block' even if they come from the user agent styles?

    -It is recommended to declare 'display: none' and 'display: block' explicitly because it makes it clear what the closed and open states of the element are. This can be particularly important when animating or working with these properties where explicit declarations may be necessary.

  • What is a 'backdrop' in the context of modals and how is it typically handled in terms of display?

    -A 'backdrop' in the context of modals is an element that appears behind the modal to provide a visual separation from the rest of the page content. For simplicity, the speaker initially sets the backdrop to 'display: none', but it is planned to animate it later in the presentation.

  • What is the purpose of keyframes in CSS animations?

    -Keyframes in CSS animations define the different stages of an animation. They allow developers to specify the starting and ending points (as well as any intermediate steps) of an animation, making it easier to create complex animations with CSS alone.

  • Why is using 'animation' considered overkill for a simple fade transform?

    -Using 'animation' is considered overkill for a simple fade transform because it involves setting up keyframes for both the 'appear' and 'vanish' states, which is more work than necessary for a simple effect that could be achieved with transitions.

  • What is a 'discrete animation' and how does it differ from a 'transition'?

    -A 'discrete animation' is an animation where the property changes instantly at the midpoint of the animation duration. It differs from a 'transition', which smoothly changes a property over a specified duration without instant changes in between keyframes.

  • Why might transitions not work when changing from 'display: none' to 'display: block'?

    -Transitions might not work when changing from 'display: none' to 'display: block' because 'display' is considered a discrete animation property. This means it changes instantly at the 50% mark of the animation, preventing a smooth transition from occurring.

  • What is the 'transition-behavior' property in CSS and how does it help with animating discrete properties?

    -The 'transition-behavior' property in CSS is used to specify whether a transition should be allowed even when it involves discrete animations. By setting it to 'allow-discrete', it enables transitions to occur on properties that would normally prevent transitions from happening, such as 'display'.

  • What is the 'starting-style' property in CSS and how does it help with controlling the start of a transition?

    -The 'starting-style' property in CSS is used to define the initial state of an element before a transition or animation takes place. It helps control the start of a transition by setting specific styles that the element should have at the beginning of the animation, allowing for more granular control over the animation process.

  • Why is browser support for certain CSS properties important and how can it affect the user experience?

    -Browser support for certain CSS properties is important because it determines the consistency and availability of features across different platforms. Lack of support in some browsers can lead to a degraded user experience, where certain animations or styles may not work, or where the website may not function as intended.

  • What is the concept of 'progressive enhancement' in web development?

    -Progressive enhancement is a web development approach that focuses on providing a basic level of user experience to all browsers, while enhancing the experience for those browsers that support more advanced features. This ensures that the website is functional and accessible, even if some users are on older or less capable browsers.

  • Why is it recommended to use 'overlay' or 'top layer' in modals and popovers?

    -Using 'overlay' or 'top layer' in modals and popovers is recommended to ensure that the modal or popover appears above all other page elements, preventing any z-index issues. It helps manage layer stacking and makes sure that interactive elements within the modal are on top of other page content.

  • What limitation does CSS nesting have with pseudo-elements, and how can it be worked around?

    -CSS nesting has a limitation where it cannot be used inside pseudo-elements. This can be worked around by using the 'starting style' at-rules outside of the pseudo-element and then nesting the pseudo-element rules inside the 'starting style', rather than the other way around.

Outlines

00:00

πŸ˜€ Animating UI Elements with CSS Transitions and Keyframes

The speaker introduces the topic of animating user interface elements, such as modals, without the need for JavaScript by utilizing CSS animations and transitions. They discuss the challenges faced when toggling between 'display none' and other display properties and how CSS has evolved to simplify these processes. The speaker demonstrates using keyframes for a fade effect and explains that while animations are an option, transitions can be more efficient for simple fade-ins and fade-outs. They also mention the importance of understanding the 'display' property and its behavior during animations.

05:01

πŸ˜• Overcoming CSS Discrete Animation Challenges

The speaker delves into the difficulties of using CSS transitions with discrete animations, such as changing the 'display' property, which traditionally does not allow for smooth transitions. They explain the concept of discrete animations and how they work differently from continuous animations, causing transitions to fail. The speaker provides a workaround by using the 'transition-behavior' property with 'allow-discrete' value to enable transitions despite the discrete nature of the animation. They also highlight the importance of understanding CSS fundamentals to overcome such challenges.

10:03

πŸ”„ Advanced CSS Transition Techniques and Browser Support

The speaker explores advanced techniques for creating smooth transitions, including the use of the 'starting-style' property to define the initial state of an element before the transition. They discuss the potential frustrations of working with CSS and how understanding its underlying principles can alleviate these issues. The speaker also touches on browser support for these CSS features, noting that while support is improving, it is not yet universal, and emphasizes the importance of progressive enhancement to ensure a consistent user experience across different browsers.

15:05

🎨 Enhancing Modal and Backdrop Animations with CSS

The speaker focuses on animating modal dialogs and their backdrops, adding visual effects like gradients to enhance user experience. They explain how to use CSS to transition the backdrop's opacity and overlay properties to ensure a smooth entrance and exit of modal dialogs. The speaker also addresses the limitations of using nesting within pseudo-elements and the necessity of using 'at starting-style' for animations involving pseudo-elements. They demonstrate how to apply these techniques to create a seamless and visually appealing modal interaction.

20:07

🌐 Final Thoughts on CSS Animations and Browser Compatibility

In the concluding segment, the speaker wraps up the discussion on CSS animations, emphasizing the importance of understanding CSS properties and their interactions. They provide a brief overview of the 'transition-behavior' property and its role in allowing discrete animations to transition smoothly. The speaker also mentions the need to consider browser compatibility and the use of progressive enhancement to provide a baseline experience for all users while enhancing the experience for those on supported browsers. They encourage viewers to explore further resources for a deeper understanding of CSS.

Mindmap

Keywords

πŸ’‘CSS Animations

CSS animations are a way to apply dynamic styles to web elements over time, creating visual effects such as fades and slides. In the video, animations are used to demonstrate how to transition elements from 'display: none' to 'display: block' with a fade-in effect, showcasing the ease of creating such effects using CSS rather than JavaScript.

πŸ’‘CSS Transitions

CSS transitions allow properties of an element to change smoothly from one value to another over a given duration, enabling effects like fading in and out. The video script discusses the use of transitions to animate the opacity and display properties of a modal dialog, highlighting a more straightforward approach compared to using animations.

πŸ’‘Keyframes

Keyframes in CSS are used to define the stages of an animation sequence. The script describes creating keyframes for 'appear' and 'vanish' animations to control the opacity changes of a modal. However, it points out the limitations when using keyframes for properties like 'display' that involve discrete animations.

πŸ’‘Display Property

The 'display' property in CSS determines how an element is rendered in the document, such as 'none', 'block', 'flex', etc. The video explains the challenges of animating this property when toggling between 'display: none' and 'display: block' and how recent CSS features can help overcome these challenges.

πŸ’‘Opacity

Opacity is a CSS property that specifies the transparency of an element, with values ranging from 0 (fully transparent) to 1 (fully opaque). The script uses opacity changes as an example of animating a modal's appearance and disappearance, demonstrating how to smoothly fade elements in and out.

πŸ’‘Backdrop

A backdrop in the context of modal dialogs refers to a layer that covers the background when the modal is open, typically dimmed to focus attention on the modal content. The video discusses animating the backdrop's opacity and the complexities involved in ensuring it transitions smoothly alongside the modal.

πŸ’‘Discrete Animation

Discrete animations in CSS are animations where the property changes instantly at the midpoint of the animation duration. The script explains how discrete animations, such as changes to the 'display' property, can interfere with smooth transitions, necessitating workarounds like the 'transition-behavior' property.

πŸ’‘Transition Behavior

The 'transition-behavior' property in CSS is used to specify how properties that are involved in discrete animations should be treated during transitions. The video script illustrates how setting 'transition-behavior' to 'allowed-discrete' can help enable smooth transitions for properties like 'display' that would otherwise animate discretely.

πŸ’‘Starting Style

The 'starting style' in CSS is a way to define the initial state of an animation or transition before it begins. The script discusses the use of 'starting style' to set the initial opacity and position of a modal, ensuring that the transition from 'display: none' to 'display: block' is smooth and controlled.

πŸ’‘Nesting in CSS

Nesting in CSS allows styles to be written in a hierarchical structure, making the code more organized and readable. The video script mentions the benefits of nesting for organizing styles related to modal animations but also points out that it cannot be used within pseudo-elements like ':before' or ':after'.

Highlights

CSS now supports animations and transitions for properties like display, making it easier to create effects without JavaScript.

The CSS working group has introduced new methods to handle animations and transitions for display properties.

Creating keyframes for opacity transitions can be used to animate elements from display none to display block.

Animations with keyframes can be overkill for simple fade effects and transitions might be more suitable.

Transitions can be tricky with properties like display that are considered discrete animations.

The 'transition-behavior' property with 'allowed-discrete' value can enable transitions for discrete animations.

The '@start' style rule in CSS allows defining the starting point of a transition.

Nesting CSS rules can simplify the syntax but cannot be used inside pseudo-elements.

Browser support for CSS transitions and animations on display properties is improving but still not universal.

The 'overlay' property is important for modal dialogs to ensure they appear on top of other elements.

Animating backdrops in modal dialogs can enhance user experience but requires careful handling of transitions.

CSS transitions and animations provide a progressive enhancement, offering better experiences on supported browsers without breaking functionality on older ones.

MDN Web Docs is now a collaborative effort supported by donations, important for developers relying on it for CSS and web standards.

Understanding CSS fundamentals like discrete animations and formatting contexts can alleviate common frustrations with CSS.

CSS properties like 'display' can behave unexpectedly during transitions, requiring specific workarounds.

The video provides a comprehensive guide on animating modal dialogs and backdrops using CSS transitions and animations.

Using 'caniuse.com' to check browser support for specific CSS features is essential for ensuring cross-browser compatibility.

Transcripts

play00:00

hi there my friend and friends if you've

play00:01

ever tried to create a model that would

play00:02

slide in or animate or Fade Out and fade

play00:04

in and all of that like this or you've

play00:06

done a popover you've done anything that

play00:08

involved going from a display none to a

play00:10

display of block or Grid or Flex or

play00:12

whatever it is uh you probably found out

play00:14

it was a lot harder than you thought it

play00:16

would be to do something like this and

play00:18

you ended up using some JavaScript to do

play00:20

different things and maybe using event

play00:21

listeners or whatever it is or just

play00:23

toggling based on set timeouts there's a

play00:26

few different methods you can use but

play00:27

those are all things of the past now

play00:28

because the CSS working group hurt us

play00:30

and we can do things just like this with

play00:32

animations and transitions now and if

play00:35

we're just doing it like I have here

play00:37

where it's just a transform with a

play00:39

opacity fade and stuff like that an

play00:41

animation is definitely Overkill but

play00:43

it's actually a little bit easier to do

play00:45

it with an animations we're going to

play00:46

look at that first and then we're going

play00:48

to look at how we can do it with a

play00:49

transition instead just cuz there's a

play00:50

few extra new things along the way to

play00:52

get it working there but it's super

play00:53

awesome and super easy to do all right

play00:55

so right now I'm using a dialogue right

play00:57

here so you can see that with this

play00:58

button here and I'm just using the show

play01:01

model and close methods right now so it

play01:03

opens and closes the model and the only

play01:06

really important thing with this model

play01:08

is that the dialogue element when we're

play01:10

doing it it will have a display of none

play01:12

is a user agent style that gets toggled

play01:14

over to a display block when we click on

play01:16

it so I don't actually have to declare

play01:19

these because they're coming from the

play01:20

user agent Styles but I'm going to leave

play01:21

them here because it is quite possible

play01:23

that you're animating or doing something

play01:25

with these where you actually have to

play01:26

declare these properties and so I just

play01:28

want to make it really explicit that

play01:30

this is on my closed State and this is

play01:31

on my open state that we have those

play01:33

right there now it's a little bit hard

play01:34

to see in the demo I have set up right

play01:36

now but there is a backdrop that comes

play01:38

through here and for Simplicity I'm

play01:40

going to just put that as a display none

play01:42

as well for now but we're going to come

play01:43

back and look at how we can animate the

play01:45

backdrop as well just cuz there is

play01:46

actually a little bit of a gotta with

play01:47

that and we'll talk more about what

play01:49

backdrop even is if you're not familiar

play01:50

with it so as I said we're going to

play01:52

start with the animation first uh and

play01:54

with key frames and then we'll look at

play01:55

how to do it with transitions cuz doing

play01:57

it with uh key frames is actually

play01:59

incredibly easy now we used to not be

play02:01

able to do this but what we can do is we

play02:03

can create some key frames like this for

play02:06

I'm just going to call it a where we

play02:07

want to go from an opacity of zero to an

play02:09

opacity of one and this actually I think

play02:11

always worked uh no matter what so it

play02:13

was when we go to the open State we

play02:14

could come and we could do an animation

play02:16

then of our appear and let's just say

play02:19

it's 1 second long so we can see it's

play02:20

working right there that we're coming in

play02:22

and that's perfect where things break is

play02:24

if we wanted it to go away and one thing

play02:27

with uh key frames if ever using them if

play02:29

you want like an animation to run one

play02:31

way and then another you actually have

play02:33

to change the animation name uh where it

play02:35

won't work so I'm going to do a vanish

play02:37

here and then it's just going to be two

play02:39

and then this will be our from and

play02:41

that's kind of awkward so we can switch

play02:43

the order though the order doesn't

play02:44

matter it's the the key frames that

play02:46

we're talking about that are the

play02:47

important part but the vanish will go

play02:49

from the opacity of one to an opacity of

play02:51

zero and you would think all we' need to

play02:53

do here is we could just copy this line

play02:56

switch it over to a vanish uh and you

play02:59

would think that would work because then

play03:00

when we click on it we're adding the

play03:01

animation it appears and then we'd click

play03:03

this and it would vanish but it doesn't

play03:06

and that's because display none is

play03:07

instantly toggling to a display of none

play03:09

it just happens it goes to display none

play03:11

and there's no animation to run anymore

play03:13

because the element is display nun and

play03:15

it's gone and this is always frustrating

play03:17

and there's JavaScript solutions to this

play03:19

and stuff but we don't need to do that

play03:20

anymore uh luckily so now what we can do

play03:23

is all this is the the simplest thing in

play03:25

the world we just have to come here and

play03:28

say that this is a display block or

play03:30

whatever you know if you're using grid

play03:31

or something else you'd put whatever

play03:33

your display value is here and then here

play03:35

you just do a display of none and

play03:38

normally if you look at the spec display

play03:40

shouldn't be able to animate like this

play03:42

and it's actually a discrete animation

play03:43

meaning uh normally discrete animations

play03:46

flip at the 50% Mark so it would be

play03:49

halfway through the animation it would

play03:50

do it but display never work that way

play03:52

display is always and it's in the spe

play03:54

that it would do it instantly at the

play03:55

beginning of the animation but if we do

play03:57

this now it knows to wait until the end

play04:00

so if I click on it it fades in and now

play04:02

if I click on it again it fades out and

play04:04

it works and it's

play04:05

fabulous it's so nice that this is easy

play04:08

to do now it's it's wonderful to use but

play04:11

this is a lot of work for something that

play04:12

would just should work with transitions

play04:15

right I have to create an appear and a

play04:17

vanish key frames and set them all up

play04:20

and then I need the animation here and a

play04:22

different animation here it's not that

play04:23

bad honestly but it's more work than

play04:25

what we actually need to do with the

play04:28

caveat that if you have more complex

play04:30

things going on where you actually need

play04:31

multiple key frames then this is what

play04:33

you'd actually want to be doing right

play04:35

you know if you have like spinning and

play04:36

moving and you have more than a from and

play04:39

a two so it's not just the 0% and the

play04:41

100% there's other steps involved well

play04:43

then this is the direction you want to

play04:44

go in if all you need to do is make

play04:47

something fade in or out or we're going

play04:48

to see how we can move it up and down

play04:49

and all of that we can now do that with

play04:51

transitions as well but as I said it's a

play04:53

little bit trickier so if you do want to

play04:54

access this code exactly as it is here

play04:57

with the key frames it will be linked in

play04:58

the description um but we're also going

play05:00

to now take this and we're going to make

play05:02

this work with transitions instead so

play05:04

I'm going to delete all of that and now

play05:05

we're back to what we were before and

play05:07

we'll see how we can do it with a

play05:09

transition so let's also drop this off

play05:11

from here and here and again the the one

play05:13

frustrating thing for me on this whole

play05:15

thing is there's a little caveat with

play05:16

this backdrop but um it's not a big deal

play05:19

but it's it's a little bit of an

play05:20

annoying thing that we'll see uh but

play05:23

what you'd want what you think you could

play05:24

do here is let's just say that we want

play05:26

to go from an opacity of zero and then

play05:29

here we want to have our opacity of one

play05:31

right opacity of one and we just then

play05:35

ideally would just do a transition of

play05:37

our opacity and once again we'll do 1

play05:40

second and we would hope that when we

play05:42

click that it would work but you can see

play05:43

it doesn't and this is a thing with

play05:46

transitions is if there's a property

play05:49

that's changing that is considered a

play05:51

discrete animation and a display block

play05:53

is one of those it will prevent any

play05:54

transitions from working and just if you

play05:56

don't really know what a discreet

play05:57

animation is just really really quickly

play05:59

here here I'm going to do a little

play06:00

Interruption cuz we're going to look at

play06:01

this with background images and

play06:02

background images are considered a

play06:04

discrete animation so here I just have

play06:07

this animation that's happening where it

play06:09

should take 10 seconds to happen when I

play06:11

hover but you'll see it just instantly

play06:12

changes and it's actually instantly

play06:14

changing after 5 seconds because it's

play06:16

getting to the 50% Mark of the animation

play06:18

and then it just instantly switches

play06:20

because we can't do a background image

play06:23

and I only have it on Hover right now so

play06:24

as soon as I leave it goes back to its

play06:26

starting state so the animation's only

play06:28

running here now this is different from

play06:30

display because display property we're

play06:33

here I'm going to throw these spans in

play06:35

here and if we go and take a look um I'm

play06:38

also doing body hover and then my div so

play06:40

I want just this div with all my hands I

play06:43

want it to go from a display none to

play06:45

having a display of block and it should

play06:47

take 10 seconds for this to happen so

play06:49

you'd expect the hands to actually show

play06:50

up after 5 seconds because it's a

play06:52

something it's a discret animation but

play06:54

when I go on you can see they actually

play06:55

instantly appear uh and then the rest

play06:58

happens and the opacity is not working

play07:00

nothing else is working and that's

play07:02

because if something has a discret

play07:03

animation the transitions will not work

play07:06

at all uh is the important thing and so

play07:10

it just it's instantly going from here

play07:11

to here and it's not transitioning at

play07:13

all it just the hands come on and off

play07:15

and there's no delay or anything and

play07:16

this might have you wondering how do you

play07:18

know if something is a discrete

play07:19

animation so we're going to jump on over

play07:21

to MDM now where we have the background

play07:23

image which I just mentioned was and

play07:25

here if you go on over to the in this

play07:27

article section there's always going to

play07:28

be a formal definition and if you go to

play07:30

the formal definition you'll get this

play07:32

table like this and there's always an

play07:34

animation type and you can see it says

play07:35

discreet if we go to background position

play07:38

and we go to the formal definition we're

play07:39

going to see that in this case the

play07:41

animation type is a repeatable list and

play07:44

let's just go over to let's say clip

play07:46

path um as another one we're going to

play07:47

click on formal definition and in this

play07:49

case animation type yes as specified for

play07:51

basic shape otherwise no so you'll get

play07:54

more information on it the one that

play07:55

you're looking for obviously you know

play07:57

bottom we can go check that one too

play07:58

really fast a length of percentage or a

play08:01

cal so there's different limitations

play08:02

sometimes on them but if it says discret

play08:04

it's one of those ones where it's going

play08:06

to happen in the middle but we're

play08:07

getting around that now we're going to

play08:08

see what that is so sorry for the little

play08:10

Sidetrack here and I'm going to stay on

play08:11

the Sidetrack for just one more second

play08:13

because if you use MDM I just want to

play08:15

give a little PSA here public service

play08:17

announcement that uh MDM is not actually

play08:19

completely run by mazilla anymore it's

play08:21

now more of a collaborative effort with

play08:23

most of the work both to maintain and

play08:24

update everything that's going on here

play08:26

being done by the open web docs which is

play08:28

actually a nonprofit that relies on

play08:30

individual and corporate donations to

play08:31

fund their operation I'm going to put a

play08:33

link to them in the description so if

play08:34

you'd like to learn more about it and if

play08:36

you use MDM which I'm pretty sure you do

play08:38

if you're watching this video you might

play08:39

want to mention it to your boss or

play08:40

something like that cuz you know say

play08:42

something like I wouldn't be able to do

play08:43

my job if it wasn't for these guys and

play08:45

see if they can't throw a little bit of

play08:46

money their way to help fund everything

play08:48

they're doing so we can continue to rely

play08:50

on MDM and all the stuff here to be able

play08:53

to do our jobs properly but anyway with

play08:55

that out of the way let's jump back on

play08:57

over to animating from display none

play08:59

right here and so we want to do it in

play09:01

this case it's transitioning I guess uh

play09:04

and so this obviously it's not working

play09:07

right now so what can we do to actually

play09:10

make this work and there's a couple of

play09:11

things the first thing is we actually

play09:12

want to transition our display property

play09:15

as well so just to make life a little

play09:17

bit easier here I'm actually going to do

play09:18

a transition uh property and I'm going

play09:21

to do display and opacity just so I can

play09:24

break it off onto more lines and then

play09:26

this one's going to be my duration which

play09:28

would be 1 second and so that just means

play09:30

the display and the opacity are going to

play09:32

transition over a duration of 1 second

play09:35

and if I do this it's still not going to

play09:37

work that that'd be nice if that was the

play09:39

trick CU that was the trick when we did

play09:40

it with the key frames before right uh

play09:43

but it's not actually far off there's

play09:45

just one more line that we need which is

play09:47

our transition behavior and this is the

play09:50

new thing that sort of unlocks

play09:52

everything which is allow discreet or

play09:53

one of two new things there's a second

play09:55

one that we're going to need in a minute

play09:56

what this allow discreet means is

play09:59

because because as I said before if

play10:00

something involves a discret animation

play10:03

going on transitions will not work it

play10:06

just goes nope there's a discret

play10:07

animation here we're we're ignoring the

play10:10

transition now what we're saying is whoa

play10:12

wait a second here we're going to allow

play10:13

it even though we have those discret

play10:15

animations on there and it works sort of

play10:19

it's working in one direction we're

play10:20

getting there so now we're actually this

play10:22

way is not working but going the other

play10:24

way is and this is where there's that

play10:27

second new thing that I mentioned which

play10:29

is that we want to add in one more thing

play10:31

to actually tell it where it's coming

play10:33

from and that's because it's going from

play10:34

a display none to what we have here and

play10:38

it gets a little bit lost along the way

play10:40

and this is one of those frustrating

play10:41

things where it feels like oh I just

play10:43

figured it out it's why why is it only

play10:45

working in One Direction and I

play10:48

understand that that's why MDM is great

play10:50

um for things like this and sometimes

play10:52

CSS can just feel really frustrating

play10:54

though and you feel like you're getting

play10:55

there and then you hit another roadblock

play10:56

and you're getting there and you hit

play10:57

another roadblock and from my experience

play11:00

most of those frustrations start to go

play11:02

away when you understand a lot of the

play11:03

fundamentals of how the language

play11:05

actually works including things like are

play11:07

discrete animations as well as things

play11:09

like formatting context and containing

play11:10

blocks and more things that just don't

play11:12

get talked about enough and if you'd

play11:14

like to get a really solid understanding

play11:16

of why CSS works the way it does and so

play11:18

that way you're less frustrated with it

play11:20

and you're writing it with a lot more

play11:21

confidence I'd suggest checking out my

play11:22

course CSS demystified which are created

play11:24

for that exact purpose there is a link

play11:26

to it in the description but if you just

play11:28

want to worry about this transition

play11:29

that's fine too we're going to look at

play11:30

exactly that right now which I'm going

play11:33

to go on the open here and we're going

play11:34

to use a bit of nesting um we'll look at

play11:36

the non- nested version of this as well

play11:38

but I'm going to do a starting style

play11:39

here and this is as I said I hinted at

play11:41

there's I didn't hint at I said there is

play11:44

a new thing which is another new thing

play11:46

like our allow discreet which is our

play11:48

start style and it's not start style

play11:50

actually it's I said it out loud and

play11:52

like start style doesn't sound right

play11:54

starting style and the starting style is

play11:57

where does this thing start from so so I

play11:59

actually want to have my starting style

play12:01

of an opacity of zero that's going to go

play12:02

to here and now it fades in and it fades

play12:06

out so that looks a lot nicer right I'm

play12:08

happy with that uh the one thing at this

play12:11

point this might feel like oh well I'm

play12:13

going I have the zero here and the zero

play12:14

here that's kind of frustrating and

play12:16

annoying you're going to be very happy

play12:17

about this in a second though because

play12:19

right now this works and if this is all

play12:20

you need well I mean it works so that's

play12:22

great but by having this this and this

play12:26

as three separate things instead of just

play12:28

two it opens up a whole world of extra

play12:30

stuff that we can do so as an example of

play12:33

that maybe I come here and I do a

play12:34

translate of zero and -25 VH so if I

play12:39

just do that here and then here I come

play12:42

in with my 0 0 like that and for now

play12:47

let's actually have the starting style

play12:49

exactly the same so we're this and this

play12:51

are matching one another it's going to

play12:54

go in and then it's going to go back to

play12:55

where it was so it's the same thing in

play12:57

both directions but because we have

play13:00

these separated that means we can

play13:01

actually make these different so I can

play13:03

actually say that this one is a positive

play13:05

25 so the starting style is different

play13:08

from when we're going towards our close

play13:10

state so we really have to think of this

play13:12

is my starting style this is once it's

play13:14

opened and this is where it goes once

play13:16

it's closed which is kind of weird and

play13:18

different from how we might normally

play13:19

think about things just cuz the order of

play13:21

it you can see it slid in from the top

play13:23

because we're going from the - 25 and

play13:25

now it's going to go out the bottom so

play13:27

that's kind of cool CU we can have

play13:28

different different directions and

play13:30

different things going and it just opens

play13:31

up that extra world of control now I did

play13:33

mention that on here we're going in with

play13:35

the starting style uh is nested inside

play13:38

of here and I'm using nesting because I

play13:40

find this is just a lot easier to do it

play13:42

this way and that's just also because if

play13:44

this is supported by the browser then

play13:46

nesting is 100% supported so I'm not

play13:48

worried about the browser support of

play13:50

nesting because I'm using this and

play13:51

speaking of browser support this is one

play13:53

of those nice things where browser

play13:54

support is far from perfect right now

play13:56

everything I'm doing is only working in

play13:59

Chrome the animation ones I think is one

play14:00

version back and this starting style is

play14:02

only supported in the latest release and

play14:04

speaking of browser support if you go to

play14:05

can I use and actually we should do that

play14:07

together so I can show this to you and

play14:08

if we look up starting style first we'll

play14:11

notice that it's supported it's there

play14:13

it's in Safari and in our chromium

play14:15

browsers Firefox it's not quite there

play14:17

yet um but at least it's in our two

play14:19

browsers so that is awesome the one

play14:21

that's a little bit annoying is if we

play14:23

look at the transition Behavior which is

play14:26

that other one right uh behav you got to

play14:28

spell things properly for these to work

play14:31

uh where it looks like it has equal

play14:33

support and it's actually coming in

play14:34

Firefox so that's great we're getting

play14:36

much better browser support for it it's

play14:37

almost at you know 78% it's fantastic

play14:40

the thing is if we scroll down a little

play14:41

bit here uh we get to this one which is

play14:43

transition Behavior allowed discreet uh

play14:46

which all of a sudden it's not working

play14:47

in Safari or Firefox and it's

play14:49

specifically talking about our display

play14:51

property there and that's because as I

play14:53

said we have other discrete animations

play14:54

we have our background images and

play14:56

there's a whole bunch of other stuff

play14:57

where Safari is supporting those Safari

play15:00

and Firefox is on the way to supporting

play15:02

them but it's only chromium browsers

play15:05

right now that are supporting when

play15:07

you're using it specifically with

play15:08

display which is a little bit kind of

play15:11

annoying um but I mean support's still

play15:13

at over 70% and if this doesn't work it

play15:16

just means the animation won't be there

play15:18

everything else is still perfectly fine

play15:20

but the thing is even if you're on one

play15:21

of these browsers where it's not working

play15:23

it's a progressive enhancement where

play15:25

we're not going to break anything

play15:26

because none nothing I've done like the

play15:28

starting style if there's no animation

play15:30

it doesn't really matter so this not

play15:32

being understood by the browser if it's

play15:34

older doesn't matter and if the gets to

play15:37

these and it goes well I'm just not

play15:38

doing this I don't know what this

play15:40

transition behavior is and I can't do a

play15:42

transition on display just means it's

play15:44

turning it on and off so we just go back

play15:46

to square one where we don't have that

play15:48

animation in older browsers newer ones

play15:50

get the nicer experience the best way to

play15:52

do web development is to do it with

play15:54

Progressive enhancements where things

play15:55

look better on browsers that support it

play15:57

but nothing is broken on older browsers

play16:00

they still get all of the stuff they

play16:01

need just it doesn't look quite as good

play16:03

but yeah we have a couple of other

play16:04

things that I want to talk about in the

play16:05

first one or it's mostly about the the

play16:08

nesting here CU and the backdrop uh I'm

play16:11

going to come up here and I'm doing this

play16:13

because it's on a dialogue and if you do

play16:14

this on a popover you'll want to use

play16:16

this as well which is we're going to add

play16:18

in overlay and overlay is just because

play16:21

uh when we open a modal you can actually

play16:23

see this inside of our Dev tools if I

play16:26

drag these on over uh there

play16:31

is oh look at that it's saying that it

play16:33

doesn't understand what's happening here

play16:35

why

play16:37

not we didn't break anything did we they

play16:40

got to update the dev tools look at that

play16:42

it's working

play16:44

fine uh see this top layer pill here

play16:46

though uh so the top layer is making

play16:49

sure that this is in front of everything

play16:50

else and popovers also use this top

play16:52

layer and it just ensures sort of like

play16:55

prevents any Z index issues basically

play16:57

from coming up it's bringing it all the

play16:58

way to the front and if you don't

play17:00

include the overlay as one of your

play17:02

things here it's possible when your

play17:03

animation's turning off or coming on

play17:06

that if you have Z index Z stuff going

play17:08

on the top layer only comes in at the

play17:10

end or halfway through I'm not sure

play17:12

exactly how it would work but it just

play17:14

you might have these layering issues

play17:15

whereas if you're mentioning the overlay

play17:18

here with the allow discreete everything

play17:19

should be fine so yeah there we go

play17:21

that's working it's up and down and we

play17:23

want to get that backdrop working so

play17:25

what we're going to do is let's do our

play17:27

backdrop and make it look a little bit

play17:28

fancy ier first so there we go I just

play17:31

added a red to blue gradient on there

play17:33

nothing too fancy so when I open it we

play17:35

hit the backdrop that comes in and then

play17:36

the backdrop disappears and once again

play17:38

we're going from a display none to a

play17:40

display of blocks we can use all the

play17:42

exact same things that we've uh learned

play17:44

along the way here to make that actually

play17:46

transition in uh and actually I'm doing

play17:48

this on my open let's just we can just

play17:49

it can always have that background image

play17:51

we don't need to uh change that so our

play17:54

background image is always there and

play17:55

this will be an opacity of zero and then

play17:57

when we have our Di dialog open we can

play18:01

change the backdrop and then here the

play18:03

opacity can be like 8.75 or something

play18:05

just so we can actually see through it

play18:07

now it's still not going to transition

play18:08

but we see that at least it's working uh

play18:10

and everything is there awesome the next

play18:13

thing we're going to do is let's come in

play18:15

with what we were looking at before

play18:16

where we do our transition properties

play18:20

right and we wanted our opacity but then

play18:22

we also need the display and we also

play18:24

need the overlay so our top layer works

play18:26

properly since we're transitioning we'll

play18:28

come in with a transition duration of 1

play18:30

second so it matches everything else

play18:32

that we've been doing so far and we're

play18:34

going to lastly come in with that

play18:36

transition behavior nice little bit of

play18:38

reinforcement here in this learning

play18:39

right you cover the same thing go a

play18:41

little bit faster the second time around

play18:43

uh of a loud discreet and in doing that

play18:45

we get it not working in this direction

play18:47

but it works when we go backwards or

play18:49

when we close it because we don't have a

play18:51

starting style and this is where the

play18:53

nesting thing comes up um because if I

play18:55

do it start starting style here this is

play18:58

going to drive people nuts if they don't

play19:00

watch this you a lot of people drop off

play19:01

when they're watching my videos they'll

play19:03

have watched the first half and they're

play19:04

gone now they're missing out on

play19:06

something that's going to drive them

play19:07

bananas one of those things is CSS that

play19:09

can be a little bit frustrating uh we're

play19:12

going to do an opacity of zero here and

play19:14

it's not going to

play19:16

work this is one of those uh annoying

play19:19

things with CSS um right because it's

play19:22

working and actually just really fast

play19:23

here you can see if I open and close it

play19:25

and then open it really fast it's like

play19:27

the it because it was at that point of

play19:29

the animation it's just continuing from

play19:31

where it was I think that's kind of cool

play19:34

right so like it it'd be kind of weird

play19:36

if it janked all the way back up but

play19:38

anyway I'm getting off topic uh this

play19:40

isn't working because I'm using nesting

play19:42

and you can't Nest inside of a pseudo

play19:45

element just the way it is with CSS and

play19:49

the way CSS nesting works so because of

play19:51

that I actually have to take this off

play19:53

and this is a limitation if you look up

play19:54

in like the actual spec it's in there um

play19:57

that you can't do that so what you need

play19:58

to do is actually do an at starting

play20:00

style here starting style and then in

play20:03

here and this is just it's an at rule so

play20:05

it works just like a media query or

play20:07

something like that where I can then

play20:08

bring rules inside of that and then here

play20:11

I can say that the opacity is going to

play20:13

be a uh zero and now we can close it and

play20:16

it closes and we can open it and it

play20:18

fades in as well and so if ever you have

play20:21

any pseudo element at all you cannot use

play20:24

nesting within that pseudo element it's

play20:26

one of those just gotchas of CSS I think

play20:28

this is so much cleaner so much nicer

play20:31

but if you do have a backdrop or

play20:32

something else that you want to be able

play20:34

to play with or animate or do whatever

play20:36

that involves nesting or you use a

play20:38

before and an after and you're trying to

play20:39

do this cuz maybe you're doing a display

play20:41

of nun to a display of block or whatever

play20:43

it is on a pseudo element you need the

play20:45

starting Style on the outside and the

play20:47

pseudo element nested inside of that and

play20:49

not the other way around now if you'd

play20:51

like to learn more about models and

play20:52

everything with like the dialogue here

play20:54

and the the show modal and the clothes

play20:56

and how they work and all the different

play20:58

stuff to do with them and more about the

play20:59

backdrop and all of that stuff I've gone

play21:01

in depth on how they work in this video

play21:03

that is right here and if you were

play21:04

interested in CSS demystified the link

play21:06

for that is in the description and with

play21:08

that I would like to thank my enablers

play21:09

of awesome who are Philip Andrew Simon

play21:11

and Tim as well as all my other patrons

play21:12

for their monthly support and of course

play21:14

until next time don't forget to make

play21:15

your corner of the internet just a

play21:17

little bit more awesome

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
CSS AnimationsTransitionsModal DesignWeb DevelopmentFrontendUI EnhancementJavaScript AlternativeKeyframesCSS PropertiesProgressive Enhancement