Initial doesn't do what you think it does

Kevin Powell · Beginner ·🛠️ AI Tools & Apps ·4y ago

Key Takeaways

The video discusses the CSS initial property, its effects on CSS properties, and alternative properties like onset and revert, with demonstrations using CSS and Chrome Canary.

Full Transcript

i'm going to put this display initial on these paragraphs here but before i do i want you to take a second i'll give you a second to think about it or pause the video and see if you can think of what's actually going to happen when i do this what's going to change what's going to stay the same keep it in mind and in three two one here we go and is that what you were expecting some of you probably got it right but for a lot of people you probably weren't expecting exactly that and if that's the case and you want to know what happened and other things you could use instead of initial that would behave more like you think they would then stick around because that's exactly what this video is going to be about hi there my friend and friends and welcome back to the channel i'm so glad that you've come to join me today and if you are new to my channel my name is kevin and here at my channel i hope you fall madly deeply in love with css and if not deeply in love with it at least you hate it a little bit less just before we dive into it i want you to take a moment to comment down below and let me know if you got it right when i before i set that paragraph to initial if you if you guessed right or if you knew what was actually going to happen there or not so leave a comment down below be honest about it it's okay when we get things wrong because we're all here to learn and part of learning is getting things wrong and learning from that and yeah we're here to learn about initial and why it's probably not what you want to be doing and or using and what you actually probably want to be using is either onset or revert so let's dive into the code and see how all of these things actually work all right so here we go and as you can see i put together a really really ugly page for this one and we're going to use this to explore initial and what it actually does and the other properties or in the properties or the other values that we have instead of it so the first thing is you can see i've styled some buttons here and see these are actually outlines on here just little tangent outlines are now getting border radiuses on them this is in chrome canary so it hasn't made the main chrome yet but it's on the way there which is super exciting and uh if you were at my last live stream with adam argyle then he's the one who let me know these are on the way which is super cool um all right so the first thing we're going to do is look at this button that we have right here so we have these three buttons that i've done and this is a button very different because sometimes this happens you style things in a very specific way i you know all of your buttons or something and then you need to get away from that and if you didn't know we have an all keyword that we can actually use our keyword it's a property uh so we have the all property which just selects every single property and that's often not useful but then maybe what you'll do is an initial on there ineo and when we do that it sort of goes back to not initial though right because often what people think initial does is to reset it as if we didn't do anything but that's not what's actually happening here because you know if we hadn't done anything we're gonna see how we can actually do that um but if we hadn't done anything then it would look like a normal button but it doesn't look like a button it's actually resetting it further than that and the thing that's weird about initial is let's just we're gonna scroll down a little bit let's go down to this other section this a different section here and i'm going to go to here section very different star which is selecting everything that's inside of this section and i'm going to say the same thing all and we'll do initial first and we're going to see you've got to spell things right if you want them to work all initial and you can see here that section is completely different so i had a container got rid of my container doing anything but i had multiple paragraphs in here just like we saw at the beginning they're all strung together uh the font has been reset as you'd sort of expect there but what the initial does is it actually looks at different ones so before we looked at display initial um and even you know we could even should we muck things up even more look at this we're just going to do a star and do a display initial watch the cree nish you'll watch the craziness that this will do where we can even see my my css showing up because it's even taking the the head and you know we're seeing things there's the title so what the initial keyword is doing is it's not looking at what it's being applied to so let's get rid of this because it's all kind of weird and what it's doing is it's it's looking at display but it's not looking at that this is a or even let's just do paragraphs now so it's a little bit more um clear so there we go it's not looking at what it's styling it's just going to the display property and going what's the going into the spec what's the initial value of the display property and the initial value of the display property is inline and that's before we get to like the user agent styles that actually say actually no a paragraph should be block and all of that we're going beyond that we're ignoring what the element is and we're just resetting everything to like the real core in the spec of every single property so that's why initial usually doesn't do what you actually want it to do and it can turn things in line it can do all sorts of strange stuff uh like here where it's just completely reset my button so this is where we have other options like i said we have onset and we have revert onset is the first one that we got because i've been doing this for a long time we used to only have initial and this is like you know i was doing this when we were making table-based layouts we only had initial so we just um and in certain cases you could use it for for certain properties i wouldn't use it on the all though just because it does crazy stuff but all initial and instead of that we can use an onset and you'll notice that onset is going to do some things similarly but something's different where we still have my button my outline is showing up just because that's styled on the hover and stuff we're not going to worry about that you'll notice the font is different but the font size and the color and all that have come in so let's just do all onset here just we can comment things on and off if we feel like we need to so what the difference between onset and initial is onset will look if the property should is traditionally inherited or not so in this case normally the font family so font family or the color are inherit so instead of going all the way back and setting things to like that crazy thing where it's going back to black it's like going back to the original like everything uh where it's not even inheriting anything there's no inheritance happening now for color for font size for fun family all of that is out the window uh whereas if we do an all on set it's going to set anything that should be inherited or that normally is inherited it will still be inherited now you might be saying but kevin like buttons normally don't inherit anyway and i know that but let's say we come on my paragraph here and i just say all um initial you'll see that like my all my paragraphs here they're going to go back to black and we can see they're becoming inline it's just going to like default city uh it's not using so we lose a lot of the power of css when we use initial because that inheritance is gone so that's onset which sets things back now for a lot of things like say even here we did um display is on set it's going to behave just like we saw before where it's going to make it all in line because the display property is not something that's inherited so if something is not inherited then it just goes back to that factory factory reset of the display property not for my paragraphs themselves and that's not the greatest thing in the world but just so we know that's what it does um and this was sort of the next stage along the way it's in my opinion better than initial but it's still a little bit heavy-handed as you can see there where some things you might have unintended consequences sometimes or then there's the one that you probably want to be using this entire time though sometimes maybe onset is the right call and so that's where we have in all and i'm just doing all we don't have to always do it on an all but it sort of really shows what's happening we have the revert and i'm leaving these on just because like you know this is replacing this one because the order of the cascade here so i'll revert look it's gone back to being a default button just like you'd expect it to be and this is the really cool one and i love revert and what it's basically doing is going get rid of any styles that are applied on it but go back to the user agent styles that we would have started with if there was no css applied here in the first place whereas onset sort of strips things back a little bit further than that this is really the revert keyword here as a value is going to what's the user agent style doing and ignore the css that was previously applied by the author who's authoring the css and this is often what we think initial is actually doing so if you do need something like this uh revert can be really useful so sometimes you might want the onset other times you might want revert it depends on which one you want to do uh maybe you try one you try the other but i really do think that most of the time you don't want to be using initial and if you've learned a thing or two during this video i have a little playlist for you right here of other css properties that often catch people off guard that you can check out and with that a really big thank you to zack randy and stewart who are my supporters of awesome over on patreon as well as all my other patrons for their monthly support and of course until next time don't forget to make your corn on the internet just a little bit more awesome

