LoginSignup
45
46

More than 5 years have passed since last update.

【HTML】footerを常に最下部にする方法

Posted at

今回はfooterを常に最下部に表示させる方法です。

これまたよくある話だけど、いつもスムーズにできずに時間がかかってしまう。

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
    html, body {
        width: 100%;
        height: 100%;
        margin: 0;
    }
    #container {
        position: relative;
        width: 100%;

        /* コンテンツ部分の高さが画面に収まらない場合はコンテンツの高さに合わせる*/
        height: auto !important;

        /* コンテンツ部分の高さが画面より低い場合は画面の高さに合わせる*/
        height: 100%;
        min-height: 100%;
        background-color: blue;
    }
    #header {
        width: 100%;
        height: 40px;
        background-color: green;
    }
    #contents {
        width: 100%;
        height: 100px;
        background-color: yellow;
        padding-bottom: 40px;
    }
    #footer {
        /* containerからの絶対位置を定義 */
        position: absolute;
        bottom: 0;
        width: 100%;
        height: 40px;
        background-color: red;
    }
    </style>
</head>
<body>
    <div id="container">
        <div id="header">header</div><!-- /#header-->
        <div id="contents">contents</div><!-- /#contents-->
        <div id="footer">footer</div><!-- /#footer -->
    </div><!-- /#container-->
</body>
</html>
45
46
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45
46