如何使页脚位于屏幕底部?

huangapple go评论61阅读模式
英文:

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 (
        &lt;header&gt;
            &lt;nav className=&quot;nav&quot;&gt;
                &lt;img src=&quot;./react-logo.png&quot; className=&quot;nav-logo&quot; /&gt;
                &lt;ul className=&quot;nav-items&quot;&gt;
                    &lt;li&gt;Pricing&lt;/li&gt;
                    &lt;li&gt;About&lt;/li&gt;
                    &lt;li&gt;Contact&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/nav&gt;
        &lt;/header&gt;
    )
}

function Footer() {
    return (
        &lt;footer&gt;
            &lt;small&gt;&#169; 2021 Ziroll development. All rights reserved.&lt;/small&gt;
        &lt;/footer&gt;
    )
}

function MainContent() {
    return (
        &lt;div&gt;
            &lt;h1&gt;Reasons I&#39;m excited to learn React&lt;/h1&gt;
            &lt;ol&gt;
                &lt;li&gt;It&#39;s a popular library, so I&#39;ll be 
                able to fit in with the cool kids!&lt;/li&gt;
                &lt;li&gt;I&#39;m more likely to get a job as a developer
                if I know React&lt;/li&gt;
            &lt;/ol&gt;
        &lt;/div&gt;
    )
}

function Page() {
    return (
        &lt;div&gt;
            &lt;Header /&gt;
            &lt;MainContent /&gt;
            &lt;Footer /&gt;
        &lt;/div&gt;
    )
}

ReactDOM.render(&lt;Page /&gt;, document.getElementById(&quot;root&quot;))

<!-- 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 (
            &lt;footer style={{ position: &#39;fixed&#39;, width: &#39;100%&#39;, left: 0, bottom: 0}}&gt;
                &lt;small&gt;&#169; 2021 Ziroll development. All rights reserved.&lt;/small&gt;
            &lt;/footer&gt;
        )
    }

or create a css file:

    //jsx file
    import &#39;./css.css&#39;
    
    function Footer() {
        return (
            &lt;footer className=&#39;footer&#39;&gt;
                &lt;small&gt;&#169; 2021 Ziroll development. All rights reserved.&lt;/small&gt;
            &lt;/footer&gt;
        )
    }


    //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 (
        &lt;footer className=&quot;target-for-my-css&quot;&gt;
            &lt;small&gt;&#169; 2021 Ziroll development. All rights reserved.&lt;/small&gt;
        &lt;/footer&gt;
    )
}

<!-- language: lang-css -->

.target-for-my-css{
  position: &quot;absolute&quot;;
  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 (
        &lt;&gt;
            &lt;div style={{ minHeight: &quot;100vh&quot;, marginBottom: &quot;-50px&quot; }}&gt;
                &lt;Header /&gt;
                &lt;MainContent /&gt;

            &lt;/div&gt;
            &lt;Footer /&gt;
        &lt;/&gt;
    )
}

答案4

得分: 0

&lt;!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false --&gt;

&lt;!-- 语言: lang-css --&gt;

    body{
        min-height: 100vh;
        display: flex;
        flex-direction: column;
    }
    footer{
        margin-top: auto;
    }


&lt;!-- 结束代码片段 --&gt;

尝试这个,它对我有效,因为无论你在中间添加多少其他页面,页脚都会保持在底部。
英文:

<!-- 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.

huangapple
  • 本文由 发表于 2023年6月2日 11:08:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定