gpt4 book ai didi

css - React makeStyles 不设置背景图片

转载 作者:太空宇宙 更新时间:2023-11-03 22:14:50 24 4
gpt4 key购买 nike

尽管尝试了多种方法来为 backgroundImage 属性加载图像,但它从未出现在页面中。加载外部图像(例如来自谷歌)按预期工作。

我试过:

backgroundImage: `url(${Papyrus})`
backgroundImage: "url(" + Papyrus + ")"
backgroundImage: "url(../../assets/images/papyrus.png)"
backgroundImage: Papyrus
backgroundImage: "url(\"../../assets/images/papyrus.png\")"
backgroundImage: "url(assets/images/papyrus.png)"

它们都不起作用。图片在我查看我的网络审核时加载,我可以在静态文件夹中找到它,但它永远不会显示。

应用.tsx

import React from 'react';
import makeStyles from './app-styles';
import {Container} from "@material-ui/core";
import Description from "../description/description";

const App: React.FC = () => {
const classes = makeStyles();
return (
<div className="App">
<Container maxWidth={"xl"}>
<div className={classes.row}>
<Description/>
</div>
</Container>
</div>
);
};

export default App;

描述.tsx

import * as React from "react";
import makeStyles from './description-styles';

interface DescriptionProps {
}

const Description: React.FC<DescriptionProps> = () => {
const classes = makeStyles();

return (
<div className={classes.descriptionCard}>
<p>Some text</p>
</div>
)
};

export default Description;

描述样式.tsx

import makeStyles from "@material-ui/core/styles/makeStyles";
import Papyrus from "../../assets/images/papyrus.png";

export default makeStyles(theme => ({
descriptionCard: {
backgroundImage: `url(${Papyrus})`,
// margin: 'auto',
height: '25vh',
width: 'calc(20vw * 0.54 - 2%)',
borderRadius: 8,
display: 'flex',
marginLeft: '10px',
marginTop: '10px'
},
text: {

}
}))

最佳答案

给背景图片添加一些额外的属性就可以了-

descriptionCard: {
backgroundImage: `url(${Papyrus})`,
backgroundPosition: 'center',
backgroundSize: 'cover',
backgroundRepeat: 'no-repeat',
// margin: 'auto',
height: '25vh',
width: 'calc(20vw * 0.54 - 2%)',
borderRadius: 8,
display: 'flex',
marginLeft: '10px',
marginTop: '10px'
}

我不确定为什么我们需要这些额外的属性(也许有人可以添加到答案中),但有时图像需要定义某些行为,如大小、位置等。

关于css - React makeStyles 不设置背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57254046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com