代码之家  ›  专栏  ›  技术社区  ›  Jason Christa

使页脚在没有无关标记的情况下不高于屏幕底部

  •  12
  • Jason Christa  · 技术社区  · 14 年前

    如果你只需要担心Firefox和Webkit浏览器,你会用什么CSS来让下面HTML中的页脚不高于底部或屏幕(如果正文内容推了它,页脚会降低)? 我不想在页面中添加任何标记。

    <html>
        <body>
            <header>...</header>
            <article>...</article>
            <aside>...</aside>
            <footer>...</footer>
        </body>
    </html>
    

    <!DOCTYPE html>
    <html>
        <head>
            <style type="text/css">
                * {
                    margin: 0;
                    padding: 0;
                }
    
                ul {
                    list-style: none;
                }
    
                p {
                    margin-bottom: 10px;
                }
    
                article {
                    display: inline-block;
                    height: auto;
                    width: 69%;
                }
    
                aside {
                    display: inline-block;
                    height: auto;
                    width: 30%;
                }
    
                footer {
                    height: 30px;
                }
            </style>
        </head>
        <body> 
            <header>
                <h1>Lorem Ipsum</h1>
            </header> 
            <article>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu viverra mauris. Fusce at erat risus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed tincidunt orci eget justo ornare vel iaculis mauris commodo. Ut id leo ipsum. Donec nunc est, convallis sit amet vehicula eget, laoreet id odio. Proin vitae purus magna. Maecenas lorem lacus, convallis ac imperdiet in, ullamcorper sed leo. Maecenas suscipit justo at arcu placerat eu ultricies orci placerat. Etiam vel erat in metus porttitor tristique vel ultricies ante. Aliquam sed porttitor nunc. Sed venenatis, sapien lacinia laoreet facilisis, lectus turpis iaculis leo, nec rhoncus tellus erat bibendum felis. Integer cursus malesuada sem id vehicula. Duis venenatis pellentesque nisi ut vulputate. Nunc elit sapien, pulvinar blandit suscipit ut, imperdiet ut neque. Cras odio dolor, commodo vitae malesuada sed, tempus sed neque.</p>
                <p>Sed nec ornare libero. Vivamus ut risus at ligula dignissim lobortis. Pellentesque dignissim iaculis fringilla. Quisque porta sagittis massa eu euismod. Vivamus nunc lectus, iaculis vitae tincidunt et, placerat at risus. Nunc elementum massa at ligula blandit quis volutpat nulla malesuada. Nunc felis massa, placerat at vehicula non, gravida a nibh. Fusce adipiscing magna et nisl aliquet vehicula posuere tortor tempor. Aliquam erat volutpat. Duis eu enim sit amet lacus hendrerit elementum vitae a purus.</p>
                <p>Phasellus porttitor congue tellus, eget rhoncus eros consequat a. Donec faucibus lorem at sapien aliquam tempus. Sed sed vulputate magna. Proin eros felis, eleifend vitae posuere vel, dictum ut purus. Pellentesque id felis sit amet neque consectetur porta. Donec non tellus augue, a sollicitudin libero. Nullam blandit hendrerit lacus. Quisque ac libero sapien. Etiam luctus tellus non sapien fringilla ultrices. Aliquam ut erat ut sapien mattis rhoncus nec eu enim. Aenean elementum dui in ligula fermentum nec egestas dui luctus. Praesent sed purus id tellus lacinia aliquam. Donec luctus, metus ut pulvinar bibendum, sapien dui aliquet est, volutpat cursus enim massa non sapien. Quisque mollis nisl a arcu ullamcorper porta. Nunc dapibus pellentesque dui in varius. Donec et eros ut lacus commodo vehicula.</p>
            </article> 
            <aside>
                <ul>
                    <li>One</li>
                    <li>Two</li>
                    <li>Three</li>
                    <li>Four</li>
                    <li>Five</li>
                </ul>
            </aside> 
            <footer>
                <span>Made by me.</span>
            </footer> 
        </body> 
    </html> 
    
    11 回复  |  直到 14 年前
        1
  •  2
  •   ScottS    14 年前

    考虑到不需要额外标记和不关心IE(在IE8中确实有效)的要求,我提出了这个解决方案(它确实需要使用固定高度的头)。我不得不用 float 而不是 display: inline-block 因为我的Safari4.0没有显示它 min-height 对于此解决方案:

       <style type="text/css">
            * {
                margin: 0;
                padding: 0;
            }
    
            html {
                height: 100%;
                }
            body {
                height: 100%;
                /*below for illustration only*/
                background: yellow;
                }
    
            #Header {
                position: relative;
                z-index: 1;
                height: 60px;
                margin-bottom: -60px;
                /*below for illustration only*/
                background: red;
                opacity: .8;
                }
    
            #Article {
                float: left;
                min-height: 100%;
                width: 69.9%;
                vertical-align: top;
                margin-bottom: -30px;               
                /*below for illustration only*/
                background: blue;
            }
    
            #Aside {
                float: left;
                min-height: 100%;
                width: 30%;
                vertical-align: top;
                margin-bottom: -30px;               
                /*below for illustration only*/
                background: green;
            }
    
            #Article:before,
            #Aside:before {
                content: ' ';
                display: block;
                height: 60px;
                width: 100%;
            }
    
            #Article:after,
            #Aside:after {
                content: ' ';
                display: block;
                height: 30px;
                width: 100%;
            }
    
            #Footer {
                position: relative;
                z-index: 1;
                height: 30px;
                margin-top: -30px;
                clear: left;
                /*below for illustration only*/
                background: pink;
                opacity: .8;
            }
        </style>
    

    HTML只是:

    <body>
        <div id="Header">Header</div>
        <div id="Article">Article</div>
        <div id="Aside">Aside</div>
        <div id="Footer">Footer</div>
    </body>
    
        2
  •  1
  •   Taryn user758618    10 年前

    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]--> 
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.1.1/build/cssreset/reset-min.css">
    <style type="text/css">
        html {
            background-color: orange;
            height: 100%;
        }
    
        body {
            background-color: yellow;
            height: auto;
            min-height: 100%;
            position: relative;
        }
    
        ul {
            list-style: none;
        }
    
        p {
            margin-bottom: 10px;
        }
    
        header {
            background-color: red;
            display: block;
        }
    
        article {
            background-color: blue;
            display: inline-block;
            margin-bottom: 30px;
            vertical-align: top;
            width: 70%;
        }
    
        aside {
            background-color: green;
            display: inline-block;
            margin-bottom: 30px;
            vertical-align: top;
            width: 30%;
        }
    
        footer {
            background-color: pink;
            bottom: 0;
            height: 30px;
            left: 0;
            position: absolute;
            width: 100%;
        }
    </style>
    
        3
  •  0
  •   bpeterson76    14 年前

    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格式:

    <html>
        <head>
            <link rel="stylesheet" href="layout.css" ... />
        </head>
        <body>
            <div class="wrapper">
                <div class="header"></div>
                <div class="article"></div>
                <div class="aside"></div>
                <div class="push"></div>
            </div>
            <div class="footer">
                <p>Copyright (c) 2008</p>
            </div>
        </body>
    </html>
    
        4
  •  0
  •   talkingD0G    14 年前

    我想你说的是页脚下10-15像素的边距。尝试将此代码添加到CSS中;

    body { margin-bottom:0px; }
    

    主体的默认边距为10-15像素,因此为了将其移除以将设计刷新到顶部或底部,必须告诉主体边距为零。

    希望能有所帮助。

        5
  •  0
  •   m0g    14 年前

    * 显示:块;

    不过,在此之后,您必须将显示更改为包装器中的inline。

        6
  •  0
  •   Azmisov    14 年前

    首先,不管你做什么,你都需要整个页面的包装div。我通常叫它“页面容器”之类的。如果你仔细想想,拥有一个用于整个页面的div容器并不能战胜css样式表的本质。另一方面,如果在html页面上散布了一堆包装器,那么它可能会因为所有额外的标记而变得非常混乱。所以,我总是在我的布局上使用一个页面容器,即使我没有在它上面添加任何css样式,我也总是有一个。基本上,它只是一个身体标签,只允许你的风格。

    尽管如此,如果您的布局被优化为:

    <html>
    ...
    <body>
    <div id='page_container'>
        <div id='header'></div>
        <div id='contents'></div>
        <div id='footer'></div>
    </div>
    </body>
    </html>
    

    <html>
    <head>
        <title>Test page</title>
            <style>
                body{
                    background-color:green;
                    margin:0px;
                }
                #page_container{
                    width:700px;
                    margin-left:auto;
                    margin-right:auto;
                    display:table;
                    height:100%;
                }
                #header{
                    background-color:red;
                    text-align:center;
                    font-size:25px;
                    font-weight:600;
                    height:75px;
                    display:table-row;
                }
                #contents{
                    background-color:yellow;
                    display:table-row;
                }
                #footer{
                    background-color:blue;
                    height:25px;
                    display:table-row;
                }   
        </style>
    </head>
    <body>
        <div id='page_container'>
            <div id='header'>Title of your page</div>
            <div id='contents'>
                Foo bar baz<br/>
                Foo bar baz<br/>
                Foo bar baz<br/>
                Foo bar baz<br/>
                Foo bar baz<br/>
                Foo bar baz<br/>
            </div>
            <div id='footer'>This is a footer</div>
        </div>
    </body>
    </html>
    

    我一直在尝试一些其他的css样式,我想我几乎有一个布局,可以跨浏览器兼容。如果我不太晚的话,我会寄出去的。

        7
  •  0
  •   Azmisov    14 年前

    <html>
    <head>
        <title>Test page</title>
        <style>
            body{
                margin:0px;
                background-color:green;
                margin-top:75px;
                margin-bottom:25px;
            }
            #page_container{
                max-width:700px;
                margin-left:auto;
                margin-right:auto;
                position:relative;
                height:100%;
                padding-top:75px;
                margin-top:-75px;
            }
            #header{
                background-color:red;
                text-align:center;
                font-size:25px;
                font-weight:600;
                height:75px;
                z-index:2;
                position:absolute;
                top:0px;
                width:100%;
            }
            #contents{
                background-color:yellow;
                width:100%;
                min-height:100%;
            }
            #footer{
                background-color:blue;
                height:25px;
            }    
        </style>
    </head>
    <body>
        <div id='page_container'>
            <div id='header'>Title of your page</div>
            <div id='contents'>
                Foo bar baz<br/>
                Foo bar baz<br/>
                Foo bar baz<br/>
                Foo bar baz<br/>
                Foo bar baz<br/>
                Foo bar baz<br/>
            </div>
            <div id='footer'>This is a footer</div>
        </div>
    </body>
    </html>
    

    哇,如果你稍微调整一下,让它与IE6-7兼容,你就会出名了。我想这是我看到的第一个解决方案。

        8
  •  0
  •   Community Marks    7 年前

    您可以很容易地修改此选项的使用,以便为您工作: Float div to bottom of screen

    那篇文章描述了在任何情况下如何在页面底部设置div。只需把你推下的那个div的页脚。

        9
  •  0
  •   meder omuraliev    14 年前

    http://medero.org/finally.html

    这将使样式适用,但它仍然看起来,至少在IE6中需要一些额外的帮助。

    这接近你的需要吗?

        10
  •  0
  •   Zuul KrapocK    14 年前

    工作和测试:
    谷歌浏览器

    歌剧院
    Internet资源管理器

    已编辑的代码(添加了溢出:auto;to#aside和#Article以修复溢出)

    <!DOCTYPE html>
        <html>
          <head>
            <style type="text/css">
              html,body{height:100%;width:100%;margin:0;padding:0;background-color:red;}
              #Header,#Article,#Aside,#Footer{position:absolute;}
              #Header{top:0;left:0;width:100%;height:60px;background-color:blue;}
              #Article,#Aside{top:60px;overflow:auto;bottom:30px;}
              #Article{left:0;width:70%;background-color:green;}
              #Aside{right:0;width:30%;background-color:yellow;}
              #Footer{bottom:0;left:0;width:100%;height:30px;background-color:pink;}
            </style>
          </head>
          <body>
            <div id="Header">Header</div>
            <div id="Article">Article</div>
            <div id="Aside">Aside</div>
            <div id="Footer">Footer</div>
          </body>
        </html>
    

    背景色是说明性的,因此css可以避免重复的属性,使我更紧凑!

        11
  •  0
  •   Jason Christa    11 年前

    对于那些只需要使用flexbox和viewport单元的浏览器的最新版本的人,另一个答案是。

    <!DOCTYPE html>
    <html>
        <head>
            <style type="text/css">
                body {
                    display: flex;
                    flex-direction: column;
                    min-height: 100vh;
                }
    
                header {
                    height: 60px;
                }
    
                article {
                    flex: 1;
                }
    
                footer {
                    height: 30px;
                }
            </style>
        </head>
        <body> 
            <header>
                <h1>Lorem Ipsum</h1>
            </header> 
            <article>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu viverra mauris. Fusce at erat risus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed tincidunt orci eget justo ornare vel iaculis mauris commodo. Ut id leo ipsum. Donec nunc est, convallis sit amet vehicula eget, laoreet id odio. Proin vitae purus magna. Maecenas lorem lacus, convallis ac imperdiet in, ullamcorper sed leo. Maecenas suscipit justo at arcu placerat eu ultricies orci placerat. Etiam vel erat in metus porttitor tristique vel ultricies ante. Aliquam sed porttitor nunc. Sed venenatis, sapien lacinia laoreet facilisis, lectus turpis iaculis leo, nec rhoncus tellus erat bibendum felis. Integer cursus malesuada sem id vehicula. Duis venenatis pellentesque nisi ut vulputate. Nunc elit sapien, pulvinar blandit suscipit ut, imperdiet ut neque. Cras odio dolor, commodo vitae malesuada sed, tempus sed neque.</p>
                <p>Sed nec ornare libero. Vivamus ut risus at ligula dignissim lobortis. Pellentesque dignissim iaculis fringilla. Quisque porta sagittis massa eu euismod. Vivamus nunc lectus, iaculis vitae tincidunt et, placerat at risus. Nunc elementum massa at ligula blandit quis volutpat nulla malesuada. Nunc felis massa, placerat at vehicula non, gravida a nibh. Fusce adipiscing magna et nisl aliquet vehicula posuere tortor tempor. Aliquam erat volutpat. Duis eu enim sit amet lacus hendrerit elementum vitae a purus.</p>
                <p>Phasellus porttitor congue tellus, eget rhoncus eros consequat a. Donec faucibus lorem at sapien aliquam tempus. Sed sed vulputate magna. Proin eros felis, eleifend vitae posuere vel, dictum ut purus. Pellentesque id felis sit amet neque consectetur porta. Donec non tellus augue, a sollicitudin libero. Nullam blandit hendrerit lacus. Quisque ac libero sapien. Etiam luctus tellus non sapien fringilla ultrices. Aliquam ut erat ut sapien mattis rhoncus nec eu enim. Aenean elementum dui in ligula fermentum nec egestas dui luctus. Praesent sed purus id tellus lacinia aliquam. Donec luctus, metus ut pulvinar bibendum, sapien dui aliquet est, volutpat cursus enim massa non sapien. Quisque mollis nisl a arcu ullamcorper porta. Nunc dapibus pellentesque dui in varius. Donec et eros ut lacus commodo vehicula.</p>
            </article> 
            <footer>
                <span>Made by me.</span>
            </footer> 
        </body> 
    </html>