Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to style the border of the ListItemButton when selected in Material UI?

I have the functionality where if the ListItemButton is selected, it will highlight it with a given background color. But now I want to style the ListItemButton even more by adding an outline or border color around it when selected. I used the attribute borderColor but currently it is not working.

Button but not border or outline style when selected

Source code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 /* eslint-disable react/react-in-jsx-scope -- Unaware of jsxImportSource */
/** @jsxImportSource @emotion/react */
import { useState } from "react";
import ListItemButton from "@mui/material/ListItemButton";
import { css } from "@emotion/react";


export default function TestSample() {
  

  const [selected2, setSelected2] = useState(false);

  return (
    <div className="App">
      <ListItemButton
        sx={{
          "&.Mui-selected": {
            backgroundColor: "#02A7DD",
            borderColor: "red",
          },
          "&.Mui-focusVisible": {
            backgroundColor: "#02A7DD",
            borderColor: "red",
          },
          ":hover": {
            backgroundColor: "#02A7DD",
            borderColor: "red",
          },
        }}
        selected={selected2}
        onClick={() => setSelected2((prev) => !prev)}
      >
       ListItemButton
      </ListItemButton>

      
    </div>
  );
}

Hoping for your responses as this would help me a lot especially since I am exploring MUI at the moment an trying to understand its use and implementation. Thank you very much!

>Solution :

You’re missing the borderWidth and borderStyle properties. Right now you have a red border that is invisible and 0 pixels thick.

As a shorthand you can specify just the border property instead, for example:

sx={{
    border: '1px solid red'
}}

which is the same as

sx={{
    borderStyle: 'solid',
    borderWidth: '1px',
    borderColor: 'red'
}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading