The problem with shorthands in CSS
Key Takeaways
The video discusses the potential issues with using shorthands in CSS, including unexpected behavior and overwriting of styles, and provides guidance on how to use them effectively, highlighting the importance of understanding longhand properties and utility classes.
Full Transcript
shorthands like margin padding and background are fantastic because they let us be lazy and write less code to get the same thing done so we can work a little bit faster but despite that most of the time i'd actually advise you not to use them hello my friend and friends i'm so glad that you've come to join me for yet another video and if you're new here my name is kevin here at my channel i help you fall madly deeply in love with css and if i can't get you to fall in love with it i'm hoping to at least help you be a little bit less frustrated by it and one of the best ways to be less frustrated with css is to write more maintainable code so let's jump right into the code and we're going to start with this example here of why i think you shouldn't use shorthands we'll see when shorthands are actually a good thing as well as my favorite shorthand that actually overcomes some of these problems so let's start off here where the this is sas so if you're not really super familiar with what this looks like don't worry too much but here i have a background image and we're going to come in and apply a background image regular css and it should give us a background right here where i have this dark background and um it didn't work let's refresh it still didn't work hmm what's going on here well what's going on here is i used a utility class to set the background here which is my inverted and this utility class is my background and here it is my background color and verse again don't worry let's just come and make this like 333 so it doesn't look too fancy and see it's updated there and i have this color coming in but my background image is not and that's because this css is coming in after the other one they're both a single class selector their equal specificity whatever comes after wins and this background shorthand well it's actually doing a lot more than just setting a background color because if we come and we look in here there's the background let's make this bigger so we can really see it and there it is so we have our background right here you can see it's setting the color but it's also setting a whole bunch of other things to initial including my background image so my original background image right here is being overwritten it's crossed off because this is being reset to initial and what that means is i should have come here and used the long hand of background color and now look at that my image has come in and it's working as intended now another issue that can come up from things like this is when we're dealing with our spacing and you can see here i have some text that's center aligned but not um aligned aligned if you know what i mean um and that's because this text here if we come and look it has a max width on it of 45 ch just prevents the the line lengths from being too long if you've never heard about ch i'll link to a video in the description or there should be a card popping up as well and uh yeah so we're limiting the max width of this and that means the text is let's just actually on here we can just add a background of lime just so we can see it and you can see that it's centered within that 45 ch but the 45 ch itself isn't centered and that's actually true for up here as well um for both of these because my title here also has a max width of 30 ch on it so if we throw a background of lime on this one uh you'll see that it's much closer to this side of the page than it is over here and so let's turn that off and let's come and see how we could fix that so i have some spacing classes on here and right now there is some space underneath here with a margin bottom two utility class that i've created and i could delete that and hit save just so you can see it update and you can see it's glued so i want to keep that space i'm going to hit my save there but for both of these we do want to also have them centered right and i have another utility class we can see that right here of an mx auto which is just my margin 0 auto and if i come over here i can write mx auto and this is just a common name so that's why i'm using it here for mx auto mx is for the x-axis and hit save on both of those and it will center them but you'll notice the space that was in between them has disappeared wait why why is that space disappearing once again this is short hands coming to bite s and i have an mx auto and a margin bottom of auto and if we go and find that we can see here that my margin bottom is well it's set to a margin block n so we don't actually see it being crossed off and i'll explain what this is in a second if you're not familiar with it but you can see here that my margin zero auto is forcing the zero on the top and the bottom which is overriding that space that i originally had so if i turn this off that space underneath comes back and then that space disappears i want to have both of them so how can i do that well let's come into here actually let's just refresh that so let's come on over to my spacing and one thing is i could just make sure that this came before my this is the one creating the bottom thing so i could you know order my css but i don't want to have to worry about what order things come in i could just come here and do a margin left of auto and a margin right of auto and while it's more verbose you can see that it's now working because it's not overwriting the bottom margin that i've added to this element right here now one shorthand that we could actually use in this situation if we wanted to is to use the margin inline and put auto on that and nothing will look like it's changed because nothing has changed because what margin inline is doing it's a logical property that is only applying to the inline axis or you know mx auto it's applying to the x axis now it is a logical property meaning if we were to do a left to right or right to left language it would apply it's both the left and the right but if ever you had a vertical writing mode it would switch a little bit like flex direction switches but that would sort of be what you'd want to happen anyway and this is one of those short hands i really really like because it means i don't have to touch when i was because when i was doing a margin zero auto i was saying zero just so i would have nothing i wanted like i don't care about that i just want the auto on the left and the right well this accomplishes that and it does a really good job of it and of course you might want to be wondering what if i only want to target the top and the bottom well that's where we do have the margin block and so margin block if i did a say 10 pixels that would be margin top and a margin bottom of 10 pixels it would apply to both and then we have a margin block end which is only on the bottom and a margin block start which would only be on the top if you'd like to know more about logical properties i have talked about it in a video before and it will be linked down in the description and just continuing off of there you see the i have these cards that have some like a hover effect on them which we can turn off on mobile um but with that hover effect i'm using some positioning absolute and that's where my favorite shorthand property comes which is on my cards here and you can see i have a position absolute and right now they're set to a bottom of zero and for the moment i am going to turn off the transform uh so they don't actually disappear just so we don't we can just see them all the time on there and instead of a bottom of zero let's just do an inset of zero an inset is a shorthand it's a logical property for top bottom left and right and you can see it's covering my entire one because they're all set to zero what's interesting with the inset shorthand it's the only shorthand that allows you to explicitly omit properties and i know like say you have a font shorthand like you don't have to put them all or if you have the background shorthand you don't have to list them all but with inset you do have to list them and you can do that by using the auto keyword so i knew auto for the top and then zero zero zero and the auto in this case is is as if i didn't declare it at all so if we do that it goes back to how i have it because i'm not declaring the top but i am declaring the bottom right and left and that sort of leads us to when i think you should be using shorthands which are in the cases like here where i want both the left and the right or maybe you want to just use the margin shorthand and you need to declare all four sides so of course if you're doing all of them are a border you're using your border shorthand because you need a border on all the sides the shorthand makes a lot of sense but especially the font property and the background property they can actually cause so much more trouble than they're worth because and even the font one unless you know the specific order things need to go in it's kind of annoying whereas the border one at least at least if you're setting a border you can just sort of mix it up and it doesn't matter what order things are in and while some of these that i looked at you might be going kevin i'm never i've never run into that it's not going to be a problem if you're always using shorthands i guarantee you one day it's going to come back to bite you and you're just going to be like oh and then you sort of have to go back and fix your code and declare things you need to be declaring don't use properties that add extra declarations to what you need that are sort of hidden away within the shorthand because they do come back they do cause you issues and if you're curious about the project that i was working on in this video it is actually part of a new course that i'm working on called beyond css if you'd like to sign up for updates as that course launch approaches the link to it is in the description just below and during this video i did mention several other videos so if you'd like to check out any of those there is a playlist that has them all right here or once again the links in the description and with that a very big thank you to jan johnny stewart and tim 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
Interesting in my upcoming course Beyond CSS? Sign up for updates and don't miss the launch: https://beyondcss.dev/
Shorthands in CSS can serve a purpose, but it's important to know the potential issues that can arise when we use them!
🔗 Links
✅ The ch unit: https://www.youtube.com/watch?v=dgbFtMBOMlA
✅ Logical properties: https://youtu.be/kzvmaVik4mA
✅ More on inset: https://youtu.be/8mv1HJgh6-o
⌚ Timestamps
00:00 - Introduction
00:36 - The background property causing trouble
01:20 - Why it's a problem
02:10 - Fixing it with the longhand
02:28 - Margin shorthand causing problems
04:36 - Fixing the margin issue
06:34 - The inset shorthand for positioning
07:45 - When it's alright to use shorthands
#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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How to create an awesome navigation bar with HTML & CSS
Kevin Powell
Improve your CSS by Keepin' it DRY
Kevin Powell
HTML & CSS for Beginners Part 6: Images
Kevin Powell
HTML & CSS for Beginners Part 7: File Structure
Kevin Powell
HTML & CSS for Beginners Part 4: Bold and Italic text and HTML comments
Kevin Powell
HTML & CSS for Beginners Part 5: Links
Kevin Powell
HTML & CSS for Beginners Part 3: Paragraphs and Headings
Kevin Powell
HTML and CSS for Beginners Part 1: Introduction to HTML
Kevin Powell
HTML and CSS for Beginners Part 2: Building your first web page!
Kevin Powell
HTML & CSS for Beginner Part 8: Introduction to CSS
Kevin Powell
HTML & CSS for Beginners Part 9: External CSS
Kevin Powell
HTML & CSS for Beginners Part 10: Divs & Spans
Kevin Powell
HTML & CSS for Beginners Part 11: Classes & IDs
Kevin Powell
HTML & CSS for Beginners Part 12: The CSS Box Model - Margin, Borders & Padding explained
Kevin Powell
HTML & CSS for Beginners Part 13: Background Images
Kevin Powell
HTML & CSS for Beginners Part 14: Style Text with CSS
Kevin Powell
HTML & CSS for Beginners Part 15: How to style links
Kevin Powell
HTML & CSS for Beginners Part 16: CSS selectors and Specificity
Kevin Powell
HTML & CSS for Beginners Part 17: How to Create and Style HTML Lists
Kevin Powell
HTML & CSS for Beginners Part 18: How Floats and Clears work
Kevin Powell
HTML & CSS for Beginners Part 19: Colors with CSS - hex, rgba, and hsla
Kevin Powell
HTML & CSS for Beginners Part 20: How to center a div
Kevin Powell
HTML & CSS for Beginners Part 21: How to create a basic website layout - the HTML
Kevin Powell
HTML & CSS for Beginners Part 22: How to create a basic layout - the CSS
Kevin Powell
How to Create a Responsive Website from Scratch - Part 1: The HTML #Responsive #HTML5
Kevin Powell
How to Create a Responsive Website from Scratch - Part 2: The Header and Hero area #Responsive #CSS3
Kevin Powell
How to Create a Responsive Website from Scratch - Part 3: The About Section #Responsive #CSS
Kevin Powell
How to Create a Responsive Website from Scratch - Part 4: Building a Responsive Portfolio Section
Kevin Powell
How to Create a Responsive Website from Scratch - Part 5: Call To Action and Footer #CSS #Responsive
Kevin Powell
Tutorial: Learn how to use CSS Media Queries in less than 5 minutes
Kevin Powell
End of the year upate and what's coming to my channel to start the new year
Kevin Powell
Create a CSS only Mega Dropdown Menu
Kevin Powell
CSS Tutorial: Outline and Outline Offset
Kevin Powell
CSS Blending Modes
Kevin Powell
Parallax effect | 2 different ways to add it with jQuery
Kevin Powell
CSS Units: vh, vw, vmin, vmax #css #responsive #design
Kevin Powell
How to Create a Website - Complete workflow | Part 01: Intro + Setting things up
Kevin Powell
100 Subscribers speed coding bonus video
Kevin Powell
How to Create a Website - Complete workflow | Part 02: The Markup #HTML
Kevin Powell
How to Create a Website - Complete workflow | Part 03: Sass Variables and a Mixin #Sass
Kevin Powell
How to Create a Website - Complete workflow | Part 04: Setting up the hero and header
Kevin Powell
How to Create a Website - Complete workflow | Part 05: Typography & Buttons
Kevin Powell
How to Create a Website - Complete workflow | Part 06.1: Building the navigation with Flexbox
Kevin Powell
How to Create a Website - Complete workflow | Part 06.2: Making the nav work with jQuery
Kevin Powell
Redesigning & Coding My Website #CreateICG
Kevin Powell
How to Create a Website - Complete workflow | Part 07: Starting the flexbox grid
Kevin Powell
How to Create a Website - Complete workflow | Part 08: Promo & Problem shooting!
Kevin Powell
How to Create a Website - Complete workflow | Part 09: The CTA and Footer
Kevin Powell
How to Create a Website - Complete workflow | Part 10: Making it responsive
Kevin Powell
How to Create a Website - Complete workflow | Part 11: Making it responsive con't
Kevin Powell
How to Create a Website - Complete workflow | Part 12: Putting the site online
Kevin Powell
Create a Custom Grid System with CSS Calc() and Sass
Kevin Powell
CSS em and rem explained #CSS #responsive
Kevin Powell
Should you use Bootstrap?
Kevin Powell
How to add Smooth Scrolling to your one page website with jQuery
Kevin Powell
Let's learn Bootstrap 4
Kevin Powell
How I approach designing a website - my thought process
Kevin Powell
Build a website with Bootstrap 4 - Part 1: The setup
Kevin Powell
Build a website with Bootstrap 4 - Introduction
Kevin Powell
Build a website with Bootstrap 4 - Part 2: Customizing Variables
Kevin Powell
More on: Prompt Craft
View skill →Related Reads
📰
📰
📰
📰
TSMC’s $265B US Expansion: Four New Chip Fabs Planned
TechRepublic
Google Is Winning the AI Race and Losing Its Business Model at the Same Time
Medium · AI
China Just Overtook America on the Only Metric That Predicts Who Builds the Future
Medium · AI
AI Will Not Save You from Thinking: Why Polymathy Is Becoming the Real Career Advantage
Medium · AI
Chapters (8)
Introduction
0:36
The background property causing trouble
1:20
Why it's a problem
2:10
Fixing it with the longhand
2:28
Margin shorthand causing problems
4:36
Fixing the margin issue
6:34
The inset shorthand for positioning
7:45
When it's alright to use shorthands
🎓
Tutor Explanation
DeepCamp AI