I need to do this in tailwind CSS for my react application.
How can I do that?
HTML
JavaScript
x
9
1
<h1>Show / Hide Animation with pure CSS</h1>
2
3
<label class="trigger">
4
<input type="checkbox" class="checkbox checkbox--red" /> Show additional information
5
<span class="msg">
6
Hey there, I'm fading in/out with pure CSS. How cool is that?!
7
</span>
8
</label>
9
CSS
JavaScript
1
35
35
1
/**
2
* Notice: Checkbox is styled via import of my other pen (https://codepen.io/fxm90/pen/JdmaeL)
3
*/
4
5
.trigger {
6
input[type="checkbox"] {
7
8
// Hide content via default
9
& + span {
10
visibility: hidden;
11
opacity: 0;
12
13
transition: visibility 0s linear 0.33s, opacity 0.33s linear;
14
}
15
16
// Show if checkbox is clicked
17
&:checked + span {
18
visibility: visible;
19
opacity: 1;
20
21
transition-delay: 0s;
22
}
23
}
24
}
25
26
// Simple styling for message.
27
.msg {
28
display: block;
29
margin-top: 8px;
30
padding: 8px 12px;
31
32
border: 1px solid #ddd;
33
border-radius: 3px;
34
}
35
This is the same thing in the above code pen link.
Advertisement
Answer
JavaScript
1
46
46
1
**Edit as you need. I gave the basic example here. If you not find your solution, let me a reply**
2
import React, { useState } from "react";
3
4
const YourComponent = () => {
5
const [checked, setChecked] = useState(false);
6
const selectStyle = (e) => {
7
const checked = e.target.checked;
8
if (checked) {
9
setChecked(true);
10
} else {
11
setChecked(false);
12
}
13
};`enter code here`
14
return (
15
<>
16
<div className="px-20 my-10">
17
<h1>Show / Hide Animation with pure CSS</h1>
18
19
<label class="trigger">
20
<input
21
type="checkbox"
22
class="checkbox checkbox--red"
23
onClick={(e) => {
24
selectStyle(e);
25
}}
26
/>{" "}
27
Show additional information
28
<div
29
className={
30
checked
31
? "opacity-100 transition-opacity duration-1000 ease-out"
32
: "opacity-0 transition-opacity duration-1000 ease-out"
33
}
34
>
35
<span class="block mt-2 py-2 px-3 border border[#ddd] rounded ">
36
Hey there, I'm fading in/out with pure CSS. How cool is that?!
37
</span>
38
</div>
39
</label>
40
</div>
41
</>
42
);
43
};
44
45
export default YourComponent;
46