Original Description

We often think initial sets something back to how it started, but it actually does a lot more than that. Often, we're looking for unset or revert instead. 🔗 Links ✅ Codepen: https://codepen.io/kevinpowell/pen/qBjqwJO ✅ Twitch: https://www.twitch.tv/kevinpowellcss ✅ Additional reading: https://developer.mozilla.org/en-US/docs/Web/CSS/revert ✅ Other CSS behaviors that work in strange ways: https://www.youtube.com/watch?v=qKiz9gdJdr8&list=PL4-IK0AVhVjMdy1JNSpL-_4UnDmY2MpDm&index=1 ⌚ Timestamps 00:00 - Introduction 01:24 - What we are starting with and an aside about outlines 01:55 - A look at what initial actually does 04:50 - unset 07:11 - revert #css -- Come hang out with other dev's in my Discord Community 💬 https://discord.gg/nTYCvrK Keep up to date with everything I'm up to ✉ https://www.kevinpowell.co/newsletter Come hang out with me live every Monday on Twitch! 📺 https://www.twitch.tv/kevinpowellcss --- Help support my channel 👨‍🎓 Get a course: https://www.kevinpowell.co/courses 👕 Buy a shirt: https://teespring.com/stores/making-the-internet-awesome 💖 Support me on Patreon: https://www.patreon.com/kevinpowell --- My editor: VS Code - https://code.visualstudio.com/ --- I'm on some other places on the internet too! If you'd like a behind the scenes and previews of what's coming up on my YouTube channel, make sure to follow me on Instagram and Twitter. Twitter: https://twitter.com/KevinJPowell Codepen: https://codepen.io/kevinpowell/ Github: https://github.com/kevin-powell --- And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Kevin Powell · Kevin Powell · 0 of 60

