How To Add CSS3 Transition With HTML5 details/summary tag reveal?
HtmlCssCss TransitionsHtml Problem Overview
Using just CSS3, is there a way to add a nice fade-in and slide-from-left transition effect on a DETAILS
/SUMMARY
reveal?
For a demo of this new tag, see this demo:
details {
transition:height 3s ease-in;
}
<details>
<summary>Copyright 1999-2014.</summary>
<section>
<p> - by Refsnes Data. All Rights Reserved.</p>
<p>All content and graphics on this web site are the property of the company Refsnes Data.</p>
</section>
</details>
In my case, after the summary
tag, I put all other content in its own section
tag so that I could style it because summary:after
selector didn't seem to work. I tried using CSS3 transitions on height for the section
and details
tag, but it didn't help.
Html Solutions
Solution 1 - Html
This should fix it.
details[open] summary ~ * {
animation: sweep .5s ease-in-out;
}
@keyframes sweep {
0% {opacity: 0; margin-left: -10px}
100% {opacity: 1; margin-left: 0px}
}
<details>
<summary>Copyright 1999-2014.</summary>
<p> - by Refsnes Data. All Rights Reserved.</p>
<p>All content and graphics on this web site are the property of the company Refsnes Data.</p>
</details>
Some credit goes to Andrew for pointing this out. I adapted his answer. Here's how this works. By adding the [open]
attribute on the DETAILS
tag, it only fires the animation keyframe when clicked. Then, by adding SUMMARY ~ *
it means "all elements after the SUMMARY
tag" so that the animation applies to those, and not the SUMMARY
element as well.
Solution 2 - Html
In addition to Volomike's answer, it would be possible to change margin-left
to transform: translateX()
for performance reasons.
details[open] summary ~ * {
animation: sweep .5s ease-in-out;
}
@keyframes sweep {
0% {opacity: 0; transform: translateX(-10px)}
100% {opacity: 1; transform: translateX(0)}
}
<details>
<summary>Copyright 1999-2014.</summary>
<p> - by Refsnes Data. All Rights Reserved.</p>
<p>All content and graphics on this web site are the property of the company Refsnes Data.</p>
</details>
Solution 3 - Html
details
{
transition: height 1s ease;
overflow: hidden;
}
details:not([open])
{
height: 1.25em;
}
details[open]
{
height: 2.50em;
}
<details>
<summary>Example</summary>
Height needs to be set for this to work. Works on Chrome, haven't tested further.
</details>
I recommend you also check out Animate.css here: http://daneden.me/animate. If
Solution 4 - Html
My styling…
Using max-height
for transition instead of height
you can have unknown height (smaller than 99rem
) of content in opened details.
details { margin: 1.3rem 0; border-bottom: 1px solid #aaa; padding: 0.5rem; height: auto; max-height: 1.5rem; /* set to line height */ transition: all 0.1s ease; }
details[open] { max-height: 99rem; transition: all 1s ease; border: 1px solid #aaa; }
details summary { font-weight: bold; cursor: pointer; margin-bottom: 0.5em; font-weight: bold; margin: -0.5em -0.5em 0; padding: 0.5em; }
details[open] summary { border-bottom: 1px solid #aaa; margin-bottom: 0.8em; }
Something like… question?
And some very, very long explanation of summarised text. And some very, very long explanation of summarised text. And some very, very long explanation of summarised textSolution 5 - Html
You probably would want to use a CSS animation using the keyframes
if you don't plan on having it appear when you hover over it. If you only want the animation to appear, say, when you see details/summary description on the page, you could use some jQuery so that the browser adds the class of the animation when scrolling.
https://jsfiddle.net/22e95bxt/
Is this what you're looking for?
Edit: Whoops. This is NOT what you're asking for. My bad!