Home > Enterprise >  How can I make a <div> class stick to the bottom of my page in HTML?
How can I make a <div> class stick to the bottom of my page in HTML?

Time:11-26

I want to add a footer to my website, and I want to be attached to the bottom of my screen, how can I do that?

I have tried this:

  .footer {
    position: absolute;
    width: 100%;
    background: #0084FF;
    color: #fff;
    padding: 25px;
    margin-top: 0px;
    margin: 0px;
    box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
    z-index: 99999;
  }

My issue is, if I do this, it will stick to the bottom, but a section of what I have on my div (on the right side) will be off my screen, if I remove the padding, it will look really bad. Is there any other way of doing this?

NOTE: The HTML code for the div I think isn't needed, since there isn't anything special with the HTML code section of my footer.

CodePudding user response:

Did you try with box-sizing: border-box;?
See documentation: https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

Example below (use the [<>] button to create a working example in your post!).

I used position: fixed; and bottom: 0; here to make the example look good, but use your own way of course.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

p {
  margin-bottom: 20px;
}

.body {
  width: 100%;
  height: 100%;
  padding: 15px;
}

.footer {
  position: fixed;
  bottom: 0;
  width: 100%;
  background: #0084FF;
  color: #fff;
  padding: 25px;
  margin-top: 0px;
  margin: 0px;
  box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
  z-index: 99999;
}
<div class="footer">Footer</div>
<div class="body">
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</p>

  <p>Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.</p>

  <p>Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.</p>

  <p>Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante.</p>

  <p>Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus.</p>

  <p>Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu. Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris.</p>

  <p>Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestibulum volutpat pretium libero. Cras id dui. Aenean ut eros et nisl sagittis vestibulum. Nullam nulla eros, ultricies sit amet, nonummy id, imperdiet feugiat, pede. Sed lectus.</p>

  <p>Donec mollis hendrerit risus. Phasellus nec sem in justo pellentesque facilisis. Etiam imperdiet imperdiet orci. Nunc nec neque. Phasellus leo dolor, tempus non, auctor et, hendrerit quis, nisi. Curabitur ligula sapien, tincidunt non, euismod vitae, posuere imperdiet, leo. Maecenas malesuada. Praesent congue erat at massa. Sed cursus turpis vitae tortor. Donec posuere vulputate arcu. Phasellus accumsan cursus velit. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed aliquam, nisi quis porttitor congue, elit erat euismod orci, ac</p>
</div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can try position fixed or sticky:

I used fixed. Here's the code, I gave some styling and the positioning

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        footer{
            width: 100%;
            background-color: rgb(231, 0, 0);
            position: fixed;
            bottom: 0;
            right: 0;
        }
    </style>
</head>
<body>
   <!-- Put in text or other code -->


    <footer>
        Copyright lorem.com  &copy;   
     </footer>
</body>
</html>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Solution:

If you want the footer to stick at the bottom then you must have bottom: 0; for your footer. Also, position: absolute; will only keep the footer till the end of its parent container so if you want the footer to stay at the bottom even while the user is scrolling, try using position: fixed; or position: sticky;.

Code

.scroll{

<!-- this is present here only to show you the scrolling mechanism -->

       margin-top: 100px;
       width:100%;
       height:300vh;
       background: #ff0000;
}

.footer{
       position: fixed;
       bottom: 0;
       background: #000;
       color: #fff;
       min-height: 50px;
       width: 100vw
}
<div class="scroll">...</div>
<div class="footer">This is a footer</div>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can use bottom: 0 (MDN Web Docs reference for more info) to push the footer to the bottom and use position: fixed or position: sticky (MDN Web Docs reference for more info) to make the footer stay at the bottom. Here's a working example :

body {
    background-color: gold;
    margin: 0;
}

.content {
    <!-- Added this to show you that you can scroll and the footer stays at the bottom -->
    height: 1500px;
}

footer {
    position: fixed;
    bottom: 0;
    width: 100vw;
    background: #0084FF;
    color: #fff;
    padding: 25px;        
}
<div class="content"></div>
<footer>Footer</footer>  
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related