Having a Little Fun With Custom Focus Styles

Every front-end developer has or will face a scenario where your boss, client or designer thinks the outline of the browser applied on the focus element does not match the UI and asks you to remove it. Or you might even want to remove it yourself.
So you did some research and found that this is strongly unrecommended because there is a reason for the existence of the focal contour: It provides visual feedback for keyboard navigation (using the Tab key) so that users who are unable to use the mouse or who have visual impairments know where they are on the screen.
But that doesn't mean you have to keep this outline. Instead of removing it, replace it with something else. This way, you can keep the interface accessible and get more flexibility about the appearance to better match your UI.
You can first remove the default browser outline by selecting the focus state of the element and applying outline: none . You can then select one of the following options to replace it:
Change background color
This is best for elements that can be filled, such as buttons. Select the focus state of the element and apply a contrasting background color to it. The higher the contrast, the better, because subtle changes may not be enough to be a strong visual cues, especially in cases of color blindness and low vision.
In the example below, the background color and border color both change; you can choose one or both of them.
Click or use the Tab key to focus to see how this state looks.
Change text color
If the element has any text, you can select the focus state and change its color. This also works for icons that use mask-image apps; you can select the icons that are descended from the focus element and change their background color, just like the example button below.
Again, contrast is key. You can also consider using underscores on text links and using them as part of changing state, as stated in the Web Content Accessibility Guide:
Color is not used as the only visual means to convey information, indicate actions, prompt responses, or distinguish visual elements. (Level A) Understanding Success Criteria 1.4.1
Application box shadow
box-shadow property does exactly the same job as the outline, but it is more powerful - you can now control its color, opacity, offset, blur radius, and expansion radius. If border-radius is specified, the box shadow also follows the same rounded corners.
You can be very creative with this technique (but seriously, don't do that):
This works with nearly any type of focusable elements such as toggle buttons, check boxes, radio buttons, and sliders.
Increase element size
As an alternative to color changes, you can also use subtle size modifications as focus feedback. In this example, we use transform: scale .
The key here is subtlety. Extreme size changes can lead to content rearrangement, not to mention a bad experience for those who prefer to reduce exercise.
Copy existing hover style
If the element already has a contrasting hover style, you can simply take that style and apply it to the focus state as well. This is a pretty elegant solution because you don't need to add any new colors or outlines to the interface.
Here is an example where both the focus state and the hover state adopt a style that forms a high contrast with the background of the element's default style:
Attached: Custom default outline
Everything we've seen so far assumes we want to remove the focus profile completely. We don't have to do this! In fact, it is a border that we can customize.
button:focus {
outline: 3px dashed orange;
}
If we want to fine-tune the style, this can be written this way:
button:focus {
outline-width: 3px;
outline-style: dashed;
outline-color: orange;
}
Another superpower we have is the outline-offset property, which is separate from outline shorthand property, but can be used with it to change the position of the focus ring:
button:focus {
outline: 3px dashed orange;
outline-offset: 10px;
}
in conclusion
You can mix and match all of these options to get a custom style that fits every component type in the interface.
It is worth reiterating: when adopting a custom focus state, don't forget to use bright color contrast and other visual cues in addition to colors. Of course, we all want an experience that matches our design, but we can adhere to good accessibility practices along the way. W3C recommends using this tool to test the contrast of color values relative to the WCAG guide.
The above is the detailed content of Having a Little Fun With Custom Focus Styles. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
How to use vw and vh units in CSS
Aug 07, 2025 pm 11:44 PM
vw and vh units achieve responsive design by associating element sizes with viewport width and height; 1vw is equal to 1% of viewport width, and 1vh is equal to 1% of viewport height; commonly used in full screen area, responsive fonts and elastic spacing; 1. Use 100vh or better 100dvh in the full screen area to avoid the influence of the mobile browser address bar; 2. Responsive fonts can be limited with 5vw and combined with clamp (1.5rem, 3vw, 3rem) to limit the minimum and maximum size; 3. Elastic spacing such as width:80vw, margin:5vhauto, padding:2vh3vw, can make the layout adaptable; pay attention to mobile device compatibility, accessibility and fixed width content conflicts, and it is recommended to give priority to using dvh first;
What is the CSS aspect-ratio property and how to use it?
Aug 04, 2025 pm 04:38 PM
Theaspect-ratioCSSpropertydefinesthewidth-to-heightratioofanelement,ensuringconsistentproportionsinresponsivedesigns.1.Itisapplieddirectlytoelementslikeimages,videos,orcontainersusingsyntaxsuchasaspect-ratio:16/9.2.Commonusecasesincludemaintainingres
How to use the CSS :empty pseudo-class?
Aug 05, 2025 am 09:48 AM
The:emptypseudo-classselectselementswithnochildrenorcontent,includingspacesorcomments,soonlytrulyemptyelementslikematchit;1.Itcanhideemptycontainersbyusing:empty{display:none;}tocleanuplayouts;2.Itallowsaddingplaceholderstylingvia::beforeor::after,wh
How to create a vertical line with CSS
Aug 11, 2025 pm 12:49 PM
Use a div with border to quickly create vertical lines, and define styles and heights by setting border-left and height; 2. Use ::before or ::after pseudo-elements to add vertical lines without additional HTML tags, suitable for decorative separation; 3. In Flexbox layout, by setting the width and background color of the divider class, adaptive vertical dividers between elastic containers can be achieved; 4. In CSSGrid, insert vertical lines as independent columns (such as autowidth columns) into grid layout, which is suitable for responsive design; the most appropriate method should be selected according to the specific layout needs to ensure that the structure is simple and easy to maintain.
What are CSS pseudo-classes and how to use them?
Aug 06, 2025 pm 01:06 PM
CSS pseudo-class is a keyword used to define the special state of an element. It can dynamically apply styles based on user interaction or document location; 1.:hover is triggered when the mouse is hovered, such as button:hover changes the button color; 2.:focus takes effect when the element gets focus, improving form accessibility; 3.:nth-child() selects elements by position, supporting odd, even or formulas such as 2n 1; 4.:first-child and :last-child select the first and last child elements respectively; 5.:not() excludes elements that match the specified conditions; 6.:visited and:link set styles based on the link access status, but:visited is restricted by privacy.
How to use the filter property in CSS
Aug 11, 2025 pm 05:29 PM
TheCSSfilterpropertyallowsvisualeffectslikeblur,brightness,andgrayscaletobeapplieddirectlytoHTMLelements.1)Usethesyntaxfilter:filter-function(value)toapplyeffects.2)Combinemultiplefilterswithspaceseparation,e.g.,blur(2px)brightness(70%).3)Commonfunct
How to create a dotted border in CSS
Aug 15, 2025 am 04:56 AM
Use CSS to create dotted borders, just set the border attribute to dotted. For example, "border:3pxdotted#000" can add a 3-pixel-wide black dot border to the element. By adjusting the border-width, the size of the point can be changed. The wider borders produce larger points. You can set dotted borders for a certain side, such as "border-top:2pxdottedred". Dotted borders are suitable for block-level elements such as div and input. They are often used in focus states or editable areas to improve accessibility. Pay attention to color contrast. At the same time, different from dashed's short-line style, dotted presents a circular dot shape. This feature is widely used in all mainstream browsers.
How to create a glassmorphism effect with CSS
Aug 22, 2025 am 07:54 AM
To create a glass mimicry effect of CSS, you need to use backdrop-filter to achieve background blur, set a translucent background such as rgba(255,255,255,0.1), add subtle borders and shadows to enhance the sense of hierarchy, and ensure that there is enough visual content behind the elements; 1. Use backdrop-filter:blur(10px) to blur the background content; 2. Use rgba or hsla to define the transparent background to control the degree of transparency; 3. Add 1pxsolidrgba(255,255,255,0.3) borders and box-shadow to enhance the three-dimensionality; 4. Ensure that the container has rich backgrounds such as pictures or textures to present a blurred penetration effect; 5. It is compatible with old browsers


