英文:
How to change the borderColor of Paper component from material-ui react?
问题
我正在使用React和Material-UI,在Material-UI中,我尝试对Paper组件进行一些自定义,比如添加绿色的边框。
这是我尝试过的代码:
<Paper
style={{ padding: 50, }}
variant="outlined" square={true}
classes={{
root: classes.root, // class name, e.g. `classes-nesting-root-x`
}}
>
这是Paper组件
</Paper>
这是我的自定义样式:
root: {
borderRadius: 20,
borderColor: '#000'
}
borderRadius
属性正常工作,但borderColor
属性不起作用。
有什么建议吗?
英文:
I'm using react with material-ui, In material-ui, im trying to make use of Paper component with some customization like adding borderColor to green.
Here is what i tried,
<Paper
style={{ padding: 50, }}
variant="outlined" square={true}
classes={{
root: classes.root, // class name, e.g. `classes-nesting-root-x`
}}
>
This paper component
</Paper>
Here is my customization styles..
root: {
borderRadius: 20,
borderColor: '#000'
},
BorderRadius property is working fine, But BorderColor property is not working,
Any suggestions?
答案1
得分: 6
使用withStyles
在你的组件中。这将允许你覆盖样式:
import React, { Component } from "react";
import withStyles from "@material-ui/core/styles/withStyles";
import Paper from "@material-ui/core/Paper";
const styles = () => ({
root: { borderRadius: 20, borderColor: "#000", padding: 50 }
});
export class ExampleComponent extends Component {
render() {
const {classes} = this.props;
return (
<Paper
className={classes.root}
variant="outlined"
square={true}
>
This paper component
</Paper>
);
}
}
export default withStyles(styles)(ExampleComponent);
如果你有一个主题,只需重新编写样式,这将首先解构主题文件和其中的属性,然后执行(或覆盖)你在这个对象中制定的样式:
const styles = theme => ({
...theme,
paper: { borderRadius: 20, borderColor: "#000", padding: 50 }
});
对于函数式组件:
import React from 'react';
import { styled } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
const MyPaper = styled(Paper)({ borderRadius: 20, borderColor: "#000", padding: 50 });
export default function StyledComponents() {
return <MyPaper>Styled Components</MyPaper>;
}
或者
import React from 'react';
import { withStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
const styles = {
root: { borderRadius: 20, borderColor: "#000", padding: 50 },
};
function AnotherComponent(props) {
const { classes } = props;
return <Paper className={classes.root}>Another component</Paper>;
}
export default withStyles(styles)(AnotherComponent);
英文:
Use it with withstyles
in your component. This will allow you to override styles:
import React, { Component } from "react";
import withStyles from "@material-ui/core/styles/withStyles";
import Paper from "@material-ui/core/Paper";
const styles = () => ({
root: { borderRadius: 20, borderColor: "#000", padding: 50 }
});
export class ExampleComponent extends Component {
render() {
const {classes} = this.props;
return;
<Paper
className={classes.root}
variant="outlined"
square={true}
>
This paper component
</Paper>;
}
}
export default withStyles(styles)(ExampleComponent);
If you have a theme, just rewrite the styles, this will first destructure the theme file and the properties in it, after then will execute (or override) the styles you make in this object:
const styles = theme => ({
...theme,
paper: { borderRadius: 20, borderColor: "#000", padding: 50 }
});
for functional component:
import React from 'react';
import { styled } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
const MyPaper = styled(Paper)({ borderRadius: 20, borderColor: "#000", padding: 50 });
export default function StyledComponents() {
return <MyPaper>Styled Components</MyPaper>;
}
or
import React from 'react';
import { withStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
const styles = {
root: { borderRadius: 20, borderColor: "#000", padding: 50 },
};
function AnotherComponent(props) {
const { classes } = props;
return <Paper className={classes.root}>Another component</Paper>;
}
export default withStyles(styles)(AnotherComponent);
答案2
得分: 1
在Material-UI v5中,您可以轻松使用sx属性来更改颜色。您的代码将如下所示:
<Paper sx={{ p: '50px', borderRadius: '20px', borderColor: 'green' }} variant="outlined">
Paper组件的内容
</Paper>
注意: 对于padding
和border-radius
,如果您不想使用Material-UI主题间距(例如,使用主题缩放因子padding: 1
将变为padding: '8px'
),则最有可能需要使用px单位,参见文档。
英文:
In Material-UI v5 you can easily use the sx-property to change the color. Your code would look like that:
<Paper sx={{p: '50px', borderRadius: '20px', borderColor: 'green'}} variant="outlined">
Content of Paper Component
</Paper>
Note: For padding
and border-radius
you most likely have to use the px unit if you don't want to use the Material-UI theme spacing (e.g. with the theme scaling factor padding: 1
would be padding: '8px'
, see the docs)
答案3
得分: 1
将全局设置纸张边框样式可以通过以下方式实现
createTheme(theme, {
components: {
MuiPaper: {
styleOverrides: {
rounded: { border: "solid red", borderRadius: 5 },
},
},
英文:
Setting paper's border styling globally can be achieved the following way
createTheme(theme, {
components: {
MuiPaper: {
styleOverrides: {
rounded: { border: "solid red", borderRadius: 5 },
},
},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论