css - 背景位置未固定在页面上

标签 css reactjs

我正在构建具有多个组件的 React 应用程序,但背景图像有问题:当我在表中添加任何行(在 div 标签上)时,它会上下移动。临时修复:更改背景位置属性。下面是一些代码:

App.js

import React from 'react';

import Header from './components/Header'
import TournamentsTable from './components/TournamentsTable'
import Footer from './components/Footer'

function App() {
  return (
    <div>
      <div id="background"></div>
      <div id="wrapper">
        <Header />
        <TournamentsTable />
        <Footer />
      </div>
    </div>
  );
}

export default App;

样式

#background {
    width: 100%;
    max-width: 2000px;
    height: 100%;
    position: absolute;
    background: url('/images/background.jpg');
    background-repeat: no-repeat;
    background-size: cover;
    background-color: #111111;
    background-position: center -440px;
    opacity: 0.6;
}

#wrapper {
    position: relative;
}

最佳答案

你首先需要确保你的Body占据了整个页面:

body {
    min-height: 100vh
}
#background {
    width: 100%;
    height: 100%;
    position: absolute;
    background: #111111 url('/images/background.jpg') no-repeat;
    background-size: cover;
    background-position: center center;
    opacity: 0.6;
}

然后在你的样式中添加“背景附件”:

#background {
        width: 100%;
        height: 100%;
        position: absolute;
        background: #111111 url('/images/background.jpg') no-repeat;
        background-size: cover;
        background-position: center center;
        background-attachment: fixed;
        opacity: 0.6;


    }

关于css - 背景位置未固定在页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57223504/

相关文章:

javascript - 一旦单击 React,单选按钮的状态将不会保留

html - 在固定宽度跨度旁边显示表格

android - 在手机浏览器上单击图标时显示边框

javascript - jQuery shadowbox 视频在 IE7 中不工作

javascript - 考虑 React Virtualized 中的动态顶部元素 `Grid`

reactjs - 在 react-leaflet 上的 react-leaflet-draw 绘制层的弹出窗口中渲染 react 组件

css - 如何进行类似翻页的转换 : rotateY?

html - 尝试创建站点地图,但文字出现在不同的行上

reactjs - React Native admob,内部错误

javascript - ReactJS 中的复选框问题