Material UI – 如何使用 sx 属性样式化列表标记?

huangapple go评论78阅读模式
英文:

Material UI - How to style the list marker using sx props?

问题

在Material UI中,如果我有一个带有标记的列表,可以使用sx和scss来设置标记的样式,例如更改字体、颜色等。

英文:

Follow up question from : https://stackoverflow.com/questions/75857187/material-ui-hierarchical-ordered-list

In material ui, if I have such a list with markers,

<List sx={{ listStyle: "decimal", pl: 4 }}>
  <ListItem sx={{ display: "list-item" }}>
    <ListItemText primary="A" />
  </ListItem>
  <ListItem sx={{ display: "list-item" }}>
    <ListItemText primary="List" />
  </ListItem>
  <ListItem sx={{ display: "list-item" }}>
    <ListItemText primary="Item" />
  </ListItem>
</List>

1. xxxxx
2. xxxxx
   a. xxxxx
   b. xxxxx
3. xxxxx
4. xxxxx
   a. xxxxx
   b. xxxxx

How do I style the marker using sx and scss ? eg change the font, color, etc.

Thanks.

答案1

得分: 1

以下是代码部分的中文翻译:

sx={{
  display: "list-item",
  fontFamily: "Calibri",
  color: "#ff0000"
}}

你可以在MUI文档中找到如何使用sx的示例。

要使用scss,请将其导入到使用样式的文件中:

import './styles.scss';

类似Webpack这样的打包工具需要一个特殊的加载器来将scss文件转换为JavaScript。

在scss中使用的选择器(ID、类等)可以通过在JSX标签中使用idclassName JSX属性来匹配:

<ListItem id="someId" className="someGlobalClassName" sx={{ display: "list-item" }}>

为了避免在每个标签上重复定义css或应用类名,你可以直接定位MUI组件,使用它们的全局类名。例如,<ListItem>标签使用.MuiListItem-root来应用样式到根元素。全局类名在API页面中列出。

更新

请注意,使用标准的css,默认情况下,不可能更改列表项的符号颜色。这样的更改需要像w3schools.com上演示的伪元素技巧:

ul li::before {
    content: '\u2022'; // 符号字符
    color: ...;
    font-family: ...;
    ...
}

标准的css属性为list-style包括:list-style-imagelist-style-positionlist-style-type。列表标记颜色属性并不存在。

Material-UI列表使用像图标、头像、图片和复选框等组件作为标记。sx prop会像应用于其他MUI组件一样应用到这些组件上。但是,如果你想要使用伪元素技巧来应用样式到标准外观的标记,可以尝试以下代码。

这个示例代码显示了两个列表。第一个使用符号,第二个使用十进制列表类型。

希望这些翻译对你有所帮助。如果有其他问题,请随时提出。

英文:

Your question contains css with sx and demonstrates how to apply styles using sx:

sx={{
  display: &quot;list-item&quot;,
  fontFamily: &quot;Calibri&quot;,
  color: &quot;#ff0000
}}

The MUI docs have examples on how to use sx.

To use scss, import it into the file that uses the styles:

import &#39;./styles.scss&#39;;

A bundler like Webpack needs a special loader to read scss files into JavaScript.

The selectors (IDs, classes, etc.) used in the scss match those in the JSX tags by using the id and className JSX attributes:

&lt;ListItem id=&quot;someId&quot; className=&quot;someGlobalClassName&quot; sx={{ display: &quot;list-item&quot; }}&gt;

To avoid repeating css definitions or applying class names on each tag you can target MUI components directly by using their Global class names. For example, the &lt;ListItem&gt; tag uses .MuiListItem-root to apply styles to the root element. Global class names are listed on the API page for components.

UPDATE

Note that using standard css By default, it is not possible to change the bullet color of a list item. --w3schools.com. Such changes require pseudo-element hacks like this (as demonstrated on the w3schools.com page):

ul li::before {
    content: &#39;\u2022&#39;; // Bullet character
    color: ...;
    font-family: ...;
    ...
}

Standard css properties for list-style are: list-style-image, list-style-position, and list-style-type. A list marker color property does not exist.

As markers, Material-UI Lists use components such as icons, avatars, images and checkboxes. The sx prop is applied to these components as it would be applied to any other MUI component. However, if you are looking to employ pseudo-element hacks to apply styles to standard-looking markers you can try the code below.

This sample code displays two lists. The first uses bullets. The second uses a decimal list type.

index.html

&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;MUI List sample&lt;/title&gt;
        &lt;link rel=&quot;icon&quot; href=&quot;data:;&quot;&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;&gt;
        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, user-scalable=1&quot;&gt;
        &lt;script defer src=&quot;index.js&quot;&gt;&lt;/script&gt;&lt;/head&gt;
    &lt;body&gt;
        &lt;div id=&quot;container&quot;&gt;&lt;/div&gt;
        &lt;noscript&gt; Please enable javascript to view the page&lt;/noscript&gt;
    &lt;/body&gt;
&lt;/html&gt;

app.jsx

/**
 * Modified Basic List example from MUI docs.
 * Compiled into a bundle saved as index.js.
 *
 * @see https://mui.com/material-ui/react-list/#basic-list
 */
import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom&#39;;
import Box from &#39;@mui/material/Box&#39;;
import List from &#39;@mui/material/List&#39;;
import ListItem from &#39;@mui/material/ListItem&#39;;
import ListItemButton from &#39;@mui/material/ListItemButton&#39;;
import ListItemIcon from &#39;@mui/material/ListItemIcon&#39;;
import ListItemText from &#39;@mui/material/ListItemText&#39;;
import Divider from &#39;@mui/material/Divider&#39;;
import InboxIcon from &#39;@mui/icons-material/Inbox&#39;;
import DraftsIcon from &#39;@mui/icons-material/Drafts&#39;;

export default function App() {
    return (
        &lt;Box sx={ { width: &#39;100%&#39;, maxWidth: 360, bgcolor: &#39;background.paper&#39; } }&gt;
            &lt;nav aria-label=&quot;main mailbox folders&quot;&gt;
                &lt;List
                    sx={ {
                        [`&amp; li.MuiListItem-root::before`]: {
                            content: &quot;&#39;\u2022&#39;&quot;, // Bullet character
                            color: &#39;red&#39;,
                            fontFamily: &#39;Segoe UI&#39;
                        }
                    } }
                &gt;
                    &lt;ListItem disablePadding&gt;
                        &lt;ListItemButton&gt;
                            &lt;ListItemIcon&gt;
                                &lt;InboxIcon /&gt;
                            &lt;/ListItemIcon&gt;
                            &lt;ListItemText primary=&quot;Inbox&quot; /&gt;
                        &lt;/ListItemButton&gt;
                    &lt;/ListItem&gt;
                    &lt;ListItem disablePadding&gt;
                        &lt;ListItemButton&gt;
                            &lt;ListItemIcon&gt;
                                &lt;DraftsIcon /&gt;
                            &lt;/ListItemIcon&gt;
                            &lt;ListItemText primary=&quot;Drafts&quot; /&gt;
                        &lt;/ListItemButton&gt;
                    &lt;/ListItem&gt;
                &lt;/List&gt;
            &lt;/nav&gt;
            &lt;Divider /&gt;
            &lt;nav aria-label=&quot;secondary mailbox folders&quot;&gt;
                &lt;List
                    sx={ {
                        [`&amp; li.MuiListItem-root`]: {
                            display: &#39;list-item&#39;,
                            listStyleType: &#39;decimal&#39;,
                            marginLeft: &#39;1em&#39;
                        },
                        [`&amp; li.MuiListItem-root::marker`]: {
                            width: &#39;1em&#39;,
                            fontFamily: &quot;&#39;Palatino Linotype&#39;&quot;,
                            fontSize: &#39;24px&#39;,
                            color: &#39;red&#39;
                        }
                    } }
                &gt;
                    &lt;ListItem disablePadding&gt;
                        &lt;ListItemButton&gt;
                            &lt;ListItemText primary=&quot;Trash&quot; /&gt;
                        &lt;/ListItemButton&gt;
                    &lt;/ListItem&gt;
                    &lt;ListItem disablePadding&gt;
                        &lt;ListItemButton component=&quot;a&quot; href=&quot;#simple-list&quot;&gt;
                            &lt;ListItemText primary=&quot;Spam&quot; /&gt;
                        &lt;/ListItemButton&gt;
                    &lt;/ListItem&gt;
                    &lt;ListItem&gt;
                        &lt;ListItemText primary=&quot;A&quot; /&gt;
                    &lt;/ListItem&gt;
                    &lt;ListItem&gt;
                        &lt;ListItemText primary=&quot;List&quot; /&gt;
                    &lt;/ListItem&gt;
                    &lt;ListItem&gt;
                        &lt;ListItemText primary=&quot;Item&quot; /&gt;
                    &lt;/ListItem&gt;
                &lt;/List&gt;
            &lt;/nav&gt;
        &lt;/Box&gt;
    );
}


const container = document.getElementById( &#39;container&#39; );

ReactDOM.render(
    &lt;App /&gt;,
    container
);

huangapple
  • 本文由 发表于 2023年4月4日 17:59:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定