css - 使用 React-Router 和 Outlet 时如何确保内容保留在下方

标签 css reactjs react-router

所以我有一个应用程序,有 2 个页面,其中一个是主屏幕,我们称之为主屏幕,(默认页面,或者更确切地说,使用 Outlet 加载所有其他页面时应该显示的页面来自react-router的组件),而另一个是不同的页面,称为其他。

现在我在主屏幕上的页面底部出现了一些文本,但是,如果其他屏幕中的文本超出了页面的长度,则该文本应该出现在页面底部实际上与其他屏幕上的文本重叠。

Example

这是我的意思的一个例子,无论我实际选择哪个页面,栏、电话号码和电子邮件都应该在那里,并且理论上应该被向下推,但它们是重叠的

代码:这是主文件:

import './App.css';
import Sidebar from './components/sidebar';
import { Outlet } from 'react-router-dom';

export default function App() {
  return (
    <div className="App" id="outer-container">
      <Sidebar />

      <hr id='line' />
      <p id='p'>Phone: +1 (XXX) XXX-XXXX</p>
      <p id='p2'>Email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8bd3d3d3d3d3cbd3d3d3d3d3a5e8e4e6" rel="noreferrer noopener nofollow">[email protected]</a></p>
      <p id='p3'>Follow me on:</p>
      <Outlet />
    </div>
  );
}

其对应的CSS文件为:

hr#line {
    position: absolute;
    width: calc(100vw - 46px);
    height: 0px;
    left: 23px;
    right: 23px;
    top: 80vh;

    border: 1px solid #000000;
}

p#p {
    position: absolute;
    width: 20vw;
    height: 34px;
    left: 19px;
    top: 85vh;

    font-family: 'PT Serif';
    font-style: normal;
    font-weight: 400;
    font-size: 13px;
    line-height: 95.52%;

    /* or 12px */

    color: #000000;
}

p#p2 {
    position: absolute;
    width: 20vw;
    height: 34px;
    left: calc(50vw - 10vw);
    top: 85vh;

    font-family: 'PT Serif';
    font-style: normal;
    font-weight: 400;
    font-size: 13px;
    line-height: 95.52%;

    /* or 12px */

    color: #000000;
}

p#p3 {
    position: absolute;
    width: 20vw;
    height: 34px;
    left: calc(100vw - 20vw);
    top: 85vh;

    font-family: 'PT Serif';
    font-style: normal;
    font-weight: 400;
    font-size: 13px;
    line-height: 95.52%;

    color: #000000;
}

对于我描述的“其他文件”,我们有:

export default function About() {
    return (
      <div className="about-container">
        <div className='peach-gradient color-block-5 mb-3 mx-auto rounded-circle z-depth-1 upper-about'>
          <h2>About</h2>
        </div>
          <p>
The standard Lorem Ipsum passage, used since the 1500s

"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC

"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
1914 translation by H. Rackham

"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
Section 1.10.33 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC

"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."
1914 translation by H. Rackham

"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."
</p>
      </div>
    );
  }

该文件没有 CSS

最佳答案

如果我正确理解您的问题和请求,您似乎想要一个“页脚”“粘”在页面底部,直到有足够的内容将其向下推。为此,我建议重构组件容器并改进 CSS 样式。

示例:

创建页脚组件:

const Footer = () => (
  <footer>
    <hr />
    <div className="row">
      <p>Phone: +1 (XXX) XXX-XXXX</p>
      <p>Email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bee6e6e6e6e6fee6e6e6e6e690ddd1d3" rel="noreferrer noopener nofollow">[email protected]</a></p>
      <p>Follow me on:</p>
    </div>
  </footer>
);

更新应用程序容器组件:

const App = () => (
  <div className="outer-container">
    ...
    <div className="page">
      <Outlet />
    </div>
    <Footer />
  </div>
);

CSS

.outer-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.page {
  flex: 1;
}

footer {
  position: relative;
  margin: 0 1rem;
}

footer .row {
  display: flex;
  flex-direction: row;
  width: 100%;
  justify-content: space-between;
}

hr {
  height: 0px;
  margin: 0;
  border: 1px solid #000000;
}

footer p {
  width: 20vw;
  height: 34px;

  font-family: 'PT Serif';
  font-style: normal;
  font-weight: 400;
  font-size: 13px;
  line-height: 95.52%;
  
  color: #000000;
}

它是如何工作的

"outer-container" div 的最小高度可以填满屏幕,而嵌套的 "page" div 则包裹 Outlet 并增长以填充可用空间。当 "page" div 中有足够的内容时,它会溢出并将页脚元素向下推。

Edit how-to-make-sure-content-stays-below-when-using-react-router-and-outlet

通过调整 View 宽度在沙箱中进行测试,以使“关于”页面的 lorem ipsum 文本在高度方向上增长/收缩,从而将页脚向下推或粘在底部。

关于css - 使用 React-Router 和 Outlet 时如何确保内容保留在下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72945686/

相关文章:

c# - 需要有关 z-index 属性的帮助

css - 如果一个人的高度发生变化,如何改变所有的div高度

javascript - React 状态获取 "corrupted"

javascript - 当倒数计时器变为 0 时,如何将用户重定向到另一个页面?

javascript - React、Flux、React-Router Dispatch Error - 可能的 batchUpdates 解决方案?

html - 使用高度 100% 来保持 float div 匹配高度,但其中一个段落内的段落溢出

javascript - 英雄轮播自动导航

javascript - 登录 React.js 后重定向

Jquery DIV 在动画中不能正确滑动

javascript - 类型错误 : Cannot read properties of null (reading '_leaflet_disable_click' )