【问题标题】:How do I add a background image in React+bootstrap?如何在 React+bootstrap 中添加背景图片?
【发布时间】:2019-12-01 16:51:06
【问题描述】:

我一直在尝试向我的网页添加背景图片。这就是我目前正在尝试的方式,但它似乎不起作用。

在我的 App.js 文件中:


import React from 'react';
import './App.css';
import NavBar from './Components/NavBar';
import LandingPageImage from './Components/LandingPageImage';


function App() {
  return (
    <div className="App">
      <div className="Screen" >
        <NavBar/>
        <LandingPageImage/>
      </div>
    </div>
  );
}

export default App;

在我的 App.css 文件中:

.Screen {
 background-image: url("https://images.unsplash.com/photo-1536514498073-50e69d39c6cf?ixlib=rb-1.2.1&auto=format&fit=crop&w=1951&q=80");
}

这似乎不起作用。我希望在 Screen div 上看到背景图片,但什么也没显示。

【问题讨论】:

  • 您可以简单地通过在 chrome 中检查开发人员工具来调试此分配的 css 类是否已正确添加到 html 中。
  • 希望它能解决你的问题stackoverflow.com/a/53360835/10477567

标签: reactjs twitter-bootstrap web-frontend


【解决方案1】:

代码是正确的。我在想你的&lt;Navbar /&gt;&lt;LandingPageComponent /&gt; 用他们自己的背景覆盖了整个div,因此你看不到它。

尝试在 div 中添加一些文本,看看它是否正常工作。

import React from 'react';
import './App.css';
import NavBar from './Components/NavBar';
import LandingPageImage from './Components/LandingPageImage';


function App() {
  return (
    <div className="App">
      <div className="Screen" >
        <NavBar/>
        <LandingPageImage/>
        Text to give additional height to div.
      </div>
    </div>
  );
}

export default App;

【讨论】:

  • 天哪,你是对的。我觉得好傻非常感谢。
  • 很高兴,我能帮上忙!
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
  • 2016-05-25
相关资源
最近更新 更多