<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
name
=
"viewport"
content="
width
=
device
-width,
initial-scale
=
1
.0">
<
title
>
Tailwind CSS Conditional
Styling Example
</
title
>
<
link
href
=
rel
=
"stylesheet"
>
</
head
>
<
body
class="bg-gray-100 min-h-screen
flex items-center justify-center">
<
div
class
=
"text-center"
>
<
h1
class="text-4xl font-bold mb-4 text-gray-700
transition duration-500 transform
motion-reduce:transform-none">
GeeksforGeeks
</
h1
>
<
p
class="text-lg text-gray-600 mb-4 transition
duration-500 transform
motion-reduce:transform-none">
Example for Tailwind CSS
Conditional Styling
</
p
>
<
button
class="px-4 py-2 rounded text-white font-semibold
transition duration-300 ease-in-out
focus:outline-none bg-blue-500 hover:bg-blue-600
active:bg-blue-700 focus:bg-blue-600"
id
=
"toggleButton"
>
Click Me
</
button
>
</
div
>
<
script
>
const toggleButton =
document.getElementById('toggleButton');
const geeksText =
document.querySelector('h1');
const exampleText =
document.querySelector('p');
let isToggled = false;
toggleButton.addEventListener('click', () => {
isToggled = !isToggled;
updateStyles();
});
function updateStyles() {
if (isToggled) {
toggleButton.classList.remove('bg-blue-500',
'hover:bg-blue-600',
'active:bg-blue-700',
'focus:bg-blue-600');
toggleButton.classList.add('bg-green-400',
'hover:bg-green-500',
'active:bg-green-600',
'focus:bg-green-500');
document.body.classList.add('bg-gray-200');
geeksText.classList.add(
'text-green-600', 'animate-bounce');
exampleText.classList.add(
'text-purple-600', 'animate-pulse');
} else {
toggleButton.classList.remove('bg-green-400',
'hover:bg-green-500',
'active:bg-green-600',
'focus:bg-green-500');
toggleButton.classList.add('bg-blue-500',
'hover:bg-blue-600',
'active:bg-blue-700',
'focus:bg-blue-600');
document.body.classList.remove('bg-gray-200');
geeksText.classList.remove(
'text-green-600', 'animate-bounce');
exampleText.classList.remove(
'text-purple-600', 'animate-pulse');
}
}
</
script
>
</
body
>
</
html
>