X close button only using css
Categories:
Crafting Elegant Close Buttons with Pure CSS

Learn how to create visually appealing and functional 'X' close buttons using only CSS, without relying on images or JavaScript. This guide covers various techniques for accessibility and styling.
Creating a simple 'X' close button for modals, alerts, or dismissible components is a common UI task. While images or icon fonts are often used, it's entirely possible and often preferable to achieve this purely with CSS. This approach reduces HTTP requests, offers greater flexibility in styling, and ensures scalability without pixelation. This article will guide you through several methods to construct a robust and accessible CSS-only close button.
The Basic 'X' Structure with Pseudo-elements
One of the most common and flexible ways to create an 'X' is by using a single HTML element and its ::before
and ::after
pseudo-elements. Each pseudo-element forms one stroke of the 'X', which can then be rotated and positioned to form the cross shape. This method keeps your HTML clean and semantic.
<button class="close-button" aria-label="Close"></button>
Minimal HTML for a CSS-only close button
.close-button {
position: relative;
width: 30px;
height: 30px;
border: none;
background: none;
cursor: pointer;
padding: 0;
}
.close-button::before,
.close-button::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 20px;
height: 2px;
background-color: #333;
transform-origin: center center;
}
.close-button::before {
transform: translate(-50%, -50%) rotate(45deg);
}
.close-button::after {
transform: translate(-50%, -50%) rotate(-45deg);
}
.close-button:hover::before,
.close-button:hover::after {
background-color: #007bff;
}
CSS for creating an 'X' using pseudo-elements
aria-label="Close"
on your close button for accessibility. Screen readers will announce 'Close' instead of just 'button', providing crucial context for users who cannot see the visual 'X'.Understanding the Pseudo-element Rotation
The core of the pseudo-element technique lies in the transform
property. Each pseudo-element is initially a horizontal bar. By setting top: 50%
and left: 50%
, and then using transform: translate(-50%, -50%)
, we center the element precisely. From this centered position, we apply rotate(45deg)
to one and rotate(-45deg)
to the other, forming the 'X'. The transform-origin: center center;
ensures the rotation happens around the element's own center.
flowchart TD A[Start with a button element] --> B{Add ::before and ::after pseudo-elements} B --> C[Set position: absolute, top: 50%, left: 50% for both] C --> D[Center with transform: translate(-50%, -50%)] D --> E[Rotate ::before by 45deg] D --> F[Rotate ::after by -45deg] E & F --> G[Result: An 'X' close button]
Flowchart illustrating the CSS pseudo-element 'X' creation process
Alternative: Using a Single Element with Box Shadow
While less common for a perfect 'X', you can also create cross-like shapes using a single element and clever box-shadow
tricks, or by manipulating borders. However, for a clean 'X', pseudo-elements are generally preferred due to their simplicity and directness in forming two distinct lines. Another approach involves using a single div
and rotating it, then using its ::before
and ::after
to create the two lines, but this is often over-complicating the pseudo-element method.
<i>
or <span>
solely for icons if you can achieve the same visual effect with pseudo-elements on a semantic button. This keeps your HTML cleaner and more meaningful.Enhancing Interactivity and Accessibility
Beyond the basic shape, consider adding hover effects and ensuring proper focus states for keyboard navigation. A smooth transition on hover can significantly improve user experience. For accessibility, ensure the button is focusable (which <button>
elements are by default) and that its purpose is clear to assistive technologies.
.close-button {
/* ... existing styles ... */
transition: transform 0.2s ease-in-out;
}
.close-button:hover {
transform: rotate(90deg); /* Optional: Spin on hover */
}
.close-button:focus {
outline: 2px solid #007bff; /* Visible focus indicator */
outline-offset: 2px;
}
Adding hover and focus styles for better UX and accessibility