Customize kc-* css classes
· 3 min read
Summary of the Debugging Process, and Solutions about Customzing Template.tsx

In this case, the issue stemmed from the kc-header-wrapper class, which is part of Keycloak's original default theme (login.css). This class had predefined styles (e.g., padding, line-height, font-size, etc.) that were overriding the Tailwind CSS styles applied via inline classes in the React component. The kc- prefixed classes are part of Keycloak's default theme, and customizing them with Tailwind CSS can be challenging because Tailwind's utility classes are often overridden by these predefined styles.
Debugging Process:
-
Initial Observation:
- The
kc-header-wrapperwas exceeding the height of its parent (kc-header), causing layout issues. - The
h-fullTailwind class was not working as expected.
- The
-
Root Cause Identification:
- The
kc-header-wrapperclass from Keycloak'slogin.csswas overriding the Tailwind styles with its ownpadding,line-height, and other properties.
- The
-
Attempted Fixes:
- Initially, Tailwind CSS classes like
p-0,m-0, andh-fullwere used inline, but they were ineffective due to the higher specificity of thelogin.cssstyles. - The
!importantrule was considered to force override the styles.
- Initially, Tailwind CSS classes like
-
Final Solution:
- A separate CSS file (
template.css) was created to reset the problematic properties of#kc-header-wrapperusing!important. This ensured that the styles fromlogin.csswere overridden. - The
template.cssfile was imported into theTemplate.tsxcomponent to apply the reset styles.
- A separate CSS file (
Final Solution Code:
/* template.css */
#kc-header-wrapper {
padding: 0 !important;
margin: 0 !important;
line-height: normal !important;
font-size: inherit !important;
text-transform: none !important;
letter-spacing: normal !important;
white-space: normal !important;
}
{/* Header */}
<div id="kc-header" className={clsx(kcClsx("kcHeaderClass"), "bg-blue-700 border-b border-orange-700 h-[3.125rem] flex items-center px-4 fixed top-0 left-0 right-0 z-50 w-full")}>
<div id="kc-header-wrapper" className={clsx(kcClsx("kcHeaderWrapperClass"), "flex items-center h-full")}>
<img src={logoPngUrl} width={40} height={40} className="mr-2" />
{msg("loginTitleHtml", realm.displayNameHtml)}
</div>
</div>
Alternatives:
-
Inline Styles:
- As a temporary debugging measure, inline styles can be used to override the
login.cssstyles:<div id="kc-header-wrapper" style={{ padding: 0, margin: 0, lineHeight: "normal", fontSize: "inherit", textTransform: "none", letterSpacing: "normal", whiteSpace: "normal" }}><img src={logoPngUrl} width={40} height={40} className="mr-2" />{msg("loginTitleHtml", realm.displayNameHtml)}</div>
- As a temporary debugging measure, inline styles can be used to override the
-
Tailwind
!importantUtility:- Tailwind CSS supports the
!importantutility (e.g.,p-0!), but this may not always work due to specificity issues with Keycloak's default styles.
- Tailwind CSS supports the
-
Custom CSS File:
- The most robust solution is to use a separate CSS file to reset or override the
kc-prefixed classes, as shown in the final solution.
- The most robust solution is to use a separate CSS file to reset or override the