【问题标题】:header/footer and page content not setup properly页眉/页脚和页面内容设置不正确
【发布时间】:2020-06-13 08:18:13
【问题描述】:

我正在建立一个网站,但我遇到了位置问题,我不知道为什么页脚没有正确放置。

所以我设置了 App.js 以在页面顶部显示页眉并始终在底部显示页脚。但是由于某些原因,当我加载其他页面(例如此处)时,页眉仍然可以,但页脚和页面内容重叠,如下图所示。不注意标题,因为屏幕短,它被剪掉了。

查看内容(txt、h 标记和蓝色按钮)和标题如何重叠。无论发生什么,我都希望始终在页眉和页脚之间保留内容。

应该是这样的:

图片上连标题都没有显示

下面是 App.js

import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

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


import Home from './pages/Home';
import NoPages from './pages/NoPages';
import Discover from './pages/Discover';
import HowItWorks from './pages/HowItWork';
import CreateClassAndHost from './pages/CreateClassAndHost';
import Profile from './pages/Profile';
import Messages from './pages/Messages';
import Settings from './pages/Settings';
import Logout from './pages/Logout';
import CreateClass from './pages/CreateClass';
import CreateHost from './pages/CreateHost';
import HereIsTheMission from './pages/HereIsTheMission';
import SuccessfullSubmission from './pages/SuccessfullSubmission';

import './App.css';

function App()  {
    return (
      <Router>
      <div>
        <div>
          <Header />
        </div>
        <div className='page-container'>
          <div className='content-wrap'>
            <Switch>
              <Route path="/" exact component={ Home } />
              <Route path="/discover" component={ Discover } />
              <Route path="/howitworks" component={ HowItWorks } />
              <Route path="/create" component={ CreateClassAndHost } />
              <Route path="/profile" component={ Profile } />
              <Route path="/messages" component={ Messages } />
              <Route path="/settings" component={ Settings } />
              <Route path="/logout" component={ Logout } />
              <Route path="/hereisthemission" component={ HereIsTheMission } />
              <Route path="/createaclass" component={ CreateClass } />
              <Route path="/createahost" component={ CreateHost } />
              <Route path="/successfull" component={ SuccessfullSubmission } />
              <Route path="*" component={ NoPages } />
            </Switch> 
          </div>
          <div className='footer'>
            <Footer />
          </div>
        </div>
      </div>
      </Router>
    );
  }

export default App;

以及相关的css App.css

.page-container {
    position: relative;
    min-height: 90vh;
}

.content-wrap {
    padding-bottom: 2.5rem;
}

.footer {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 2.5rem;
}

内容在单独的文件中完成并通过/create navlink 加载

但它看起来像机会

以下是我的内容文件:


import React from 'react';
import TextContents from '../assets/translations/TextContents';
import BlueButton from '../components/materialdesign/BlueButton';
import './CreateClassAndHost.js';

class CreateClassAndHost extends React.Component {

    render() {

        const CreateAClass = 
            <div className="tile">
                <h2 className="subtitle" >{TextContents.BeATeacher}</h2>
                <p className="text" >{TextContents.BeATeacherDesc}</p>
                <div className="button">
                <BlueButton textSize="13" link_href="/createaclass" text={TextContents.BeATeacherBtn} />
                </div>
            </div>;

        const CreateAHost = 
            <div className="tile">
                <h2 className="subtitle">{TextContents.HostAClass}</h2>
                <p className="text">{TextContents.HostAClassDesc}</p>
                <div className="button">
                <BlueButton textSize="13" link_href="/createahost" text={TextContents.HostAClassBtn} />
                </div>
            </div>;
        return (
            <div className="container">
                <h1 className="title">{TextContents.CreateClassOrHostTitle}</h1>
                <div classname="section">
                    {CreateAClass}
                    {CreateAHost}
                </div>
            </div>

        );
    }

}

export default CreateClassAndHost;

以及相关的 css:

.container {
    width: 650px;
    position: absolute;
    display: flex;
    text-align: center;
    margin: auto;
    top:30%;
    bottom: 0;
    left: 0;
    right: 0;
}


.section {
    display: inline-block;
}
.tile {
    position: relative;
    left: 12px;
    top: 80px;
    text-align: center;
    width: 200px;
    height: 250px;
    background-color: #ffffff;
}

.title {
    font-family: Fredoka One;
    font-size: 50px;
    color: #333333;
}

.subtitle {
    font-family: Source Sans Pro;
    font-weight: bold;
    font-size: 30px;
    line-height: 0.7;
    color: #333333;
    letter-spacing: -0.6px;

}

.text {
    font-family: Source Sans Pro;
    font-size: 20px;
    line-height: 1.6;
    color: #616161;
    width: 280px;
    height: 85px; 
}

.button {
    position: relative;
    left: 0px;
    top: 50px;
}

有什么想法吗?

【问题讨论】:

    标签: javascript css reactjs react-bootstrap


    【解决方案1】:

    .main-container{
      height:100vh;
    }
    
    .header{
      background-color:#000;
      color:#fff;
      padding:10px;
    }
    
    .footer{
    position:absolute;
    bottom:0;
    text-align:center;
    }
    
    .footer-menu{
    padding:10px;
    background-color:yellow;
    width:100vw;
    }
    <div class="main-container">
      <div class="header">
        <span>Heading1</span>
        <span>Heading2</span>
      </div>
      
      <div class="footer">
        <div class="footer-menu">
            <span>Footer</span>
        </div>
      </div>
    </div>

    【讨论】:

      【解决方案2】:

      您可以使用 flex-box 来实现这一点。保持您的 html 结构如下所示,并使用下面的 css

      注意:main 的高度应该等于屏幕高度

      body,html{
        height :100%;
      }
      
      .main{
        display :flex;
        flex-direction:column;
        justify-content:space-between;
        height:100%;
      }
      
      .page-container{
        overflow:auto;
      }
      
      <html>
      <body>
      <div className="main">
              <div className="header">
                <Header />
              </div>
              <div className='page-container'>
                <div className='content-wrap'>
                <Switch>
                <Route path="/" exact component={ Home } />
                <Route path="/discover" component={ Discover } />
                <Route path="/howitworks" component={ HowItWorks } />
                <Route path="/create" component={ CreateClassAndHost } />
                <Route path="/profile" component={ Profile } />
                <Route path="/messages" component={ Messages } />
                <Route path="/settings" component={ Settings } />
                <Route path="/logout" component={ Logout } />
                <Route path="/hereisthemission" component={ HereIsTheMission } />
                <Route path="/createaclass" component={ CreateClass } />
                <Route path="/createahost" component={ CreateHost } />
                <Route path="/successfull" component={ SuccessfullSubmission } />
                <Route path="*" component={ NoPages } />
                </Switch>
                </div>
              </div>
              <div className='footer'>
                  <Footer/>
                </div>
            </div>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-22
        • 2023-03-17
        • 1970-01-01
        • 2022-01-15
        • 2015-03-03
        • 1970-01-01
        • 2010-10-05
        • 1970-01-01
        相关资源
        最近更新 更多