What is ::webkit-search-cancel-button?
The ::webkit-search-cancel-button is a vendor-prefixed CSS pseudo-element that targets the cancel button within <input type="search"> elements. This button, typically displaying an "X" or cross icon, appears automatically when users type text into a search field and disappears when the field is empty.
The HTML Search Input Context
Modern HTML5 introduced the type="search" input, which provides a semantic way to create search fields. WebKit browsers (Chrome, Safari, Edge, Opera) add platform-native UI elements to these inputs, including the cancel button. This differs from standard text inputs, which lack any built-in clearing mechanism.
How It Works
When a user enters text into a search input, WebKit automatically displays the cancel button on the right side of the input. Clicking this button clears the input value and removes the button from view. The pseudo-element allows developers to style this native control or remove it entirely.
According to UDN Web Docs' pseudo-element documentation, this is a non-standard pseudo-element specific to WebKit and Blink browsers. The CSS-Tricks guide to WebKit HTML5 search inputs provides additional context on how these browser-specific pseudo-elements interact with search input styling.
For a broader understanding of CSS pseudo-elements and their capabilities, explore our guide on CSS pseudo-elements as part of our comprehensive web development services.
| Browser | Version | Support |
|---|---|---|
| Chrome | 1+ | Full Support |
| Safari | 3+ | Full Support |
| Edge | 79+ | Full Support |
| Opera | 15+ | Full Support |
| Firefox | N/A | No Support |
| Internet Explorer | N/A | No Support |
Removing Default Styling
The -webkit-appearance Property
Before applying custom styles to the cancel button, you must first remove its default appearance using -webkit-appearance: none. This is a critical first step because WebKit applies its own styling that overrides most standard CSS properties. As documented by CSS-Tricks on appearance property usage, WebKit search inputs have protected properties that cannot be easily overridden without this declaration.
/* Remove default appearance first */
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: none;
}
Without this step, your custom styles will be ignored, and the browser's default cancel button will remain visible. Stack Overflow solutions for removing the button confirm this is the essential first step.
Understanding the Appearance Cascade
WebKit's search inputs have multiple layers of styling restrictions. Even after removing the cancel button's appearance, the parent search input itself has protected properties that cannot be overridden. This is by design to maintain consistency with each operating system's native search fields, as noted in the CSS-Tricks WebKit styling limitations.
For related CSS techniques, see our guides on CSS display property and custom highlight API for advanced styling approaches.
Styling Techniques
Changing Size and Dimensions
After removing the default appearance, you can control the button's dimensions. The pseudo-element behaves like an inline-block element, accepting width, height, and other box model properties as shown in Stack Overflow size customization examples:
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: none;
width: 20px;
height: 20px;
}
Custom Background and Color
Replace the default "X" with a custom background image or color:
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: none;
background-color: #ff4444;
border-radius: 50%;
}
Using CSS gradients or images provides more design flexibility:
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: none;
background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23333"><path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/></svg>') no-repeat center;
background-size: 16px;
}
Position and Margin Adjustments
Fine-tune the button's position within the search input. The pseudo-element is positioned absolutely within the search input's padding area, allowing margin adjustments to shift it horizontally as explained in Stack Overflow positioning solutions:
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: none;
margin-right: 8px;
position: relative;
right: 4px;
}
Hover and Active States
Add interactivity with pseudo-classes. Note that hover states can be tricky because the button only appears when text is entered, affecting the input's hover state detection as noted in CSS-Tricks state limitations:
input[type="search"]::-webkit-search-cancel-button:hover {
background-color: #cc0000;
cursor: pointer;
}
input[type="search"]::-webkit-search-cancel-button:active {
transform: scale(0.95);
}
For advanced CSS animations and transformations, explore our guide on reset transform and custom easing in CSS.
Hiding the Cancel Button Completely
When to Hide
Sometimes you need complete control over the search interface and prefer to implement your own clear button. This is common in custom design systems or when the default X conflicts with your UI, as discussed in Stack Overflow hiding use cases.
CSS Hiding Technique
The most reliable method to hide the cancel button:
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: none;
display: none;
}
This removes the button entirely while keeping the search input functional, as verified by Stack Overflow hiding approaches.
Alternative: Text Input Override
For maximum control, override the entire search input to behave like a text field. This removes all WebKit search decorations including the cancel button, results button, and decoration elements as described in the CSS-Tricks appearance override guide:
input[type="search"] {
-webkit-appearance: textfield;
}
When implementing custom search interfaces, consider our front-end development services for professional implementation of form controls and interactive elements.
1/* Custom SVG-based cancel button */2input[type="search"]::-webkit-search-cancel-button {3 -webkit-appearance: none;4 width: 24px;5 height: 24px;6 background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23666'%3E%3Cpath d='M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z'/%3E%3C/svg%3E");7 background-repeat: no-repeat;8 background-position: center;9 cursor: pointer;10 transition: filter 0.2s;11}12 13input[type="search"]::-webkit-search-cancel-button:hover {14 filter: invert(0.2);15}Related WebKit Search Pseudo-Elements
Complete List
WebKit provides several pseudo-elements for comprehensive search input customization:
::-webkit-search-decoration- The magnifying glass icon (if shown)::-webkit-search-cancel-button- The clear/remove button (X)::-webkit-search-results-button- The results dropdown button::-webkit-search-results-decoration- The results decoration
Hiding All WebKit Decorations
input[type="search"]::-webkit-search-decoration,
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-results-button,
input[type="search"]::-webkit-search-results-decoration {
display: none;
}
This removes all native WebKit search decorations for a completely custom implementation as demonstrated in Stack Overflow's complete hiding technique.
Selective Styling
Target specific elements while keeping others:
/* Keep decoration but style cancel button */
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: none;
/* Custom styles */
}
/* Hide results button */
input[type="search"]::-webkit-search-results-button {
display: none;
}
For more advanced CSS pseudo-element techniques, see our guide on the custom highlight API and related resources in our web development documentation.
Performance Considerations
Rendering Impact
The cancel button is rendered by the browser's native UI layer, which is generally more performant than JavaScript-based solutions. The pseudo-element uses minimal resources and doesn't require additional DOM elements, as noted in CSS-Tricks WebKit rendering analysis.
Animation Performance
When animating the cancel button, use CSS transforms for optimal performance:
input[type="search"]::-webkit-search-cancel-button {
transition: transform 0.15s ease;
}
Memory Efficiency
Using data URIs for custom images reduces HTTP requests but increases CSS file size. For complex icons, consider using an external SVG sprite sheet for better caching as suggested in Stack Overflow optimization discussions.
Cross-Browser Alternatives
Firefox doesn't support the WebKit pseudo-elements. For cross-browser search clear functionality, implement a JavaScript solution alongside the CSS approach:
<div class="search-wrapper">
<input type="search" id="search-input" placeholder="Search...">
<button type="button" id="clear-btn" aria-label="Clear search">
<svg>...</svg>
</button>
</div>
This progressive enhancement approach, where non-WebKit browsers gracefully fall back to JavaScript solutions, ensures all users can clear their search queries effectively.
For implementing robust form handling across all browsers, explore our front-end development services and web development expertise.
Always Remove Default Appearance First
Never skip the `-webkit-appearance: none` declaration. Without it, your custom styles will be ignored.
Provide Clear Visual Feedback
Ensure users understand the cancel button's purpose through visual cues like cursor changes and hover effects.
Test Across WebKit Browsers
Safari, Chrome, and Edge may render pseudo-elements slightly differently. Test on all target platforms.
Consider Accessibility
When hiding or replacing the cancel button, maintain keyboard accessibility for all users.
Use Relative Units
Prefer relative units (em, rem) over fixed pixels to maintain proportional sizing across contexts.
Troubleshooting Common Issues
Styles aren't applying - what should I do?
Ensure `-webkit-appearance: none` is applied before other styles, and verify the selector specificity. WebKit overrides custom properties without this declaration.
Button isn't visible after styling
Check if you've set `display: none` anywhere, and verify the input contains text (the button only shows when text is present).
Positioning is off - how do I fix this?
Use margin and position properties on the pseudo-element itself, not on the parent input. The cancel button is positioned absolutely within the input.
Hover effects don't work
Hover states can be tricky because the button only appears when text is entered. Consider using focus states on the parent input as an alternative.
Conclusion
The ::webkit-search-cancel-button pseudo-element provides a powerful way to customize the search field's clear button in WebKit browsers. By understanding its limitations, browser compatibility, and styling capabilities, you can create search inputs that maintain usability while matching your design system.
Key takeaways for implementing search input styling in your projects:
- Always start with
-webkit-appearance: none- This is the foundation for any custom styling - Consider performance implications - Native browser rendering is more efficient than JavaScript alternatives
- Provide fallbacks for non-WebKit browsers - Implement JavaScript solutions for Firefox and other browsers
- Test thoroughly across all target platforms - Safari, Chrome, and Edge may render pseudo-elements differently
Whether you're building a simple contact form with a search field or a complex e-commerce platform, these techniques ensure your search inputs work consistently across browsers while maintaining your brand's visual identity.
For projects requiring comprehensive form development, our front-end development services can help implement these and other CSS techniques for polished user interfaces.
Need help with your web development projects? Our web development team has expertise in creating performant, accessible web interfaces that work across all browsers and devices.
Sources
- CSS-Tricks: WebKit HTML5 Search Inputs - Comprehensive guide covering search input styling, including the cancel button pseudo-element
- UDN Web Docs: ::-webkit-search-cancel-button - Official documentation for this non-standard pseudo-element
- Stack Overflow: Editing input type="search" Pseudo-element Button - Practical solutions with 212K+ views