How to Add an "X" Close Button to a UK-Modal Popup
The Uniform Kit (UK) modal system is robust, but it relies on specific HTML semantics and attributes to render its default close button correctly. When developers attempt to manually inject a standard Bootstrap-style <button class="close"> or duplicate the dialog container without proper initialization, the result is often an unstyled element that fails to dismiss the modal.
To achieve the native UK-Modal look and behavior, you must leverage the uk-modal-close attribute rather than relying on generic CSS classes alone. The standard approach involves placing a button inside the .uk-modal-header container and attaching the uk-close attribute to it. This tells the JavaScript engine to listen for click events and trigger the modal's close logic.
The Correct Placement
In your existing code, you have a .uk-modal-header div containing only an <h2>. To add the close button, insert a <button> element directly inside that header block. The HTML structure should look like this:
<div class="uk-modal">
<div class="uk-modal-dialog uk-modal-body" uk-modal>
<!-- Header with Close Button -->
<div class="uk-modal-header">
<h2><?php echo esc_attr_e( $options['popuptitle'] ); ?></h2>
<!-- The critical part: use the specific UK class and attribute -->
<button type="button" uk-close class="uk-modal-close-default"></button>
</div>
<!-- Content -->
<div class="uk-modal-content">
<?php echo stripslashes( $options['popuptext'] ); ?>
</div>
<!-- Footer -->
<div class="uk-modal-footer">
<button id="button1" type="button" class="uk-button uk-button-large uk-button-<?php echo esc_attr_e( $options['popupbutton1style'] ); ?><?php if( $options['popupbuttonoptions'] != 'button-1-redirect' ) echo ' uk-modal-close'; ?>">
<?php echo esc_attr_e( $options['popupbutton1text'] ); ?>
</button>
</div>
</div>
</div>
Why Your Previous Attempts Failed
Your first attempt used a Bootstrap-style button (class="close" with data-dismiss). UK-Modal does not natively recognize this class for styling or event handling. Without the specific .uk-modal-close-default (or -small, -large) class, the browser renders it as a generic button without the expected "X" shape or the subtle hover effects defined in the component's CSS. Furthermore, without the uk-close attribute, no JavaScript logic is attached to handle the dismissal.
Your second attempt involved duplicating the .uk-modal-dialog container entirely. This creates a structural conflict because UK-Modal expects a single root dialog element per modal instance. Nesting or doubling this container breaks the z-index stacking context and can prevent the backdrop from closing correctly. Additionally, simply adding uk-close to an empty button inside a duplicated div does not fix the layout if the parent structure isn't unique.
Handling the "X" Shape
By default, UK-Modal renders a simple icon using CSS pseudo-elements (::before and ::after) on the empty <button> tag. This is why you see an empty button in the code but an "X" in the browser. If you prefer to use a literal character (like × or Ã) inside the button, UK-Modal will stack it on top of the pseudo-element icons, resulting in a messy overlay.
If you want to replace the icon with text entirely, you must remove the default styling classes and apply custom CSS to hide the pseudo-elements. For example:
.my-custom-close {
/* This hides the default X icons */
&::before,
&::after {
display: none;
}
}
Then use a button with content: <button uk-close class="my-custom-close">×</button>. However, sticking to the default empty button is usually cleaner and ensures consistent spacing.
Integration with Your Footer Button
In your footer, you are conditionally adding the uk-modal-close class to your submit button. This is a valid pattern for closing the modal upon form submission. However, be aware of a specific behavior: if both the header close button and the footer button have uk-modal-close, clicking either will trigger the same JavaScript handler.
If you click the header "X", the modal closes immediately. If you click the submit button, it processes the action and then closes the modal. This is generally desired behavior, but ensure your backend logic (if any) runs before the close animation completes if you are redirecting users. The uk-modal-close attribute works seamlessly with both external clicks and internal buttons, so no additional JavaScript event listeners are required for basic functionality.
Final Check
Before deploying, verify that the .uk-modal-dialog has the uk-modal-body class (or simply ensure it's wrapped correctly). In newer versions of UK-Modal, the uk-modal-body class is sometimes implicit on the dialog, but explicitly adding it ensures the header and footer stack heights are calculated correctly. If you find the "X" is overlapping your title, check your CSS for potential z-index conflicts or margin resets, though this is rare with standard usage.