Alpine supports both String and Object for Class Binding. We can choose either one for our preference.
Version
Alpine 3.9
One Utility
String
Alpine supports class binding for String.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/alpinejs" defer></script>
<script src="https://cdn.tailwindcss.com"></script>
<title>Alpine</title>
</head>
<body x-data="{ isActive: true }">
<div class="text-3xl" :class="isActive ? 'text-red-600' : ''">
TailwindCSS
</div>
</body>
</html>
Line 11
<div class="text-3xl" :class="isActive ? 'text-red-600' : ''">
TailwindCSS
</div>
text-3xl
: directly specify onclass
attribute. This utility is not changed by statetext-red-600
: determined byisActive
state. We can simply use class binding with String
Unlike another attribute binding in Alpine, class binding will not override original
text-3xl
, just append newtext-red-600
toclass
attribute
Object
Alpine also supports class binding for Object.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/alpinejs" defer></script>
<script src="https://cdn.tailwindcss.com"></script>
<title>Alpine</title>
</head>
<body x-data="{ isActive: true }">
<div class="text-3xl" :class="{ 'text-red-600': isActive }">
TailwindCSS
</div>
</body>
</html>
Line 11
<div class="text-3xl" :class="{ 'text-red-600': isActive }">
TailwindCSS
</div>
text-3xl
: directly specify onclass
attribute. This utility is not changed by statetext-red-600
: determined byisActive
state. We can also use class binding with Object. Key is utility, and value is boolean which is state
Multiple Utilities
String
Alpine supports class binding for String with multiple utilities.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/alpinejs" defer></script>
<script src="https://cdn.tailwindcss.com"></script>
<title>Alpine</title>
</head>
<body x-data="{ isActive: true }">
<div class="text-3xl" :class="isActive ? 'text-red-600 font-bold' : ''">
TailwindCSS
</div>
</body>
</html>
Line 11
<div class="text-3xl" :class="isActive ? 'text-red-600 font-bold' : ''">
TailwindCSS
</div>
text-3xl
: directly specify onclass
attribute. This utility is not changed by statetext-red-600 font-bold
: determined byisActive
state. We can simply use class binding with String returning multiple utilities
Object
Alpine also support class binding for Object with multiple utilities.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/alpinejs" defer></script>
<script src="https://cdn.tailwindcss.com"></script>
<title>Alpine</title>
</head>
<body x-data="{ isActive: true }">
<div
class="text-3xl"
:class="{ 'text-red-600': isActive, 'font-bold': isActive }"
>
TailwindCSS
</div>
</body>
</html>
Line 11
<div
class="text-3xl"
:class="{ 'text-red-600': isActive, 'font-bold': isActive }"
>
TailwindCSS
</div>
text-3xl
: directly specify onclass
attribute. This utility is not changed by statetext-red-600
: determined byisActive
state. We can also use class binding with Object. Multiple utilities are multiple keys, and values are booleans which are states
String Shorthand
?:
Since Alpine supports class binding for String, we often use ?:
in :class
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/alpinejs" defer></script>
<script src="https://cdn.tailwindcss.com"></script>
<title>Alpine</title>
</head>
<body x-data="{ isActive: true }">
<div class="text-3xl" :class="isActive ? 'text-red-600' : ''">
TailwindCSS
</div>
</body>
</html>
Line 11
<div class="text-3xl" :class="isActive ? 'text-red-600' : ''">
TailwindCSS
</div>
text-red-600
: determined byisActive
state. IfisActive
istrue
, returntext-red-600
to apply this utility. IfisActive
isfalse
, return empty String not to apply it
&&
We can simply ?:
to &&
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/alpinejs" defer></script>
<script src="https://cdn.tailwindcss.com"></script>
<title>Alpine</title>
</head>
<body x-data="{ isActive: true }">
<div class="text-3xl" :class="isActive && 'text-red-600'">TailwindCSS</div>
</body>
</html>
Line 11
<div class="text-3xl" :class="isActive && 'text-red-600'">TailwindCSS</div>
text-red-600
: determined byisActive
state. IfisActive
istrue
, returntext-red-600
to apply this utility. IfisActive
isfalse
, return empty String not to apply it- Instead of using
?:
, we can just use&&
to shorten the codebase
||
We can simply ?:
to ||
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/alpinejs" defer></script>
<script src="https://cdn.tailwindcss.com"></script>
<title>Alpine</title>
</head>
<body x-data="{ isActive: false }">
<div class="text-3xl" :class="isActive || 'text-red-600'">TailwindCSS</div>
</body>
</html>
Line 11
<div class="text-3xl" :class="isActive || 'text-red-600'">TailwindCSS</div>
text-red-600
: determined byisActive
state. IfisActive
istrue
, returntext-red-600
to apply this utility. IfisActive
isfalse
, return an empty String not to apply it- Instead of using
?:
, we can just use||
to shorten the codebase
Ternary Operator
String
Since Alpine supports class binding for String, we often use ?:
in :class
to select one of two utilities.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/alpinejs" defer></script>
<script src="https://cdn.tailwindcss.com"></script>
<title>Alpine</title>
</head>
<body x-data="{ isActive: true }">
<div class="text-3xl" :class="isActive ? 'text-blue-600' : 'text-red-600'">
TailwindCSS
</div>
</body>
</html>
Line 11
<div class="text-3xl" :class="isActive ? 'text-blue-600' : 'text-red-600'">
TailwindCSS
</div>
text-blue-600
ortext-red-600
: determined byisActive
state. IfisActive
istrue
, returntext-blue-600
to apply this utility. IfisActive
isfalse
, returntext-red-600
to apply it.
Object
Since Alpine supports class binding for Object, we can also use Object to implement ?:
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://unpkg.com/alpinejs" defer></script>
<script src="https://cdn.tailwindcss.com"></script>
<title>Alpine</title>
</head>
<body x-data="{ isActive: true }">
<div
class="text-3xl"
:class="{ 'text-blue-600': isActive, 'text-red-600': !isActive }"
>
TailwindCSS
</div>
</body>
</html>
Line 11
<div
class="text-3xl"
:class="{ 'text-blue-600': isActive, 'text-red-600': !isActive }"
>
TailwindCSS
</div>
text-blue-600
: enable ifisActive
state istrue
text-red-600
: enable ifisActive
state iffalse
Conclusion
- Although Alpine is inspired by Vue, as for Class Binding, there are subtle differences between Vue and Alpine
- Alpine doesn’t support class binding for Object Array, just use plain Array for multiple utilities