522

次のページ(デッドリンク:)http://www.workingstorage.com/Sample.htmには、ページの下部に配置できないフッターがあります。

フッターを

  • ページが短く、画面がいっぱいになっていないときは、ウィンドウの下部に固執し、
  • (コンテンツをオーバーラップするのではなく)画面一杯以上のコンテンツがある場合は、ドキュメントの最後にとどまり、通常どおり下に移動します。

CSSは継承され、私を困惑させます。コンテンツに最小の高さを設定したり、フッターを下に移動したりするために、適切に変更できないようです。

4

37 に答える 37

367

簡単な方法は100%、あなたのページmin-heightのボディを作ること100%です。フッターの高さが変わらない場合、これはうまく機能します。

フッターに負の margin-top を指定します。

footer {
    clear: both;
    position: relative;
    height: 200px;
    margin-top: -200px;
}
于 2009-03-27T10:53:47.807 に答える
65

優れたクロスブラウザで機能する非常に単純なアプローチは次のとおりです。

http://matthewjamestaylor.com/blog/keeping-footers-at-the-bottom-of-the-page

html,
body {
   margin:0;
   padding:0;
   height:100%;
}
#container {
   min-height:100%;
   position:relative;
}
#header {
   background:#ff0;
   padding:10px;
}
#body {
   padding:10px;
   padding-bottom:60px;   /* Height of the footer */
}
#footer {
   position:absolute;
   bottom:0;
   width:100%;
   height:60px;   /* Height of the footer */
   background:#6cf;
}
<div id="container">
   <div id="header">header</div>
   <div id="body">body</div>
   <div id="footer">footer</div>
</div>

于 2013-11-01T09:02:56.773 に答える
18

私が使用する簡単なソリューションは、IE8+から動作します

html で min-height:100% を指定して、コンテンツがそれより少ない場合でも、ページがビューポートの高さ全体を占め、フッターがページの下部に固定されるようにします。コンテンツが増えると、フッターはコンテンツとともに下に移動し、一番下に固定され続けます。

JS フィドル動作デモ: http://jsfiddle.net/3L3h64qo/2/

CSS

html{
  position:relative; 
  min-height: 100%;
}
/*Normalize html and body elements,this style is just good to have*/
html,body{
  margin:0;
  padding:0;
}
.pageContentWrapper{
  margin-bottom:100px;/* Height of footer*/
} 
.footer{
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height:100px;
    background:#ccc;
}

HTML

   <html>
    <body>
        <div class="pageContentWrapper">
            <!-- All the page content goes here-->
        </div>
        <div class="footer">
        </div>
    </body>
    </html>
于 2014-09-19T07:16:52.440 に答える
14

Flexbox を使用してフッターを下部に保持する

<div style="min-height:100vh; display:flex; flex-direction:column; 
            justify-content:space-between;">
 
     <div> <!-- Wrapper (Without footer) -->
   
        <header>
         I am a header.
        </header>
  
        <article>
        I am an article!
        </article>
   
    </div> <!-- End: Wrapper (Without footer) -->
 
 
     <footer>
     I am a footer.
     </footer>
 
</div>

ノート

  • <div>次の CSS スタイルを使用して、またはその他のブロック レベル要素ですべてをラップしていることを確認してくださいmin-height:100vh; display:flex; flex-direction:column; justify-content:space-between;

  • フッター要素以外のすべてを a<div>またはその他のブロックレベル要素でラップしていることを確認してください。

  • <footer>またはその他のブロックレベル要素を使用してフッターをラップしていることを確認してください。

コード 説明

  • min-height: 100vhbody 要素が少なくとも画面の高さいっぱいまで伸びるようにする

  • flex-direction: columnスタックされたブロック要素を保持するという点で、通常のドキュメント フローの動作を維持します (これは、ボディの直接の子が実際にすべてブロック要素であることを前提としています)。

  • justify-content:space-betweenフッターを画面の一番下に押し込みます。


Bootstrap 5を使用して同じことを行う方法(フッターを下部に保持する)を確認してください -リンク

