英文:
How to make the footer be at the bottom of the screen?
问题
我目前有一些运行良好的代码,但无论我怎么做,都无法使页脚固定在屏幕底部。是否有人能够快速提供解决方案并将其添加到JSX代码中?
function Header() {
return (
<header>
<nav className="nav">
<img src="./react-logo.png" className="nav-logo" />
<ul className="nav-items">
<li>Pricing</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
</header>
)
}
function Footer() {
return (
<footer>
<small>© 2021 Ziroll development. All rights reserved.</small>
</footer>
)
}
function MainContent() {
return (
<div>
<h1>Reasons I'm excited to learn React</h1>
<ol>
<li>It's a popular library, so I'll be
able to fit in with the cool kids!</li>
<li>I'm more likely to get a job as a developer
if I know React</li>
</ol>
</div>
)
}
function Page() {
return (
<div>
<Header />
<MainContent />
<Footer />
</div>
)
}
ReactDOM.render(<Page />, document.getElementById("root"))
英文:
I currently have some code that runs fine but no matter what I do, I cannot get the footer to stick to the bottom of the screen. Is it possible for anyone who can come up with a quick solution for this to add it to jsx code?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function Header() {
return (
<header>
<nav className="nav">
<img src="./react-logo.png" className="nav-logo" />
<ul className="nav-items">
<li>Pricing</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
</header>
)
}
function Footer() {
return (
<footer>
<small>© 2021 Ziroll development. All rights reserved.</small>
</footer>
)
}
function MainContent() {
return (
<div>
<h1>Reasons I'm excited to learn React</h1>
<ol>
<li>It's a popular library, so I'll be
able to fit in with the cool kids!</li>
<li>I'm more likely to get a job as a developer
if I know React</li>
</ol>
</div>
)
}
function Page() {
return (
<div>
<Header />
<MainContent />
<Footer />
</div>
)
}
ReactDOM.render(<Page />, document.getElementById("root"))
<!-- end snippet -->
答案1
得分: 1
你可以使用内联 CSS 实现它,例如:
function Footer() {
return (
<footer style={{ position: 'fixed', width: '100%', left: 0, bottom: 0}}>
<small>© 2021 Ziroll development. All rights reserved.</small>
</footer>
)
}
或者创建一个 CSS 文件:
// jsx 文件
import './css.css'
function Footer() {
return (
<footer className='footer'>
<small>© 2021 Ziroll development. All rights reserved.</small>
</footer>
)
}
// css 文件
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
}
<details>
<summary>英文:</summary>
you can do it with inline css like:
function Footer() {
return (
<footer style={{ position: 'fixed', width: '100%', left: 0, bottom: 0}}>
<small>© 2021 Ziroll development. All rights reserved.</small>
</footer>
)
}
or create a css file:
//jsx file
import './css.css'
function Footer() {
return (
<footer className='footer'>
<small>© 2021 Ziroll development. All rights reserved.</small>
</footer>
)
}
//css file
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
}
</details>
# 答案2
**得分**: 0
尝试这个。将页脚的位置设为绝对定位,底部为0。看看是否有效。如果你喜欢的话,你可以使用内联样式。
```jsx
function Footer() {
return (
<footer className="target-for-my-css">
<small>© 2021 Ziroll development. All rights reserved.</small>
</footer>
)
}
.target-for-my-css {
position: absolute;
bottom: 0;
}
英文:
Try this. Give the footer a position of absolute, bottom 0. See if it works. You can use inline-styles if you prefer it.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function Footer() {
return (
<footer className="target-for-my-css">
<small>© 2021 Ziroll development. All rights reserved.</small>
</footer>
)
}
<!-- language: lang-css -->
.target-for-my-css{
position: "absolute";
bottom: 0;
}
<!-- end snippet -->
答案3
得分: 0
你可以简单地将以下样式添加到你的代码中,并更改 dev
结构。
function ErrorExample() {
return (
<>
<div style={{ minHeight: "100vh", marginBottom: "-50px" }}>
<Header />
<MainContent />
</div>
<Footer />
</>
)
}
英文:
You can simply add the bellow styles to your code and change the dev
structure.
function ErrorExample() {
return (
<>
<div style={{ minHeight: "100vh", marginBottom: "-50px" }}>
<Header />
<MainContent />
</div>
<Footer />
</>
)
}
答案4
得分: 0
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-css -->
body{
min-height: 100vh;
display: flex;
flex-direction: column;
}
footer{
margin-top: auto;
}
<!-- 结束代码片段 -->
尝试这个,它对我有效,因为无论你在中间添加多少其他页面,页脚都会保持在底部。
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body{
min-height: 100vh;
display: flex;
flex-direction: column;
}
footer{
margin-top: auto;
}
<!-- end snippet -->
Try this one, it worked for me, because it won't matter the size of the other pages you add in between, the footer will stick at the bottom.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论