← Previous Next →
1 How to create an awesome navigation bar with HTML & CSS
How to create an awesome navigation bar with HTML & CSS
Kevin Powell
2 Improve your CSS by Keepin' it DRY
Improve your CSS by Keepin' it DRY
Kevin Powell
3 HTML & CSS for Beginners Part 6: Images
HTML & CSS for Beginners Part 6: Images
Kevin Powell
4 HTML & CSS for Beginners Part 7: File Structure
HTML & CSS for Beginners Part 7: File Structure
Kevin Powell
5 HTML & CSS for Beginners Part 4: Bold and Italic text and HTML comments
HTML & CSS for Beginners Part 4: Bold and Italic text and HTML comments
Kevin Powell
6 HTML & CSS for Beginners Part 5: Links
HTML & CSS for Beginners Part 5: Links
Kevin Powell
7 HTML & CSS for Beginners Part 3: Paragraphs and Headings
HTML & CSS for Beginners Part 3: Paragraphs and Headings
Kevin Powell
8 HTML and CSS for Beginners Part 1: Introduction to HTML
HTML and CSS for Beginners Part 1: Introduction to HTML
Kevin Powell
9 HTML and CSS for Beginners Part 2: Building your first web page!
HTML and CSS for Beginners Part 2: Building your first web page!
Kevin Powell
10 HTML & CSS for Beginner Part 8: Introduction to CSS
HTML & CSS for Beginner Part 8: Introduction to CSS
Kevin Powell
11 HTML & CSS for Beginners Part 9: External CSS
HTML & CSS for Beginners Part 9: External CSS
Kevin Powell
12 HTML & CSS for Beginners Part 10: Divs & Spans
HTML & CSS for Beginners Part 10: Divs & Spans
Kevin Powell
13 HTML & CSS for Beginners Part 11: Classes & IDs
HTML & CSS for Beginners Part 11: Classes & IDs
Kevin Powell
14 HTML & CSS for Beginners Part 12: The CSS Box Model - Margin, Borders & Padding explained
HTML & CSS for Beginners Part 12: The CSS Box Model - Margin, Borders & Padding explained
Kevin Powell
15 HTML & CSS for Beginners Part 13: Background Images
HTML & CSS for Beginners Part 13: Background Images
Kevin Powell
16 HTML & CSS for Beginners Part 14: Style Text with CSS
HTML & CSS for Beginners Part 14: Style Text with CSS
Kevin Powell
17 HTML & CSS for Beginners Part 15: How to style links
HTML & CSS for Beginners Part 15: How to style links
Kevin Powell
18 HTML & CSS for Beginners Part 16: CSS selectors and Specificity
HTML & CSS for Beginners Part 16: CSS selectors and Specificity
Kevin Powell
19 HTML & CSS for Beginners Part 17: How to Create and Style HTML Lists
HTML & CSS for Beginners Part 17: How to Create and Style HTML Lists
Kevin Powell
20 HTML & CSS for Beginners Part 18: How Floats and Clears work
HTML & CSS for Beginners Part 18: How Floats and Clears work
Kevin Powell
21 HTML & CSS for Beginners Part 19: Colors with CSS - hex, rgba, and hsla
HTML & CSS for Beginners Part 19: Colors with CSS - hex, rgba, and hsla
Kevin Powell
22 HTML & CSS for Beginners Part 20: How to center a div
HTML & CSS for Beginners Part 20: How to center a div
Kevin Powell
23 HTML & CSS for Beginners Part 21: How to create a basic website layout - the HTML
HTML & CSS for Beginners Part 21: How to create a basic website layout - the HTML
Kevin Powell
24 HTML & CSS for Beginners Part 22: How to create a basic layout - the CSS
HTML & CSS for Beginners Part 22: How to create a basic layout - the CSS
Kevin Powell
25 How to Create a Responsive Website from Scratch - Part 1: The HTML #Responsive #HTML5
How to Create a Responsive Website from Scratch - Part 1: The HTML #Responsive #HTML5
Kevin Powell
26 How to Create a Responsive Website from Scratch - Part 2: The Header and Hero area #Responsive #CSS3
How to Create a Responsive Website from Scratch - Part 2: The Header and Hero area #Responsive #CSS3
Kevin Powell
27 How to Create a Responsive Website from Scratch - Part 3: The About Section #Responsive #CSS
How to Create a Responsive Website from Scratch - Part 3: The About Section #Responsive #CSS
Kevin Powell
28 How to Create a Responsive Website from Scratch - Part 4: Building a Responsive Portfolio Section
How to Create a Responsive Website from Scratch - Part 4: Building a Responsive Portfolio Section
Kevin Powell
29 How to Create a Responsive Website from Scratch - Part 5: Call To Action and Footer #CSS #Responsive
How to Create a Responsive Website from Scratch - Part 5: Call To Action and Footer #CSS #Responsive
Kevin Powell
30 Tutorial: Learn how to use CSS Media Queries in less than 5 minutes
Tutorial: Learn how to use CSS Media Queries in less than 5 minutes
Kevin Powell
31 End of the year upate and what's coming to my channel to start the new year
End of the year upate and what's coming to my channel to start the new year
Kevin Powell
32 Create a CSS only Mega Dropdown Menu
Create a CSS only Mega Dropdown Menu
Kevin Powell
33 CSS Tutorial: Outline and Outline Offset
CSS Tutorial: Outline and Outline Offset
Kevin Powell
34 CSS Blending Modes
CSS Blending Modes
Kevin Powell
35 Parallax effect | 2 different ways to add it with jQuery
Parallax effect | 2 different ways to add it with jQuery
Kevin Powell
36 CSS Units: vh, vw, vmin, vmax #css #responsive #design
CSS Units: vh, vw, vmin, vmax #css #responsive #design
Kevin Powell
37 How to Create a Website - Complete workflow | Part 01: Intro + Setting things up
How to Create a Website - Complete workflow | Part 01: Intro + Setting things up
Kevin Powell
38 100 Subscribers speed coding bonus video
100 Subscribers speed coding bonus video
Kevin Powell
39 How to Create a Website - Complete workflow | Part 02: The Markup #HTML
How to Create a Website - Complete workflow | Part 02: The Markup #HTML
Kevin Powell
40 How to Create a Website - Complete workflow | Part 03: Sass Variables and a Mixin #Sass
How to Create a Website - Complete workflow | Part 03: Sass Variables and a Mixin #Sass
Kevin Powell
41 How to Create a Website - Complete workflow | Part 04: Setting up the hero and header
How to Create a Website - Complete workflow | Part 04: Setting up the hero and header
Kevin Powell
42 How to Create a Website - Complete workflow | Part 05: Typography & Buttons
How to Create a Website - Complete workflow | Part 05: Typography & Buttons
Kevin Powell
43 How to Create a Website - Complete workflow | Part 06.1: Building the navigation with Flexbox
How to Create a Website - Complete workflow | Part 06.1: Building the navigation with Flexbox
Kevin Powell
44 How to Create a Website - Complete workflow | Part 06.2: Making the nav work with jQuery
How to Create a Website - Complete workflow | Part 06.2: Making the nav work with jQuery
Kevin Powell
45 Redesigning & Coding My Website #CreateICG
Redesigning & Coding My Website #CreateICG
Kevin Powell
46 How to Create a Website - Complete workflow | Part 07: Starting the flexbox grid
How to Create a Website - Complete workflow | Part 07: Starting the flexbox grid
Kevin Powell
47 How to Create a Website - Complete workflow | Part 08: Promo & Problem shooting!
How to Create a Website - Complete workflow | Part 08: Promo & Problem shooting!
Kevin Powell
48 How to Create a Website - Complete workflow | Part 09: The CTA and Footer
How to Create a Website - Complete workflow | Part 09: The CTA and Footer
Kevin Powell
49 How to Create a Website - Complete workflow | Part 10: Making it responsive
How to Create a Website - Complete workflow | Part 10: Making it responsive
Kevin Powell
50 How to Create a Website - Complete workflow | Part 11: Making it responsive con't
How to Create a Website - Complete workflow | Part 11: Making it responsive con't
Kevin Powell
51 How to Create a Website - Complete workflow | Part 12: Putting the site online
How to Create a Website - Complete workflow | Part 12: Putting the site online
Kevin Powell
52 Create a Custom Grid System with CSS Calc() and Sass
Create a Custom Grid System with CSS Calc() and Sass
Kevin Powell
53 CSS em and rem explained #CSS #responsive
CSS em and rem explained #CSS #responsive
Kevin Powell
54 Should you use Bootstrap?
Should you use Bootstrap?
Kevin Powell
55 How to add Smooth Scrolling to your one page website with jQuery
How to add Smooth Scrolling to your one page website with jQuery
Kevin Powell
56 Let's learn Bootstrap 4
Let's learn Bootstrap 4
Kevin Powell
57 How I approach designing a website - my thought process
How I approach designing a website - my thought process
Kevin Powell
58 Build a website with Bootstrap 4 - Part 1: The setup
Build a website with Bootstrap 4 - Part 1: The setup
Kevin Powell
59 Build a website with Bootstrap 4 - Introduction
Build a website with Bootstrap 4 - Introduction
Kevin Powell
60 Build a website with Bootstrap 4 - Part 2:  Customizing Variables
Build a website with Bootstrap 4 - Part 2: Customizing Variables
Kevin Powell

