CSS: fixed to bottom and centered

前端 未结 9 1273
南笙
南笙 2020-11-28 04:33

I need my footer to be fixed to the bottom of the page and to center it. The contents of the footer may change at all time so I can\'t just center it via margin-left: xxpx;

相关标签:
9条回答
  • 2020-11-28 04:46

    here is an example using css grid.

    html, body{
        height: 100%;
        width: 100%;
    }
    .container{
        height: 100%;
        display:grid;
        /*we divide the page into 3 parts*/
        grid-template-rows: 20px auto  30px;
        text-align: center;   /*this is to center the element*/ 
        
    }
    
    .container .footer{
        grid-row: 3;   /*the footer will occupy the last row*/
        display: inline-block;
        margin-top: -20px;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="style.css">
        <title>Document</title>
    </head>
    <body>
        <div class="container">
            <div class="header">
    
            </div>
            <div class="content">
    
            </div>
            <div class="footer">
                here is the footer
            </div>
        </div>
    </body>
    </html>

    you can use css grid:a concrete example

    0 讨论(0)
  • 2020-11-28 04:48

    revised code by Daniel Kanis:

    just change the following lines in CSS

    .problem {text-align:center}
    .enclose {position:fixed;bottom:0px;width:100%;}
    

    and in html:

    <p class="enclose problem">
    Your footer text here.
    </p>
    
    0 讨论(0)
  • 2020-11-28 04:50

    A jQuery solution

    $(function(){
        $(window).resize(function(){
            placeFooter();
        });
        placeFooter();
        // hide it before it's positioned
        $('#footer').css('display','inline');
    });
    
    function placeFooter() {    
        var windHeight = $(window).height();
        var footerHeight = $('#footer').height();
        var offset = parseInt(windHeight) - parseInt(footerHeight);
        $('#footer').css('top',offset);
    }
    
    <div id='footer' style='position: fixed; display: none;'>I am a footer</div>
    

    Sometimes it's easier to implement JS than to hack old CSS.

    http://jsfiddle.net/gLhEZ/

    0 讨论(0)
  • 2020-11-28 04:51

    I ran into a problem where the typical position: fixed and bottom: 0 didn't work. Discovered a neat functionality with position: sticky. Note it's "relatively" new so it won't with IE/Edge 15 and earlier.

    Here's an example for w3schools.

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    div.sticky {
      position: sticky;
      bottom: 0;
      background-color: yellow;
      padding: 30px;
      font-size: 20px;
    }
    </style>
    </head>
    <body>
    
    <p>Lorem ipsum dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est,  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dlerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dlerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dlerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dlerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas  dolor nteger frinegestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas </p>
    
    <div class="sticky">I will stick to the screen when you reach my scroll position</div>
    
    </body>
    </html>

    0 讨论(0)
  • 2020-11-28 04:53

    There are 2 potential issues that I see:

    1 - IE has had trouble with position:fixed in the past. If you are using IE7+ with a valid doctype or a non-IE browser this isn't part of the problem

    2 - You need to specify a width for the footer if you want the footer object to be centered. Otherwise it defaults to the full width of the page and the auto margin for the left and right get set to 0. If you want the footer bar to take up the width (like the StackOverflow notice bar) and center the text, then you need to add "text-align: center" to your definition.

    0 讨论(0)
  • 2020-11-28 05:03

    I have encased the 'problem div in another div' lets call this div the enclose div... make the enclose div in css have a width of 100% and postion fixed with a bottom of 0... then insert the problem div into the enclose div this is how it would look

    #problem {margin-right:auto;margin-left:auto; /*what ever other styles*/}
    #enclose {position:fixed;bottom:0px;width:100%;}
    

    then in html...

    <div id="enclose">
        <div id="problem">
        <!--this is where the text/markup would go-->
        </div>
    </div>
    

    There ya go!
    -Hypertextie

    0 讨论(0)
提交回复
热议问题