于 2021-06-05T06:20:53.743 に答える
8

これはスティッキーフッターとして知られています。それをグーグル検索すると、たくさんの結果が出てきます。CSSスティッキーフッターは私がうまく使ったものです。しかし、もっとあります。

* {
    margin: 0;
}
html, body {
    height: 100%;
}
.wrapper {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -4em;
}
.footer, .push {
    height: 4em;
}
<html>
    <head>
        <link rel="stylesheet" href="layout.css" ... />
    </head>
    <body>
        <div class="wrapper">
            <p>Your website content here.</p>
            <div class="push"></div>
        </div>
        <div class="footer">
            <p>Copyright (c) 2008</p>
        </div>
    </body>
</html>

このコードのソース

于 2009-03-13T18:00:15.547 に答える
8

注意すべきことの 1 つは、モバイル デバイスです。ビューポートのアイデアが「通常とは異なる」方法で実装されているためです。

http://developer.apple.com/library/ios/#documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html#//apple_ref/doc/uid/TP40006509-SW25

そのため、position: fixed;(他の場所で推奨されているように) を使用することは、通常は行うべき方法ではありません。もちろん、それはあなたが求めている正確な動作に依存します。

私が使用し、デスクトップとモバイルでうまく機能したのは次のとおりです。

<body>
    <div id="footer"></div>
</body>

body {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 0;
    margin: 0;
}
 
#footer {
    position: absolute;
    bottom: 0;
}
于 2013-04-20T13:01:09.553 に答える
6

私のために働きます。

#container{ 
  height:100vh; 
  margin:0; 
  display:flex; 
  flex-direction:column; 
}

#footer{
  margin-top:auto; 
}


<div id="container">
   <div id="header">header</div>
   <div id="body">body</div>
   <div id="footer">footer</div>
</div>

#container{ 
  height:100vh; 
  margin:0; 
  display:flex; 
  flex-direction:column; 
}

#footer{
  margin-top:auto; 
}
<div id="container">
   <div id="header">header</div>
   <div id="body">body</div>
   <div id="footer">footer</div>
</div>

于 2021-05-01T10:53:56.690 に答える
4

私のjQueryメソッド、これは、ページのコンテンツがウィンドウの高さよりも小さい場合はページの下部にフッターを配置するか、そうでない場合はコンテンツの後にフッターを配置します:

また、他のコードの前にコードを独自のエンクロージャーに保持すると、フッターの位置を変更するのにかかる時間が短縮されます。

(function() {
    $('.footer').css('position', $(document).height() > $(window).height() ? "inherit" : "fixed");
})();
于 2013-10-18T08:36:15.267 に答える
3

Bootstrap を使用した 1 行のソリューション

提供されているすべての CSS および jQuery ソリューションとは別に、Bootstrapを使用してbodyタグで単一のクラス宣言を
行うソリューションをリストしました。d-flex flex-column justify-content-between

  • これには、フッターの高さを事前に知る必要はありません。
  • これには絶対位置の設定は必要ありません
  • これは、小さな画面でオーバーフローする動的 div で機能します。

html, body {
  height: 100%;
}
<html>

    <head>
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    </head>

    <body class="d-flex flex-column justify-content-between text-white text-center">

        <header class="p-5 bg-dark">
          <h1>Header</h1>
        </header>
        <main class="p-5 bg-primary">
          <h1>Main</h1>
        </main>
        <footer class="p-5 bg-warning">
          <h1>Footer</h1>
        </footer>

        <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
    </body>

</html>

于 2020-08-14T05:25:36.817 に答える
2

、、および0 に設定された位置fixedは、私にとって最適です。bottomleftright

.footer {    
    position: fixed;
    background : #d1ccc0;
    bottom : 0;
    left : 0;
    right : 0;
    height : 50px;
}

ポジションabsoluteは底にくっつきませんが、fixedつきます。

于 2021-06-30T15:43:32.970 に答える
1

