How to use CSS object-fit to control your images

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 พ.ย. 2024

ความคิดเห็น • 221

  • @Limonische
    @Limonische 6 ปีที่แล้ว +40

    Kevin, you can actually use it even in ie with the help of some tiny js libraries which mirror functionality of object-fit in ie. They are using some font-family hack to allow this and it works great.

    • @KevinPowell
      @KevinPowell  6 ปีที่แล้ว +7

      Thanks for mentioning that, I've pinned it :). A lot of the IE issues can be fixed with the help of some JS libraries :)

    • @TheHermitHacker
      @TheHermitHacker 6 ปีที่แล้ว +5

      @@KevinPowell I've always known these little helpers as "shims". Now that I build my own projects and don't build for clients anymore, i focus purely on modern browsers and don't bother with IE.

    • @eugenhildt910
      @eugenhildt910 6 ปีที่แล้ว +3

      I'm a freelancer and most of my recent clients don't bother to support Microsoft's IE/Edge in their projects. Microsoft never learns but who knows..

    • @Limonische
      @Limonische 6 ปีที่แล้ว +2

      @@eugenhildt910 I usually don't support IE either, but recently had to do a project for a company that really wanted to have exactly same experience in all browsers. And for that project, the js solution for object-fit was a real saver.

    • @mattwood8659
      @mattwood8659 5 ปีที่แล้ว

      I've never had any problems with edge. To me it seems to work just the same as all the other modern browsers. There are media queries that target edge spefically so if something doesn't work 100% you can still do a fall back option. I no longer support I.e versions below edge. I just put in a custom message that pops up saying the browser is outdated. However that's depending on the target audience of the project. If the anyltical data is still saying a high percentage of users are on I.e which is most government websites then yeah I. E support needs to be included. Which you can just do a different style sheet for and target it with conditional comments. It's not a huge deal.

  • @philipdufour7233
    @philipdufour7233 3 ปีที่แล้ว +51

    I literally spent a whole day trying to get my image grid to scale properly, then I found this video and fixed it in 30 seconds lol. Thanks, Kevin!

    • @lardosian
      @lardosian 3 ปีที่แล้ว +2

      Same here!!

  • @ricksegalCanada
    @ricksegalCanada 8 หลายเดือนก่อน +2

    Five years later and five minutes, presto several problems are now solved. Thank you very much for your YT content.

  • @pankam7800
    @pankam7800 5 ปีที่แล้ว +9

    Kevin Powell is the new "CSS GUY" ...of TH-cam..!!! Great effort ...great work..!!!

  • @kenji303
    @kenji303 2 ปีที่แล้ว

    I spent so long trying to find a tutorial on how to do this but nothing worked. This video was so helpful. Thanks Kevin .

  • @FunkyToe369
    @FunkyToe369 5 ปีที่แล้ว

    I had no idea this existed. This is super helpful. I was stumped trying to figure out how to use something like how we can configure background images with cover etc while using a picture element for srcset.

    • @KevinPowell
      @KevinPowell  5 ปีที่แล้ว +1

      Glad I could help. For something so useful, it's really a bit of a hidden gem as many people don't know about it.

  • @ga7853
    @ga7853 6 ปีที่แล้ว +3

    Thanks Kevin, keep bring those things that we never used before, so we can start using them. million thanks.

  • @Romeo-vt8hq
    @Romeo-vt8hq 3 ปีที่แล้ว

    This is way more convenient than to use background-image. Haven't heard about this one before but I'll definetely use it in my future projects from now on.

  • @Professor_Bugs
    @Professor_Bugs 4 ปีที่แล้ว +6

    Thank you so much for your courses on Scrimba! :-)
    They really got me into html and css and i would never have started on my
    front end developer path if it was not for you Kevin! :-)
    Being unemployed because of corona i have finally found something to fill my days with,
    and some day.... Who knows? Maybe i will get a job! :-)

  • @99Leachim99
    @99Leachim99 6 ปีที่แล้ว +2

    If you want use both image with and height relative to parent, just set parent to position: relative, image to position: absolute and set percentage (width: 100%; height: 100%; left: 0; top: 0). Works best with object-fill: cover :) You can also do pretty mixin in scss for that :)
    There is also pretty simple fallback for IE, just add class for your parent or image itself and than in JavaScript get each of them, insert element before image, get images url and put it as background, than hide the image. (10 lines of JS code for fallback, no jQuery plugin needed). You can also copy the dimensions.

    • @KevinPowell
      @KevinPowell  6 ปีที่แล้ว

      Thanks for sharing Michael :)

  • @justinoneill2837
    @justinoneill2837 6 ปีที่แล้ว +12

    really good stuff dude. your content has those nitty gritty golden nuggets that make me wish i knew about them sooner. keep it up!

  • @techxpertpcs
    @techxpertpcs 6 ปีที่แล้ว +1

    Nice tutorial kevin, i follow a lot of your videos and have learnt alot, but one thing i gotta say when i saw this video is that i love your choice of image used as i have the exact same one as my desk top background

  • @rl6382
    @rl6382 ปีที่แล้ว

    Kevin. I know you want to grow like other channels (it’s natural being a content creator and all lol) but you really help people with your service/position. Which is something most people can’t say. That’s cool man

  • @inner-astronaut
    @inner-astronaut 5 ปีที่แล้ว +2

    I have a PHP function of like 50 lines of code that do just this. haha Seriously. I have looked high and low for something like this for years. You should see the number of Stack Overflow posts addressing this problem, but never once mentioning this solution. Thanks for the content.

    • @KevinPowell
      @KevinPowell  5 ปีที่แล้ว

      Glad I could help :)

  • @lardosian
    @lardosian 3 ปีที่แล้ว

    Contain really helped me out sizing beer images from a beer API, thanks Kevin!!

  • @cagra8448
    @cagra8448 2 ปีที่แล้ว

    Thanks Kevin, you're truly a CSS god.

  • @kopilkaiser8991
    @kopilkaiser8991 3 หลายเดือนก่อน

    Love learning from you. It is always a pleasure 🎉🤩

  • @octavian3033
    @octavian3033 5 ปีที่แล้ว +1

    I got stuck when image-formatting and this is exactly what I was thinking about. Thanks a lot

  • @fire_fux
    @fire_fux 2 ปีที่แล้ว

    Was searching W3, FreeCodeCamp, and stack overflow for my banner/header img issue and was finding nothing to help me. Your video did, thank you so much.

  • @iamdann783
    @iamdann783 ปีที่แล้ว

    Still, KP is #1 in teaching class. Thanx bro.

  • @facu8888
    @facu8888 2 ปีที่แล้ว

    incredible help for managing images for lists or tables 🥰

  • @lalnuntluangachhakchhuak5767
    @lalnuntluangachhakchhuak5767 6 ปีที่แล้ว +12

    When the client provides grid images and has no time to crop them, this will save the day.

  • @TiemenBouma-q6h
    @TiemenBouma-q6h ปีที่แล้ว

    Thank you for the talk. I have used "none" for a image banner at the top of a website. that stays the same height but the width will crop when screen is smaller. disclaimer I am just starting so probably not optimal, but it works great ;)

  • @godfathermikal
    @godfathermikal 3 ปีที่แล้ว

    Was working with this the other day. Got a little frustrated. Thank you for the clear explanation. 😁

  • @misterbasti4649
    @misterbasti4649 6 ปีที่แล้ว +9

    Hi Kevin, I really like the new fiveminutefriday series! Keep up the good work

  • @tech6841
    @tech6841 4 ปีที่แล้ว

    Omg you literally save my life I was so tired trying to figure this out with my imgs thanks a lot. You're a Hero!

  • @CosmeValera
    @CosmeValera 2 ปีที่แล้ว

    object-fit: cover is what I was looking for, so cool :))

  • @juliannicholls
    @juliannicholls 8 หลายเดือนก่อน +1

    I came here to confirm what I discovered on Friday night, and within 90 seconds, you've done it.
    I couldn't get object-fit to work for the life of me because I assumed that it would be fitted based on the dimensions of the parent, but no, it has to be specified on the image itself.
    Actually, this may be the video that originally introduced me to object-fit in the first place.

  • @LukeSimshauser
    @LukeSimshauser 4 ปีที่แล้ว +1

    Awesome tip, thanks Kevin.
    Interested to see where you would use this over setting a bg image.. Im sure there would be a use!

  • @daveoconnor2009
    @daveoconnor2009 5 ปีที่แล้ว

    Thanks Kevin. This was the solution to an annoying issue I couldn't fix!

  • @sidarjunful
    @sidarjunful 3 ปีที่แล้ว

    Exactly what I wanted to know right now.

  • @Hudlle
    @Hudlle 2 ปีที่แล้ว

    This helps me so much. Thanks, Kevin!

  • @clover6480
    @clover6480 ปีที่แล้ว

    You safed my daaamn WEEK. I LOVE yo!

  • @applehead95
    @applehead95 2 ปีที่แล้ว

    Hello kind sir! You're truly the only person I find very helpful in these subjects. Thank you a lot! :DD

  • @ronaldjoytengco7000
    @ronaldjoytengco7000 3 ปีที่แล้ว

    This channel deserved a millions subs!

  • @Sollace
    @Sollace 4 ปีที่แล้ว

    Your videos are amazing, dude. I'm learning about css features I've never heard of, and ones that I'd wished for ever since I started WebDev.

  • @BlueTreeCode
    @BlueTreeCode 4 ปีที่แล้ว

    Cannot believe I just found your channel! Your content is amazing!

  • @sidheshwartiwari9834
    @sidheshwartiwari9834 3 ปีที่แล้ว

    This is exactly what I was looking for.

  • @datumcoder
    @datumcoder 3 ปีที่แล้ว

    Thank you Kevin your advice has helped me today. Keep up the great tips, videos and courses. Makes learning CSS that much easier!

  • @bdev5988
    @bdev5988 6 ปีที่แล้ว +2

    Man I really appreciate you content! You're super underrated!

    • @KevinPowell
      @KevinPowell  6 ปีที่แล้ว

      Thanks Bryce, glad that you're enjoying it!

  • @KileyOhl
    @KileyOhl 4 ปีที่แล้ว

    I've never heard of this. You blew my mind! THANKS!!

  • @iubob98
    @iubob98 4 ปีที่แล้ว

    wow super helpful. thank you for explaining all the different properties!

  • @nuggets5787
    @nuggets5787 2 ปีที่แล้ว

    Tears of joy. Thank you man🙏

  • @os23-n8g
    @os23-n8g 4 ปีที่แล้ว +1

    thanks a lot! very helpful, solved literally the problem I had with the images

  • @Dexter101x
    @Dexter101x 6 ปีที่แล้ว

    Strange, I only started using this element a few weeks ago. Thanks for explaining the different options

    • @KevinPowell
      @KevinPowell  6 ปีที่แล้ว +1

      It's one that really goes under the radar, but can be really useful.

  • @KyleMerl
    @KyleMerl 6 ปีที่แล้ว +1

    Awesome! Definitely see the usefulness of this property. Thanks for telling us about it!

    • @KevinPowell
      @KevinPowell  6 ปีที่แล้ว

      It's a handy one for sure :)

  • @eumm11
    @eumm11 4 ปีที่แล้ว

    thank you for your work, i'm always learning with you!

  • @braedondavies9592
    @braedondavies9592 5 ปีที่แล้ว

    Thanks for the tip on using the object-fit. I had no idea this existed!

    • @denuf23
      @denuf23 4 ปีที่แล้ว

      Is object fit a software or its own website?

  • @luis96xd
    @luis96xd 3 ปีที่แล้ว

    Wow, I'm so thankful for this video! It's just what I was looking for my images, Thanks 😄
    Excellent video! 👏

  • @parasarora5869
    @parasarora5869 6 ปีที่แล้ว +2

    week ago...i found this property and was mainly using cover...but sir you introduced me to different other ones...and also the object-position...i loved this video thank you so much..
    but...what happened to the intro...it was so cool☹️😱

    • @KevinPowell
      @KevinPowell  6 ปีที่แล้ว +1

      Oh man, hahaha, I didn't even realize! Oooops! I had to change the order of my layers, and it was late at night and... well that happened, lol. It'll be back next week, though it might be changing again in the not too distant future.

    • @parasarora5869
      @parasarora5869 6 ปีที่แล้ว

      @@KevinPowell ohhh...that's the reason...its alright still...the content was great!!!😀

  • @nikhilraut9674
    @nikhilraut9674 3 ปีที่แล้ว

    Your great man I learned lot about responsive nedd from you keep doing work like this 🔥

  • @PeyiOyelo
    @PeyiOyelo 4 ปีที่แล้ว

    Your videos are a godsend Kev!

  • @henryifeanyi6535
    @henryifeanyi6535 2 ปีที่แล้ว

    This was so helpful been looking for a solution 😱😱😱😱

  • @didkei
    @didkei 3 ปีที่แล้ว

    Awesome video! Any other "how come I didn't know this sooner" properties?

  • @kamrucci
    @kamrucci 6 ปีที่แล้ว +1

    I much appreciate your videos, Kevin :) Thank you for sharing!

  • @gschiavon
    @gschiavon 4 ปีที่แล้ว

    You always havre great and clear instructions, thank you so much and all the best!!

  • @aman12988
    @aman12988 ปีที่แล้ว +1

    how do I center a Video thats lying behind a .png-image..?? As I change my pixel-width of the Page it moves dramatically from its behind position

  • @soumyazyx
    @soumyazyx 4 ปีที่แล้ว

    Great video. You just made my day! Thanks Kevin.

  • @wakwawunited1588
    @wakwawunited1588 2 ปีที่แล้ว

    Maybe im late to know this. But thanks for ur experience explain to people for free

  • @yasirtahirkheli74
    @yasirtahirkheli74 5 ปีที่แล้ว

    very professionally explained....

  • @Harrisoo
    @Harrisoo 4 ปีที่แล้ว +1

    Brilliant, just saved me pulling my hair out over some bloody slideshow containers! Thanks!

  • @adarsh_ravikumar
    @adarsh_ravikumar 3 ปีที่แล้ว

    You littelraly saved my career!!

  • @NehaSingh-xo5wn
    @NehaSingh-xo5wn 6 ปีที่แล้ว

    I JUST LOVE THE Way you teah

  • @ernestogutierrez5684
    @ernestogutierrez5684 6 ปีที่แล้ว

    A true scholar mr. powell. nice ending.

  • @wp-stars
    @wp-stars 2 ปีที่แล้ว

    thanks for these amazing videos Kevin. Always very helpful 🙂

  • @reedZ.k
    @reedZ.k 3 ปีที่แล้ว

    the explanation was awesome man but your codepen screen was a bit too blurry so i couldnt type exactly what u had done there so it would have been nice if u could have put the codepen source link it would have been awesome !!

  • @ohidujjamanfahad1942
    @ohidujjamanfahad1942 5 ปีที่แล้ว

    keep it up your five minutes friday

    • @KevinPowell
      @KevinPowell  5 ปีที่แล้ว +1

      Haven't had time to do them for awhile now, but it's something I plan on bringing back one day!

  • @jamesrosemary2932
    @jamesrosemary2932 5 ปีที่แล้ว

    Thank you! I saved me to write a very complicated JS script to accomplish the same thing!.-

  • @leenardgalad5448
    @leenardgalad5448 2 ปีที่แล้ว

    Oh?! Finally can make rounded img card with border tnks

  • @miriamolivier8321
    @miriamolivier8321 3 ปีที่แล้ว

    This helped alot. Thank you very much.

  • @CAMELTOESURPRIZEE
    @CAMELTOESURPRIZEE 5 ปีที่แล้ว

    Dude, thanks so much for this! Really appreciate the videos....

  • @nightcoresammy2107
    @nightcoresammy2107 4 ปีที่แล้ว

    thanks so much this video really helped me so much! you're a life saver!

  • @akhilratnachettri2982
    @akhilratnachettri2982 4 ปีที่แล้ว

    DAMN! I didn't know that! That is one good thing 2020 brought to me.

  • @md.sumonbiswas2272
    @md.sumonbiswas2272 2 ปีที่แล้ว

    Thanks again kevin

  • @sigmiami
    @sigmiami 5 ปีที่แล้ว

    A better example would have been to fill in the div area you have in green with the image which is what most people would want

  • @thesaltyfrog7683
    @thesaltyfrog7683 5 ปีที่แล้ว

    it gets really relaxing on (00:50)

  • @Sammorris207
    @Sammorris207 3 ปีที่แล้ว

    Thanks for the video it really helped, and Kevin you said that you use object-fit sometimes not all the time, what do you use most of the time then?

  • @nirmalmattmusic3993
    @nirmalmattmusic3993 4 ปีที่แล้ว

    You know what this stuff is dope

  • @mattwood8659
    @mattwood8659 5 ปีที่แล้ว

    I guess it would work on my current project. I was just using overflow hidden and absolute positioning. However I wouldn't use the full image once with all the dynamic backend content. As for a folio with heaps of images. that would hinder optimisation.

  • @karlmalcolm3228
    @karlmalcolm3228 6 ปีที่แล้ว

    Succinct, informative, and valuable information - appreciated.

  • @TheGorillafoot
    @TheGorillafoot 6 ปีที่แล้ว

    Dope tip! Thanks Kevin

  • @zuhriddinsamariddinov650
    @zuhriddinsamariddinov650 3 ปีที่แล้ว

    In the CSS section of this video, the code is written starting from line 14. Do You Need Codes Up to Row 14?

  • @dylanwarrener5857
    @dylanwarrener5857 2 ปีที่แล้ว

    Is there an alternative way to object fit? As I am trying to use object fit with transitions and it just doesn't work

  • @RussDnB
    @RussDnB 4 ปีที่แล้ว

    Helped loads. Thank you.

  • @0the0ambient0
    @0the0ambient0 3 ปีที่แล้ว

    Another great video. Thanks!

  • @josemanuelberrios7390
    @josemanuelberrios7390 3 ปีที่แล้ว

    Thank you, Thank you, Thank you, Thank you, Thank you, Thank you

  • @zeebloxyt7728
    @zeebloxyt7728 4 ปีที่แล้ว +1

    what you used in writing codes? i only got notepad :C

  • @PerpetualEducation
    @PerpetualEducation 2 ปีที่แล้ว +1

    We find very few situations where we have the option to explicitly set the width and height of something - so, object-fit never seems to come to the rescue! Do you have any favorite use-cases?

  • @kendo4165
    @kendo4165 5 ปีที่แล้ว

    Thanks! This was very helpful!

  • @craigburton4447
    @craigburton4447 3 ปีที่แล้ว

    Having trouble getting object position to do ANYTHING, no matter what I try, the image remains firmly lined up along it's left edge, center has zero effect. Anyone have any advice?

  • @zuhriddinsamariddinov650
    @zuhriddinsamariddinov650 3 ปีที่แล้ว

    Your Videos Are Very Good

  • @SparrowValentine
    @SparrowValentine 4 ปีที่แล้ว

    the object-fit doesn't work for me, like it's grayed out as if it doesn't exist. Any help?

  • @_alexlazar_
    @_alexlazar_ 3 ปีที่แล้ว

    I've spent the past 30 minutes looking for a property that does to the image tag what "background-attachement: fixed" does to a background image. Does anybody know about that?

  • @jamess5542
    @jamess5542 3 ปีที่แล้ว

    Hey mate, I have one Cover image on a page using the default Wordpress theme and I want that Cover image to fill the screen. Could you please suggest how do I do that?

  • @kevinwines9507
    @kevinwines9507 2 ปีที่แล้ว

    You're a hero!

  • @shinigami2669
    @shinigami2669 4 ปีที่แล้ว

    so i put two images side by side, due to their different size i had to give height size 12 rem; but when i got to small screen or mobile view the height is 12 rem; and not looking attractive in compare to large screen
    please help

  • @guilbaudguitar8153
    @guilbaudguitar8153 3 ปีที่แล้ว

    thank you for this tutorial,it was really usefull

  • @ferenccziryek3642
    @ferenccziryek3642 3 ปีที่แล้ว

    Hi,my name is frank, I would like to ask, how can be made divs with different height to become all with the same hight?

  • @Dt-8398
    @Dt-8398 2 ปีที่แล้ว

    How do you get the image to fill the container (green box) ?

  • @giovannimichel5506
    @giovannimichel5506 3 ปีที่แล้ว

    thank you for the intel.