Skip to content

Instantly share code, notes, and snippets.

@bastianallgeier
Created December 2, 2011 10:57
Show Gist options
  • Save bastianallgeier/1422798 to your computer and use it in GitHub Desktop.
Save bastianallgeier/1422798 to your computer and use it in GitHub Desktop.
I'd love to have a :loaded pseudo selector in css, which makes it possible to style elements when they are actually loaded – like images, iframes or the entire document
img {
opacity: 0;
-webkit-transition: opacity .2s;
-moz-transition: opacity .2s;
-o-transition: opacity .2s;
transition: opacity .2s;
}
img:loaded {
opacity: 1;
}
@oelna
Copy link

oelna commented Jul 23, 2012

Great idea. But I think some browsers already do the fade-in you describe, once their images load. I believe I have seen this happen in Chrome.

@bastianallgeier
Copy link
Author

Fading them in was just one example. There are loads of cool things you could do with this IMHO.

@oelna
Copy link

oelna commented Jul 24, 2012

transform: scale immediately comes to mind. But I wonder if this could be implemented in a way that allows for modifying the parent's properties too. For now it's limited to only the tag itself.

@bastianallgeier
Copy link
Author

Coolest thing would be to have this for all dynamically loaded contents. But maybe it's just too fancy for css and should be done with js anyway.

@jvonmitchell
Copy link

body:loaded div#someId {
content: 'something heavy I loaded in after the important stuff'
}
Of course javascript can do that but there is a trend of trying to get css to cover more ground, and it's a perfectly valid trend.

@lthoerner
Copy link

lthoerner commented Nov 18, 2023

You can do this with keyframes, though I agree a dedicated pseudo selector would be nice.

@keyframes fade_in {
    from { opacity: 0; }
    to { opacity: 1; }
}

img {
    animation: fade_in 0.2s linear;
    opacity: 1;
}

(Yes, I am aware this is a decade-old post.)

@gerardreches
Copy link

I would love that to be possible as well. I have a carousel for whose elements I set a background so that something is visible while images, videos and iframes haven't loaded yet. I would like to remove that background after each element has loaded.

@lthoerner does the animation only trigger once the element has loaded?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment