I will outline two solutions that are relevant to MUI version 5:
- Using CSS only: This solution allows you to change the color of the border and circle independently.
- Custom icon solution: This method involves using a custom icon to customize the design as desired.
CSS-only Solution
If your goal is to solely alter the border color without affecting its thickness, particularly in MUI 5, you can achieve this through the sx
prop by following these steps:
- To style the border, target the selector
'& .MuiSvgIcon-root:not(.MuiSvgIcon-root ~ .MuiSvgIcon-root)'
, utilizing the CSS general sibling combinator.
- To style the circle, utilize the selector
'& .MuiSvgIcon-root + .MuiSvgIcon-root'
, which uses the CSS adjacent sibling combinator (+
) to access the second SVG element.
The complete code snippet for Radio button customization:
<Radio
{...otherRadioProps}
sx={{
'& .MuiSvgIcon-root:not(.MuiSvgIcon-root ~ .MuiSvgIcon-root)': {
color: 'red',
},
'& .MuiSvgIcon-root + .MuiSvgIcon-root': {
color: 'blue',
},
}}
/>
Important Notes:
Border Thickness: While it's not possible to make the border thinner according to your design preferences, you can increase the border thickness through CSS alone. To manipulate the path
element inside the SVG for the radio button's surrounding border, include the following in the sx
prop:
'& .MuiSvgIcon-root:not(.MuiSvgIcon-root ~ .MuiSvgIcon-root) path':
{
stroke: 'red',
strokeWidth: 3,
},
https://i.stack.imgur.com/95Vyi.png
Note that despite setting the color: 'red'
on the .MuiSvgIcon-root
element, you still need to specify stroke: 'red',
.
Directly targeting the circle using a class name isn't feasible due to Material UI assigning the same class name to both the border and circle elements, as depicted in this Firefox DevTools screenshot:
https://i.stack.imgur.com/z27JF.png
Solution with Custom icon
and checkedIcon
Component
If you desire a thinner border, incorporating custom SVG icons via the icon
and checkedIcon
props becomes necessary. To achieve the desired look shown below:
https://i.stack.imgur.com/mb33R.png
You can implement the following icon component:
const RadioButton = ({ checked }) => (
<svg
width="38px"
height="38px"
viewBox="0 0 24 24"
fontSize="38px">
<circle
cx="50%"
cy="50%"
r="11px"
stroke="red"
stroke-width="1px"
fill="none"
/>
{checked && (
<circle
cx="50%"
cy="50%"
r="6px"
fill="blue"
/>
)}
</svg>
);
Integrate the custom icon component into your MUI Radio
component like so:
<Radio
icon={<RadioButton />}
checkedIcon={<RadioButton checked />}
{...otherProps}
/>