The video teaches how to use the initial property in CSS to reset properties to their browser defaults, and how to use onset and revert as alternatives, with examples and demonstrations.

Key Takeaways
  1. Use the all keyword to select every single property
  2. Use the initial property to reset CSS properties to their browser defaults
  3. Use onset and revert to reset CSS properties to their default values
  4. Reset CSS properties to their initial values using the initial keyword
  5. Use the onset keyword to reset properties to their initial values while considering traditional inheritance
  6. Test the effects of using initial and onset on different elements and properties
💡 The initial property resets all CSS properties to their initial values, ignoring the element's properties, and can cause unexpected behavior, while onset and revert provide more targeted reset options.

Related Reads

📰
Three Token-2022 Mints in One Week: Fees, Yield, and Soulbound
Learn how to build three different Token-2022 mints on Solana devnet in one week, including fee-bearing, interest-accruing, and soulbound tokens
Dev.to · atharv shukla
📰
Maximize Google Workspace AI Power: Safeguard Data and Boost Performance in 2026
Maximize Google Workspace AI power by safeguarding data and boosting performance to stay ahead in 2026
Dev.to AI
📰
What is Gemini Spark, and what can it actually do for you?
Gemini Spark integrates AI into daily Google apps to enhance productivity, learn how to leverage it
TechCabal
📰
How I use python to save hours every week
Learn how to use Python to automate tasks and save hours every week, with practical steps for beginners and experienced users alike.
Dev.to AI

Chapters (5)

Introduction
1:24 What we are starting with and an aside about outlines
1:55 A look at what initial actually does
4:50 unset
7:11 revert
Up next
Implement Microsoft Entra ID Auth with Delegated Graph API Calls in ASP.NET Core Web App Razor Pages
Dewiride Technologies
Watch →