47

So I have used CSS transitions before but I have a unique case with this one. I am writing a custom plugin for creating modals. Essentially I create a div on the fly document.createElement('div') and append it to the body with a few classes. These classes define color and opacity. I would like to use strictly CSS to be able to fade in this div, but making the state change seems difficult b/c they require some user interaction.

Tried some advanced selectors hoping it would case a state change, tried media query hoping to change state...looking for any ideas and suggestions, I really want to keep this in CSS if possible

2
  • Not sure if i understand the question correct. Are you looking for a solution that doesn't require user interaction for the div to fade in? Commented Jul 26, 2012 at 7:01
  • Yes that is correct...I will programmatically create a div and want it to fade in is CSS Commented Jul 27, 2012 at 2:31

4 Answers 4

116

CSS Keyframes support is pretty good these days:

.fade-in {
	opacity: 1;
	animation-name: fadeInOpacity;
	animation-iteration-count: 1;
	animation-timing-function: ease-in;
	animation-duration: 2s;
}

@keyframes fadeInOpacity {
	0% {
		opacity: 0;
	}
	100% {
		opacity: 1;
	}
}
<h1 class="fade-in">Fade Me Down Scotty</h1>

Sign up to request clarification or add additional context in comments.

6 Comments

You still want to use JS to add this class if you are not programatically adding an element, because you probably want to wait for document.ready, which pure CSS will not do.
This doesn't answer the question which is asking how do this with CSS transition.
Cheers mate, I've been trying 5 different ways to get this to work with JS and CSS transitions, and this is the first that works as it supposed to, every time, on every page by chucking it into the body class. :)
@Zaqx The transition is CSS, the trigger is up to you.
Not to be pedantic but just noticed (again apparently) that you are using a CSS animation not a transition. You might be interested to learn the distinction: cssanimation.rocks/transition-vs-animation
|
16

I believe you could addClass to the element. But either way you'd have to use Jquery or reg JS

div {
  opacity:0;
  transition:opacity 1s linear;*
}
div.SomeClass {
  opacity:1;
}

Comments

16

OK, first of all I'm not sure how it works when you create a div using (document.createElement('div')), so I might be wrong now, but wouldn't it be possible to use the :target pseudo class selector for this?

If you look at the code below, you can se I've used a link to target the div, but in your case it might be possible to target #new from the script instead and that way make the div fade in without user interaction, or am I thinking wrong?

Here's the code for my example:

HTML

<a href="#new">Click</a> 
<div id="new">
    Fade in ... 
</div>

CSS

#new {
    width: 100px;
    height: 100px;
    border: 1px solid #000000;
    opacity: 0;    
}


#new:target {
    -webkit-transition: opacity 2.0s ease-in;
       -moz-transition: opacity 2.0s ease-in;
         -o-transition: opacity 2.0s ease-in;
                                  opacity: 1;
}

... and here's a jsFiddle

3 Comments

Appreciate the help, tried to manually fire this through various techniques and none seem to work. Ran across this article stackoverflow.com/questions/8101854/… and it appears they gave up as well =(
OK. I'll get back to you if I think of something else that might solve it.
For anyone coming across this, here is a good reference on transitions css-tricks.com/almanac/properties/t/transition
3

I always prefer to use mixins for small CSS classes like fade in / out incase you want to use them in more than one class.

@mixin fade-in {
    opacity: 1;
    animation-name: fadeInOpacity;
    animation-iteration-count: 1;
    animation-timing-function: ease-in;
    animation-duration: 2s;
}

@keyframes fadeInOpacity {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}

and if you don't want to use mixins, you can create a normal class .fade-in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.