A better image reset for your CSS

Kevin Powell · Beginner ·🛠️ AI Tools & Apps ·2y ago
Skills: HTML & CSS80%

Key Takeaways

The video discusses a better image reset for CSS, including setting max-width, display, background-size, font-style, and shape-margin, and demonstrates how to optimize images and improve page loading performance using CSS techniques and Dev tools.

Full Transcript

hi there my frontend friends normally we see something like this used as a reset for our images and it works really well but what if I were to tell you that maybe we should be using this instead now you might be thinking that I'm trolling you because why would you want a background image on an image and I get it cuz I was thinking the same thing and I actually saw this uh from a post by Harry Roberts and when I first saw it I wasn't sure if he was being serious or not he talks a lot about CSS performance and I thought maybe he was found something along the way on a site where were doing something kind of weird and was you know posting this a little bit in Gest but he had a whole thread where he went through it Point by point and as I went through the entire thing each one of those lines actually started to make a whole lot of sense and I wanted to spread the word about it a little bit so we're going to go through this code right here and we're going to see why maybe you might actually want to use a whole bunch of these on your images so let's dive into the code and we're going to see right here I just have a simple thing set up with an image at the top we're going to be using this to sort of go through all of the different things uh right now we just have an image that's right here with an ALT attribute on there with you know a description of what my image is and I have put a width and a height uh which are just generally a good thing to have on there and it's going to help us with one of the things that we're going to look at along the way and in general it is considered a best practice to have your width and and height on there so I would suggest having those even though that's not part of what we're looking at for now though I am going to take this width and height off um just because you're going to see my I'm I'm purposely bringing in an image that's way too big uh please optimize your images at least a little bit uh and don't bring in Giant images like this uh but this does bring up the first thing and this is the one that I think everybody can agree on right if you the first line that Harry shared there was a Max width of 100% on our images which just stops them from overflowing which is nice it's you know one of those lines that we often include uh but normally we also throw a display block on there and when I do that actually see this text right here I'm going to comment this out and hit save and you can see it's moved down a little bit and then when I put that back on it moves back up and the reason that's happening let's turn off this display of block and if I come here images are sort of like this weird inline slash not inline element um so if I put it inside my paragraph and of course the width is too big now let's just say that the max width is 250 pixels to make it smaller you can see the text is in or the image is in here with this um this text right it it's just lives in the text and we could even you know take this word and put it in front of my image and it just lives in there and the reason that we have is we have this like little bit of space underneath and let's like really zoom in on here um but like we have descender characters like this J that go underneath and images are meant to be on this Baseline of the text like normal characters are not the ones that descend down uh and normally if we do a display of block that just gets rid of that space underneath but Harry's suggestion was instead of bothering with a display of block was to do a vertical align of middle and if we do that now it breaks everything here so it is assuming that you're not going to be putting your images inside of text obviously uh but let's go and grab our image and not put it in there cuz that's not something we would normally do anyway and if I did a display block it would also completely ruin it so either way um it wouldn't work but by doing that if we turn off that vertical line middle you can see we have that space showing up and when I put that back on it removes it and it keeps that like display inline block style thing that images normally have so we're not changing the behavior we're just getting rid of that alignment issue so it's very similar to doing a display block but a little bit less heavy-handed uh along the way which is why he suggested that using that instead of using our display block on there uh so a vertical align of middle gives us that same desired Behavior now we also had on here to do a height of Auto and by having the height of Auto in here we are just making sure that our image is actually going to work the way that we're we're expecting it to work um so it is one of those ones you know it's going to keep the aspect ratio of the image and as I said before on the image normally you do want to have the width and height on there um and so by having this this is telling the browser what the aspect ratio is and then this is just making sure that that aspect ratio will always be maintained now the next one is the first one for me that was like why would I ever want to do that and we'll throw it up here at the top uh in its font style italic why would you why would you want to do this take a guess um but there actually was a good reason for it and it's if ever you know we have alt text for two reasons really we have it for screen readers so if somebody who's using a screen reader comes on this and they can't see the image the screen reader can read the description of it uh you also say it's used for SEO and stuff but it's also if the image doesn't load it could be a network issue if you have a very large image which we do here we're going to look at some other things with that um but it could be you know there's a typo there could be a lot of reasons an image doesn't load and look at that the alt text actually comes in as italic instead of coming in uh you know like this and the advantage with that is you're setting it apart from the rest of it to sort of indicate that this is this text is actually a little bit different uh cuz the little broken image icon is not the most obvious thing in the world so we add this one line here in just a fail safe right so it's not really going to impact things almost ever but in the rare situation we're just sort of offsetting this text making a little bit different um in you know if ever we do need it or if ever it does show up now the next one uh F that one I sort of figured out pretty fast this next set there's actually two of them the the first one is our background repeat of no repeat uh along with a back background size of cover and this was more of a head scratcher for me I really couldn't figure out uh why we'd want to do that and I actually ended up on a blog post of Harry's that I'm going to link below because he gives a lot of Det details on how to optimize things and make sure that things are well set up for this to actually work properly um but it has to deal with sort of like a fake lazy loading of images almost uh where if you're on either a slow network connection or if your images are are way too big like this one is here um so the idea with this is you actually want to have a low reses version of your images so I have my Hills and then I'm also going to have this Hills low res which is a very low res actually let's go and take a look at how low res it is uh low res you can see it's really disgusting and blurry uh now if you're going to implement this please check the blog post from Harry that I'm linking down below because it goes into detail on sort of the size and quality ratio that you really want to make sure you're hitting for this uh I'm just going to give a brief overview of why you might want to do it and if you're interested please check out Harry's post uh and of course there's ways of like having these generated for you right you don't have to do this all manually if you have a blog or something with lots of images um but here we have my Lowes or no we want our regular one here but then I'm going to come and I'm going to say that the uh we're going to do a style is equal to background image URL and we're going to bring in our low res one as a background image instead so that was Hills low res and you might say well what's the point right I reload my site and I'm why why would I see a background image here yeah that's a good question uh but what happens if we come in and in this case as I said my my original image is like three mags I purposely made it really big just for the demo uh but I'm going to come in my Dev tools here and we're going to go to network and I'm going to simulate a slow 3G site uh and there's one more step actually we might want to take here but we're just going to I'm going to have that on and we're going to refresh the page and Watch What Happens here as the page refreshes we're going to get a blank then we get the low res one and while the low res one is acting as a placeholder there the highres image is also coming in that's pretty cool right uh so it's sort of like almost like a skeleton loading but with the original image itself there first which is coming in as a low reses background image and one last thing though if you are going to use this approach which I think is really cool is uh what you can also do just to ensure that the image loads in so I hit save there and it took a minute with the the slow 3G coming in um but we can actually preload the low version of the images as well so uh we can have that set up and again this would probably be some uh something you'd want to automate a little bit instead of manually setting this all up but it does depend if you just have one hero image and you probably don't need to do this for other images right you can lazy load lower down images and other stuff but maybe you have an hero image coming in um or something that is a little bit bigger um it you know in that case maybe you could manually set this up and it's really not the end of the world um and you just do something like this where we're pre-loading the low reses version in so make sure that's coming in and write it basically as the page comes in and then we can have the other one slowly come in after if somebody's on that slow 3G uh Network or whatever it is you know you have a bad Wi-Fi connection or or whatever it is um but it just makes it so there's at least something there instead of a jump in content which is always terrible uh which is also why you want the width and the height on here right because if you don't have a width and height defined it's possible that that's also where you as the image loads in that's when content starts shifting around which is also the worst and of course there was one more in here which I found really interesting and it's to come in with a shape uh margin of uh he did 75 it's just the idea here is just to have a default shape margin uh on it and this would be in those rare situations where you might be coming in and floating an image so as a really quick example of that I just have this one here uh where I have this image that's a circle and it's floating to the left but I've set it up to have a you know here's the the float left on the image and of course I've set this up a little bit fancier but we do the float left and then it wraps around but that's also happening because I'm using the shape outside here so if I took off shape outside it gives me you know obviously that's not quite what we're after uh so the shape outside is saying that it's a circle so the text can actually wrap around and then I can have a shape margin on there and actually give it the spacing that I want and you can you know play around with different numbers there to control the space around the image uh and this isn't something that we use every day so maybe you don't want to bother with it but it's you know it's inert it's not doing anything unless you actually end up floating something so it's one line of CSS it's not like it's going to on optimize your site or something just because you're not using it 99% of the time again you don't have to include it but it could just be one of those defaults to have in there just in case you ever run into a situation where you need to do something like that and if you'd like to know more about how shape outside works and some of the stuff you can do with it of course I've covered this in a video in the past so if you'd want to know more about that you can check it out right here and with that I would really like to thank my neers of awesome who are web on demand Andrew Michael Simon Tim and Johnny as well as all my other patrons for their monthly support and of course until next time don't forget to make your corner of the internet just a little bit more awesome

Original Description

One of the most common resets in CSS is to set a max-width: 100% and a display: block on our images. In this video, I look at why it might also be a good idea to declare background-size, font-style, and shape-margin, among other things. 🔗 Links ✅ Harry Roberts post on Twitter: https://twitter.com/csswizardry/status/1717841334462005661 ✅ Harry's article on low-quality image placeholders: https://csswizardry.com/2023/09/the-ultimate-lqip-lcp-technique/ ✅ More on shape-margin and shape-outside: https://www.youtube.com/watch?v=sifXs4XVK7g ⌚ Timestamps 00:00 - Introduction 00:50 - max-width 01:50 - vertical-align: middle 03:50 - height: auto 04:20 - font-style: italic 05:35 - background-size and background-repeat 09:29 - shape-margin #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

This video teaches how to improve image resets in CSS and optimize image loading for better page performance. By applying these techniques, developers can enhance the user experience and improve website loading times. The video demonstrates the use of CSS properties such as max-width, display, and background-size, as well as Dev tools for testing and optimization.

Key Takeaways
  1. Comment out display: block and observe the effect
  2. Turn off display: block and observe the image's behavior
  3. Zoom in on the image to see the space underneath
  4. Turn off vertical-align: middle to see the effect
  5. Bring in a low-res image as a background image
  6. Simulate a slow 3G network
  7. Refresh the page to see the low-res image load first
  8. Preload the low-res image
  9. Define the width and height of the image
💡 Using a combination of CSS properties such as max-width, display, and background-size, along with Dev tools for testing and optimization, can significantly improve image loading and page performance.

Related Reads

Chapters (7)

Introduction
0:50 max-width
1:50 vertical-align: middle
3:50 height: auto
4:20 font-style: italic
5:35 background-size and background-repeat
9:29 shape-margin
Up next
Schedule Python Scripts in the Cloud for FREE
Thomas Janssen
Watch →