简体   繁体   English

create-react-app 中 CSS 公用文件夹中的参考文件

[英]Reference file in public folder from CSS in create-react-app

I am using creat-react-app (CRA) and simply want to include a png file placed in the public folder via CSS (I keep all image files there).我正在使用 creat-react-app (CRA),只是想通过 CSS 包含一个放在公共文件夹中的 png 文件(我将所有图像文件都保存在那里)。 Now I am trying to reference this image via CSS (I only added the background-image line to a freshly generated CRA app):现在我试图通过 CSS 引用此图像(我只将background-image行添加到新生成的 CRA 应用程序):

.App-header {
  background-color: #222;
  height: 150px;
  padding: 20px;
  color: white;
  background-image: url("../public/example.png");
}

You attempted to import example.png which falls outside of the project src/ directory

How do I reference the image file from within the CSS-file without copying somewhere inside /src ?如何从 CSS 文件中引用图像文件而不复制/src中的某处? I also don't want to eject the application and get rid of the error message.我也不想弹出应用程序并摆脱错误消息。

Edit: This question is different from The create-react-app imports restriction outside of src directory because it does not show how to solve this problem at a CSS level.编辑:这个问题不同于The create-react-app imports restriction outside of src directory因为它没有显示如何在 CSS 级别解决这个问题。 The proposed solution is to add the style inside the JavaScript file which I don't want to do.建议的解决方案是在我不想做的 JavaScript 文件中添加样式。

Just use a / before the name , this will make it relative to the output root, which includes anything in the public folder (provided the finished hosted application is served at the root of a domain).只需在 name 之前使用 / ,这将使其相对于输出根目录,其中包括公共文件夹中的任何内容(前提是完成的托管应用程序在域的根目录下提供)。

so for the question asked above:所以对于上面提出的问题:

.App-header {
  background-color: #222;
  height: 150px;
  padding: 20px;
  color: white;
  background-image: url("/example.png");
}

the critical part being关键部分是

/example.png 

refers to a file, example.png, that is in the public folder (served at the root level)指的是 public 文件夹中的文件 example.png(在根级别提供服务)

Could also be relative:也可以是相对的:

one could also use一个也可以使用

./example.png

provided that the css file was also imported from the public/build directory, this would be relative to the css file and not depend on being served at the domain root, but typically in CRA webpack will bundle the CSS and it may or may not be loaded from this location.如果 css 文件也是从 public/build 目录导入的,这将是相对于 css 文件的,而不取决于在域根目录中提供的服务,但通常在 CRA webpack 中会捆绑 CSS,它可能会也可能不会从这个位置加载。 (you could import it in the html file directly using rel tag with the %PUBLIC_URL%/Styles.css macro) (您可以使用带有%PUBLIC_URL%/Styles.css宏的 rel 标签直接将其导入到 html 文件中)

In my case, to access the images from css/scss, had to move the images directory as well as fonts, to the src directory.就我而言,要从 css/scss 访问图像,必须将图像目录和字体移动到 src 目录。 After which i was able to refer them in the css/scss files directly,之后我可以直接在 css/scss 文件中引用它们,

 background-image: url("/images/background.jpg");

references:参考:

https://github.com/facebook/create-react-app/issues/829 https://github.com/facebook/create-react-app/issues/829

https://create-react-app.dev/docs/using-the-public-folder/ https://create-react-app.dev/docs/using-the-public-folder/

I was using "public" folder but it says here to use images inside "src" folder:我正在使用“public”文件夹,但它在这里说要使用“src”文件夹中的图像:

https://create-react-app.dev/docs/using-the-public-folder/ https://create-react-app.dev/docs/using-the-public-folder/

"Normally we recommend importing stylesheets, images, and fonts from JavaScript." “通常我们建议从 JavaScript 导入样式表、图像和 fonts。”

Remove the extra .. in the relative path.删除相对路径中多余的.. :) :)

Use "./image.png" .使用"./image.png"

The logic behind is that the css file you will place the code in, will be part of index.html when the app is finished, built and deployed.背后的逻辑是,当应用程序完成、构建和部署时,您将放置代码的css文件将成为index.html一部分。

The only way that helped me is adding full URL-adress to the image path.帮助我的唯一方法是将完整的 URL 地址添加到图像路径中。 So this one can help:所以这个可以帮助:

background-image: url("http://localhost:3000/background.jpg"); 

背景图像:url(“http:/images/pexels-photo-1.jpg”);

there is a special page about the /public folder in the official documentation:官方文档中有一个关于/public文件夹的特殊页面:
https://create-react-app.dev/docs/using-the-public-folder/ https://create-react-app.dev/docs/using-the-public-folder/

Here is how you should refer to the public folder:以下是您应该如何引用公用文件夹:

<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />

or或者

render() {
  return <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;
}

BUT, you can't use those solution in a css file.但是,您不能在 css 文件中使用这些解决方案。 So you'll have to precise the ressource from the js files:因此,您必须从 js 文件中精确定位资源:

const MyComp = () => {
  return <div style={{ backgroundImage: `${process.env.PUBLIC_URL}/img/logo.png` }} />;
}

Using the URL directly doesn't work for my environment, for me I needed to specify the path TO the image FROM the current directory.直接使用 URL 对我的环境不起作用,对我来说我需要从当前目录指定图像的路径。

eg;例如;

/src/styles/style.css
cursor: url("../../public/images/image.png"), auto;


/src/public/images/image.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM