Pin the footer of an html page and follow the page content

Asked

Viewed 47,612 times

8

I need to pin the tag footer at the end of the html page, but when the content exceeds the page by tag footer should follow the content and not be fixed.

See the CSS code is the HTML code I’m using for this.

    header {
        min-height: 55px; /* original era 255px mudei para caber no snippet */
        background-color: blue;
    }
    article {
        padding-bottom: 60px; 
        width: 900px; 
        margin: 0 auto;
    }
    footer {
        position: absolute; 
        bottom: 0px; 
        width: 100%; 
        height: 60px; 
        background-color: green;
    }
<header></header>

<article>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>    
</article>

<footer></footer>
</body>
</html>

The problem is that the tag footer with the property position with the value fixed leaves the footer fixed.

When I change the property position for absolute the tag footer is floating and does not follow the content.

4 answers

10


To browsers recent you can add this to your CSS:

html {
    position: relative;
    min-height: 100%;
}
body {
    margin: 0 0 60px; /* altura do seu footer */
}

According to the creator of the solution, functiona with: IE8+, Chrome / Safari, Firefox and Opera.

The biggest advantage of this solution over the others is that you do not need to create Wrappers around its contents. It uses its own body as wrapper.

Example: Com muito conteúdo - Sem muito conteúdo

(The same code was used for the two examples, only the content that changes.)

4

Da to do using flex in the body.

First you need to define the body with display: flex, then place the order as column, so that the "orientation" of the grid is vertical (more or less like the standard behavior of divs). Then in the main that will receive the content you put flex-grow: 1; and puts position:sticky in nav for it to stay fixed at the top. Thus the main will always occupy all available space, "pushing" the footer always at the bottom of the page regardless of the amount of content.

inserir a descrição da imagem aqui

See the code used to get the image result above:

Exipa also as "Página toda" to see better the result!

html, body {
	width: 100%;
	height: 100%;
	margin: 0;
	padding: 0;
}
body {
	display: flex;
	flex-direction: column;
	background-color: #ddd;
}
nav, 
footer {
	width: 100%;
	min-height: 50px;
	background-color: #f00;
	text-align: center;
	color: #ddd;
}
nav {
	position: sticky;
	top: 0;
}
main {
	width: 100%;
	background-color: turquoise;
	flex-grow: 1;
}
<nav>NAV</nav>
<main>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tenetur voluptates reprehenderit magni molestias iure unde id minima voluptatem laudantium ipsum. Nam reprehenderit iusto consectetur ipsum natus doloremque laudantium illum quod delectus perferendis! Quam explicabo laudantium suscipit reprehenderit harum quo nostrum porro fugit natus. Eius sequi odit sunt, aut eveniet similique dicta mollitia reprehenderit incidunt, molestiae quod amet esse facere vero perspiciatis dignissimos voluptatum eligendi laborum dolorum asperiores inventore, ipsum est. Obcaecati dolores perspiciatis dignissimos. Consequuntur soluta quaerat, asperiores sed earum non impedit. Tempora dicta dolorum quas id asperiores laboriosam delectus velit perferendis veniam blanditiis ea exercitationem ullam, ab consequuntur incidunt ipsa est voluptate suscipit cumque non ipsum praesentium minima aliquam. Tempore ratione dolore aut saepe sint. Eos vel facilis minima nostruoloremque reprehenderit nisi sunt inventore maxime quisquam quaerat, totam perspiciatis. Quidem in sint debitis perspiciatis labore, eaque nam quis doloremque blanditiis ipsum architecto temporibus sunt tempore voluptates? Voluptatem, dolorum unde ut facilis quasi eligendi officia earum in. Nemo impedit pariatur itaque soluta non minus. Minima vel maiores aliquid officiis accusamus optio excepturi praesentium in doloremque, similique necessitatibus! Iure asperiores necess nemo ipsa laborum velit optio 
</main>
<footer>FOOTER</footer>

OBS: The Flex works from IE10 up https://caniuse.com/#search=flex aténde

-2

A simple and responsive solution would be like this.

footer{
position: relative;
background-color: red;
width:100%;
height:48px;
left:0;
bottom:0;
}
<header></header>

<article>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p> 
</article>

<footer></footer>
</body>
</html>

  • 1

    If there is little content on the page, the footer will not be fixed at the bottom: https://jsfiddle.net/b8gze2ta/

  • Add to css left: 0; bottom: 0; Like this: footer{&#xA;position: absolute;&#xA;background-color: red;&#xA;width:100%;&#xA;height:48px;&#xA;left: 0;&#xA;bottom: 0;&#xA;}

-2

In order for Case to have little content on the page not to scroll to follow the content use {left: 0; bottom: 0;}

footer{
position: relative;
background-color: red;
width:100%;
height:48px;
}
<header></header>

<article>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>
  <p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p><p>Conteúdo</p>    
</article>

<footer></footer>
</body>
</html>

  • Your code is not cool, just you run there and make a scroll you will see that Footer is in the middle of the page and the content continues after it

  • It was because I forgot to adjust the position. When the content is large do not use the left: or bottom. When Content is short or short use them like this left:0; bottom:0 positon must be relative

Browser other questions tagged

You are not signed in. Login or sign up in order to post.