diff --git a/samples/TagHelperPack.Sample/Pages/Index.cshtml b/samples/TagHelperPack.Sample/Pages/Index.cshtml
index fee5589..d1d7841 100644
--- a/samples/TagHelperPack.Sample/Pages/Index.cshtml
+++ b/samples/TagHelperPack.Sample/Pages/Index.cshtml
@@ -354,6 +354,23 @@ public Customer Customer { get; set; }
<div asp-if="(DateTime.UtcNow.Second % 2) == 1">This paragraph will only render during <strong>odd</strong> seconds.</div>
+
Class-If Tag Helper
+
+ Use <any-element asp-class-if-{className}="..."> to conditionally add class name when the provided expression is true.
+
+
Example
+
+
+
This paragraph will have bg-primary class during even seconds.
+
This paragraph will have bg-primary class during odd seconds.
+
+
+
Source
+
+
<div asp-class-if-bg-primary="(DateTime.UtcNow.Second % 2) == 0">This paragraph will have <code>bg-primary</code> class during <strong>even</strong> seconds.</div>
+<div asp-class-if-bg-primary="(DateTime.UtcNow.Second % 2) == 1">This paragraph will have <code>bg-primary</code> class during <strong>even</strong> seconds.</div>
+
+
If combined with RenderPartial
Remember, you can compose certain Tag Helpers together on the same element, assuming they all apply to the specific element name.
diff --git a/src/TagHelperPack/ClassIfTagHelper.cs b/src/TagHelperPack/ClassIfTagHelper.cs
new file mode 100644
index 0000000..de3c911
--- /dev/null
+++ b/src/TagHelperPack/ClassIfTagHelper.cs
@@ -0,0 +1,76 @@
+using Microsoft.AspNetCore.Razor.TagHelpers;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace TagHelperPack;
+
+///
+/// Add/Remove a class name based on a condition.
+/// Supports different variations:
+/// asp-class-if-my-class,
+/// asp-class-if-class,
+/// asp-class-if-my_class,
+/// asp-class-if-MyClass,
+/// asp-class-if-myClass,
+/// asp-class-if-_my-class,
+/// asp-class-if-__myclass
+///
+[HtmlTargetElement("*", Attributes = "asp-class-if*")]
+public class ClassIfTagHelper : TagHelper
+{
+ private const string Space = " ";
+
+ ///
+ /// Add a class name based on a condition
+ ///
+ [HtmlAttributeName(DictionaryAttributePrefix = "asp-class-if-")]
+ public IDictionary ClassIfAttributes { get; set; } = new Dictionary();
+
+ ///
+ public override void Process(TagHelperContext context, TagHelperOutput output)
+ {
+ var existingClassNames = GetExistingClassNames(context);
+
+ var addClassNames = ClassIfAttributes
+ .Where(c => c.Value)
+ .Select(c => GenerateClassName(c.Key))
+ .ToList();
+
+ var removeClassNames = ClassIfAttributes
+ .Where(c => !c.Value)
+ .Select(c => GenerateClassName(c.Key))
+ .ToList();
+
+ // remove where false
+ var endResultClassNames = existingClassNames
+ .Union(addClassNames)
+ .Where(c => !removeClassNames.Contains(c));
+
+ var endResultClassNameString = string.Join(Space, endResultClassNames);
+
+ output.Attributes.SetAttribute("class", endResultClassNameString);
+ }
+
+ private static HashSet GetExistingClassNames(TagHelperContext context)
+ {
+ var existingClassAttribute = context.AllAttributes["class"];
+ if (existingClassAttribute?.Value != null)
+ {
+ var existingClasses = existingClassAttribute.Value.ToString()!
+ .Split(Space.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
+ .ToList();
+
+ return new HashSet(existingClasses);
+ }
+
+ return new HashSet();
+ }
+
+ private static string GenerateClassName(string className)
+ {
+ // Handle className transformation here
+ // for something that we hadn't considered
+ return className;
+ }
+}
diff --git a/tests/UnitTests/ClassIfTagHelperTests.cs b/tests/UnitTests/ClassIfTagHelperTests.cs
new file mode 100644
index 0000000..d6ea49b
--- /dev/null
+++ b/tests/UnitTests/ClassIfTagHelperTests.cs
@@ -0,0 +1,118 @@
+using Microsoft.AspNetCore.Razor.TagHelpers;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using TagHelperPack;
+using Xunit;
+
+namespace UnitTests;
+
+public class ClassIfTagHelperTests
+{
+ private readonly TagHelperAttributeList _attributeList;
+ private readonly TagHelperContext _context;
+ private readonly TagHelperOutput _output;
+ private readonly ClassIfTagHelper _tagHelper;
+
+ public ClassIfTagHelperTests()
+ {
+ _attributeList = new TagHelperAttributeList();
+
+ _context = new TagHelperContext("div", _attributeList, new Dictionary