【问题标题】:How to use background-image: url() with a relative path in react?如何在反应中使用带有相对路径的背景图像:url()?
【发布时间】:2018-08-06 12:44:46
【问题描述】:

我不知道如何正确引用图像作为组件的背景。

我的项目结构是:

//src
  //images
    //homepage
      //homepage.png
  //styles
    //base
      //components
        //homepage.scss

如何在 homepage.scss 中引用 homepage.png 并使用background-image: url('some-path');

【问题讨论】:

标签: css reactjs sass


【解决方案1】:

与在常规 html/css 中执行此操作的方式相同。只需在 return 函数中将 className 添加到顶部父 div,然后在 scss 中使用 background-img 引用它。

这是一个演示此代码的代码:https://codepen.io/wnamen/pen/QQJNVe

JS

var BackgroundImage = React.createClass({
  render:function(){
    return (<div className="img"></div>)
  }  
});

ReactDOM.render(
  <BackgroundImage/>,
  document.getElementById('app')
);

CSS

.img {
  background-image: url('https://source.unsplash.com/1223x840/?nature')
}

【讨论】:

  • 我知道该怎么做。它是指图像与 scss 文件的相对路径。
  • 明白了——我误解了这个问题
猜你喜欢
  • 1970-01-01
  • 2012-11-16
  • 2020-10-07
  • 1970-01-01
  • 2019-07-12
  • 2019-11-28
  • 2014-05-26
  • 1970-01-01
  • 2018-11-05
相关资源
最近更新 更多