他のソリューションと比較して、コンテナを追加する必要はありません。したがって、このソリューションはもう少しエレガントです。コード例の下で、これが機能する理由を説明します。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>test</title>
        <style>
        
            html
            {
                height:100%;
            }
            
            body
            {
                min-height:100%;
                padding:0; /*not needed, but otherwise header and footer tags have padding and margin*/
                margin:0; /*see above comment*/
            }
        
            body
            {
                position:relative;
                padding-bottom:60px; /* Same height as the footer. */           
            }
            
            footer
            {
                position:absolute;
                bottom:0px;
                height: 60px;
                
                background-color: red;
            }
        
        </style>
    </head>
    <body>
        <header>header</header>
        
        
        <footer>footer</footer>
    </body>
</html>

最初に行うことは、最大のコンテナー ( html ) を 100% にすることです。html ページは、ページ自体と同じ大きさです。次に、body の高さを設定します。これは、html タグの 100% よりも大きくすることができますが、少なくとも同じ大きさにする必要があるため、min-height 100% を使用します。

また、身体を相対的にします。相対とは、ブロック要素を元の位置から相対的に移動できることを意味します。ただし、ここでは使用しません。親戚には二次利用があるからです。絶対要素は、ルート (html) または最初の相対親/祖父母に対して絶対的です。それが私たちが望んでいることです。フッターは、本文、つまり下部に対して絶対的なものにしたいと考えています。

最後のステップは、フッターをabsoluteとbottom:0に設定することです。これにより、相対的な最初の親/祖父母の下部に移動します( body ofcourse )。

ページ全体を埋めると、コンテンツがフッターの下に表示されます。なんで?フッターはもはや「html フロー」内にないため、絶対的なものです。では、これを修正するにはどうすればよいでしょうか。ボディに padding-bottom を追加します。これにより、本文がコンテンツよりも実際に大きくなります。

于 2014-12-30T15:43:20.167 に答える
-1

html、body、およびフッターを除くその他の行を 100% に設定するだけです。例えば

<body>
<header></header>
<content></content>
<footer></footer>
CSSは次のようになります
html, body, header, content{
height:100%;
}
于 2014-07-15T13:07:10.250 に答える
-1

いくつかの解決策はうまくいきませんでしたが、flex オプションを使用することを決めたときに見つけた最良のオプションは、以下の例でした。

html, body{
    height: 100%;   
}

body{
    display: flex;
    flex-direction: column;
}

.main-contents{ 
    flex: 1 0 auto;
    min-height: 100%;
    margin-bottom: -77px;
  background-color: #CCC;
}

.footer{
    height:  77px;
    min-height: 77px;
    width: 100%;
    bottom: 0;
    left: 0;
    background: #000000;
    flex-shrink: 0;
    flex-direction: row;
    position: relative;
    
    
}

.footer-text{
  color: #FFF;
}

@media screen and (max-width: 767px){
    #content{
        padding-bottom: 0;
    }
    .footer{
        position: relative;
        /*position: absolute;*/
        height: 77px;
        width: 100%;
        bottom: 0;
        left: 0;
    }

}
<html>
  <body>
    <div class="main-contents" >
      this is the main content
    </div>
  </body>

  <footer class="footer">
    <p class="footer-text">This is the sticky footer</p>
  </footer>

</html>

于 2020-09-09T19:41:25.837 に答える
-3

私は多くのプロジェクトで使用してきましたが、単一の問題はありません:)

参考までに、コードはスニペットにあります

* {
    margin: 0;
}
html, body {
    height: 100%;
}
.wrapper {
    min-height: 100%;
    height: auto !important; /* This line and the next line are not necessary unless you need IE6 support */
    height: 100%;
    margin: 0 auto -50px; /* the bottom margin is the negative value of the footer's height */
  background:green;
}
.footer, .push {
    height: 50px; /* .push must be the same height as .footer */
}

.footer{
  background:gold;
  }
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
  <div class="wrapper">
    Content Area
    </div>
  
  <div class="push">
    </div>
  
  <div class="footer">
    Footer Area
    </div>
</body>
</html>

于 2016-12-19T13:09:24.723 に答える