I want to make a website more accessible by considering user with high contrast mode (or maybe just dark mode). Now I have a problem with custom radio buttons which are just HTML elements that have a round border: when the user has set the default background color of his browser to black, the background-color of my elements is also black, making it look like a checked option, even though I set the background-color of this elements to white.
To prevent this I made the white border thicker, so it looks like a white background. But it also creates the problem, that sometimes there is a tiny black dot in the middle of my buttons at some zoom-levels or resolutions.
Is there any way to prevent the browser from overwriting the background-color I declared in my CSS? Is there anything I missed out on like an attribute that prevents this behaviour?
I could also replace this elements or implement them in a different way, but at this point i am just curious if there isn't any simple solution to that.
The style of my radio buttons looks like this:
input.check + label:before {
display: inline-block;
content: "";
width: 1rem;
height: 1rem;
background: #fefefe;
border: 0.2rem solid #fefefe;
border-radius: 50%;
position: absolute;
left: -1rem;
}
input.check:checked + label:before {
background: #000000;
}
Here is a link to a picture of my radio when checked / or default black background-color
I make the 'checked' look of my radio button by setting the background-color to black - like the browser does in this case.
Edit
So far there are good answers for improving the accessability of custom radio buttons. To be a little bit more specific on my problem:
I tested the page in Firefox with the following color settings
But this seems to overwrite every background-color no matter what you specified for each element. Is there a way to make an exception for this overwriting?
Using a pseudo element - which is already mooted in the question - it is possible to style the button exactly as you would like.
The main difference in this snippet and the CSS in the question is that we set the actual input element to opacity 0. This means it is still 'there' for accessibility purposes but it can't be actually seen.
Also, using radial-gradient for coloring the buttons we can have rings of different colors, or just one color as best suits the background chosen. As the code in the question does, we also size everything in terms of rems so that the user who has changed their browser setting for this gets the benefit.