There is a basic <div class="container"> block with various content, and a footer is placed under it.

If there is no content or it does not take up enough space, you need to make the minimum height .container 100% of the browser window. In this case, the footer should be at the bottom of the page and remain visible.

The footer can change the height, but at the same time it still has to remain pressed to the bottom of the browser window.

 .container { background: #fff; margin: auto; height: auto; width: 900px; } 
 <body> <header> header </header> <main class="container"> content </main> <footer> footer </footer> </body> 

  • This is a duplicate question , and not vice versa. - Vadim Ovchinnikov
  • I understand that you should constantly change the content, depending on the selected tabs in some menu. I also came across moments when it was necessary for the footer to be clearly below with practically empty content (even scrolling a little). I decided this moment bonalno simple: in my css file I put. Content {min-height: 600px; } (well, or 500-700px, depending on how high your head is, just calculate the content). I don’t say that this solution is original, but in my projects I used it and it didn’t give me any inconvenience in the future. - Alexander

7 answers 7

In order to always press the footer to the bottom of the screen, there are several solutions:

1. Solution through absolute positioning for a fixed footer height

 html { /* Растягиваем документ на всю высоту окна */ height: 100%; } body { position: relative; margin: 0; color: #fff; /* Растягиваем body по высоте html */ min-height: 100%; } header { background: blue; } main { /* Выставляем отступ с высотой footer */ padding-bottom: 30px; background: red; } footer { /* Позиционируем footer внизу main */ position: absolute; bottom: 0; width: 100%; /* Высота footer */ height: 30px; background: black; } 
 <header role="banner"> header </header> <main role="main"> content </main> <footer role="contentinfo"> footer </footer> 

2. Solution via Flexbox for adaptive footer height

 body { margin: 0; color: #fff; display: flex; flex-direction: column; min-height: 100vh; } header { background: blue; } main { /* Чтобы занимал оставшееся пространство */ flex-grow: 1; background: red; } footer { /* Чтобы footer не уменьшался */ flex-shrink: 0; background: black; } 
 <header role="banner"> header </header> <main role="main"> content </main> <footer role="contentinfo"> footer </footer> 

3. Solution through tables for adaptive footer height

 body { margin: 0; color: #fff; display: table; min-height: 100vh; width: 100%; } header { background: blue; } main { display: table-row; /* Чтобы ряд занимал всё оставшееся пространство, так как табличная разметка не позволит ему вытолкнуть header и footer */ height: 100%; background: red; } footer { background: black; } 
 <header role="banner"> header </header> <main role="main"> content </main> <footer role="contentinfo"> footer </footer> 

4. Solution in native javascript for adaptive footer height

 // Высчитываем высоту footer и делаем соответствующий отступ от main: function footer(){ var main = document.getElementsByTagName('main')[0], footer = document.getElementsByTagName('footer')[0]; footerHeight = footer.clientHeight; main.style.paddingBottom = (footerHeight)+'px'; } window.addEventListener('load',footer); window.addEventListener('resize',footer); 
 html { /* Растягиваем документ на всю высоту окна */ height: 100%; } body { position: relative; margin: 0; color: #fff; /* Растягиваем body по высоте html */ min-height: 100%; } header { background: blue; } main { /* Выставляем отступ с высотой footer по умолчанию */ padding-bottom: 30px; background: red; } footer { /* Позиционируем footer внизу main */ position: absolute; bottom: 0; width: 100%; /* Высота footer по умолчанию */ height: 30px; background: black; } 
 <header role="banner"> header </header> <main role="main"> content </main> <footer role="contentinfo"> footer </footer> 

5. Solution via calc () for a fixed footer height

 html { /* Растягиваем документ на всю высоту окна */ height: 100%; } body { position: relative; margin: 0; color: #fff; /* Растягиваем body по высоте html */ min-height: 100%; } header { background: blue; } main { /* Выставляем отступ с высотой footer и header */ min-height: calc(100vh - 30px - 18px); background: red; } footer { /* Высота footer */ height: 30px; background: black; } 
 <header role="banner"> header </header> <main role="main"> content </main> <footer role="contentinfo"> footer </footer> 

6. Solution via Grid Layout for fixed footer height

 html { height: 100%; } body { margin: 0; color: #fff; /* Растягиваем body по высоте html */ min-height: 100%; display: grid; grid-template-rows: [row1] 18px [row2] 1fr [row3] 30px; grid-template-areas: "header" "main" "footer"; } header { background: blue; grid-area: header; } main { background: red; grid-area: main; } footer { background: black; grid-area: footer; } 
 <header role="banner"> header </header> <main role="main"> content </main> <footer role="contentinfo"> footer </footer> 

  • If you need a solution on Flexbox to work in IE, then I have a solution in my answer in the Internet Explorer section. - Vadim Ovchinnikov
  • Also the above solution on CSS Grid Layout is inflexible (requires a fixed height) and does not work in IE 10/11, which is fixed in my answer. - Vadim Ovchinnikov

Solution on flexbox .

More versatile in terms of maintainability, since it does not require the specification of a fixed height and hacks with position: absolute . If you also need to support IE 10/11, then the solution including their support is located in the "Flexbox for Internet Explorer 10/11" section.

The case of content for the rest of the height:

 body { min-height: 100vh; margin: 0; display: flex; flex-direction: column; } .content { /* Занять всё оставшееся пространство */ flex-grow: 1; border: 1px dotted red; } 
 <header> header </header> <div class="content"> content </div> <footer> footer </footer> 

If you need to be indented from the content, but the content does not occupy the full height, then use margin-top: auto for footer :

 body { min-height: 100vh; margin: 0; display: flex; flex-direction: column; } .content { border: 1px dotted red; } footer { /* Опустить в самый низ */ margin-top: auto; } 
 <header> header </header> <div class="content"> content </div> <footer> footer </footer> 

Flexbox for Internet Explorer 10/11

In order for the methods described above to work in IE 10/11, you need to remember the following browser bugs:

  • min-height does not apply to an element with display: flex and flex-direction: column in IE 10-11. Use height where possible.

  • Chrome, Opera, and Safari do not take into account the minimum size of the content of direct children of the container c display: flex . Set flex-shrink to 0 (instead of defaults to 1 ) to avoid unwanted "compression".

The case of content for the rest of the height:

 body { height: 100vh; margin: 0; display: flex; flex-direction: column; } header, footer { flex-shrink: 0; } .content { /* Занять всё оставшееся пространство */ flex-grow: 1; border: 1px dotted red; } 
 <header> header </header> <div class="content"> content </div> <footer> footer </footer> 

If you need to be indented from the content:

 body { height: 100vh; margin: 0; display: flex; flex-direction: column; } header, footer { flex-shrink: 0; } .content { border: 1px dotted red; } footer { /* Опустить в самый низ */ margin-top: auto; } 
 <header> header </header> <div class="content"> content </div> <footer> footer </footer> 

CSS grid layout

The case of content for the rest of the height:

 body { height: 100vh; margin: 0; display: grid; /* задаём высоту строк */ grid-template-rows: auto 1fr auto; } .content { border: 1px dotted red; } 
 <header> header </header> <div class="content"> content </div> <footer> footer </footer> 

If you need to be indented from the content, but the content does not occupy the full height, then use align-self: start for footer :

 body { min-height: 100vh; margin: 0; display: grid; /* задаём высоту строк */ grid-template-rows: auto 1fr auto; } .content { /* кладём элемент в начало ячейки по вертикали */ /* чтобы его высота была равна высоте контента */ align-self: start; border: 1px dotted red; } 
 <header> header </header> <div class="content"> content </div> <footer> footer </footer> 

CSS Grid Layout for Internet Explorer 10/11

Internet Explorer 10/11 features an outdated version of the CSS Grid Layout module. From a practical point of view, this means that the implementation of this module is very different from other browsers that support this module.

The most important thing to keep in mind is that in IE, the elements are not arranged by default in free cells in order, but simply placed on top of each other in the very first cell. That is, in other browsers, the default values ​​are grid-row : auto and grid-column: auto , whereas in IE, -ms-grid-row : 1 and -ms-column: 1 , so if the line number or column number of the element does not match 1 , then you have to prescribe them differently.

The rest is more trivial. Some properties are not supported (like grid-areas , grid-gap , etc.), some are named differently or different syntax is required and may have excellent defaults.

The case of content for the rest of the height:

 body { height: 100vh; margin: 0; display: -ms-grid; display: grid; /* по умолчанию в IE размер ячеек равен содержимому, */ /* тогда как в других браузерах размер ячеек растягивается */ /* под свобоодное пространство grid-контейера */ -ms-grid-columns: 100%; /* задаём высоту строк */ -ms-grid-rows: auto 1fr auto; grid-template-rows: auto 1fr auto; } .content { /* явно прописываем номер строки, когда она не равна 1 */ -ms-grid-row: 2; border: 1px dotted red; } footer { /* явно прописываем номер строки, когда она не равна 1 */ -ms-grid-row: 3; } 
 <header> header </header> <div class="content"> content </div> <footer> footer </footer> 

If you need to be indented from the content, but the content does not occupy the full height, then use align-self: start and -ms-grid-row-align: start for footer :

 body { height: 100vh; margin: 0; display: -ms-grid; display: grid; /* по умолчанию в IE размер ячеек равен содержимому, */ /* тогда как в других браузерах размер ячеек растягивается */ /* под свобоодное пространство grid-контейера */ -ms-grid-columns: 100%; /* задаём высоту строк, разные имена свойств */ -ms-grid-rows: auto 1fr auto; grid-template-rows: auto 1fr auto; } .content { /* явно прописываем номер строки, когда она не равна 1 */ -ms-grid-row: 2; /* кладём элемент в начало ячейки по вертикали */ /* чтобы его высота была равна высоте контента */ -ms-grid-row-align: start; align-self: start; border: 1px dotted red; } footer { /* явно прописываем номер строки, когда она не равна 1 */ -ms-grid-row: 3; } .content { -ms-grid-row: 2; border: 1px dotted red; } footer { -ms-grid-row: 3; } 
 <header> header </header> <div class="content"> content </div> <footer> footer </footer> 

    You can still with verification (if there is little content, then fix, otherwise static).

    a lot of content:

     if( $(document).height() <= $(window).height() ){ $(".page-footer").addClass("fixed-bottom"); } 
     .page-footer { padding: 1rem; background: #333; color: #fff; text-align: center; } .page-footer.fixed-bottom { position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; } 
     <script src="https://code.jquery.com/jquery-2.1.4.js"></script> <main> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Velit, harum. Sapiente dignissimos in provident fugit voluptatem commodi, ipsa blanditiis assumenda quasi amet excepturi nostrum voluptatum molestiae ratione, corrupti hic voluptatibus. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum accusamus cum voluptas voluptate esse asperiores cupiditate velit quaerat optio, praesentium ipsa, deserunt veniam facilis libero accusantium! Similique accusamus assumenda beatae amet harum delectus quisquam minima quidem id veniam a eaque iste labore distinctio quia cupiditate, ullam suscipit. Repellendus, porro, officiis! </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vero atque officia, hic iure placeat, dolores amet eaque quae, eveniet laboriosam voluptatibus fugit velit aut. Facilis expedita, id quasi asperiores molestiae, numquam provident consectetur maxime ad dolorem illo, voluptas dolore accusantium quam deleniti enim ratione doloremque cum omnis ea maiores, deserunt earum eveniet minima eaque. Soluta earum amet esse rem vitae eaque enim aut obcaecati laudantium provident eius delectus nulla doloremque omnis quisquam, ut eos modi, autem tenetur! Deserunt pariatur cum aspernatur aperiam, obcaecati libero, tenetur veritatis aut praesentium architecto optio perspiciatis quo ut. Atque, soluta doloribus recusandae quibusdam ipsam qui! </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id, unde. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus perspiciatis molestiae nemo soluta nesciunt alias porro impedit, perferendis molestias possimus mollitia asperiores laboriosam consectetur enim odit, animi facere earum consequatur in veniam neque quae esse. Beatae iure laboriosam optio? Pariatur. </p> </main> <footer class="page-footer"> FOOTER </footer> 

    little content:

     if( $(document).height() <= $(window).height() ){ $(".page-footer").addClass("fixed-bottom"); } 
     .page-footer { padding: 1rem; background: #333; color: #fff; text-align: center; } .page-footer.fixed-bottom { position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; } 
     <script src="https://code.jquery.com/jquery-2.1.4.js"></script> <main> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Velit, harum. Sapiente dignissimos in provident fugit voluptatem commodi, ipsa blanditiis assumenda quasi amet excepturi nostrum voluptatum molestiae ratione, corrupti hic voluptatibus. </p> </main> <footer class="page-footer"> FOOTER </footer> 

      Once 5 I read the question and am not sure that I understood what the author wants, but still. If you want div behave differently on different screens, you can specify in css :

       @media all and (min-width: 600px) { .container { /* задавай свойства какие тебе надо */ } .можно_дописать_еще_класс_других_div_или_чего_тебе_хочется { } } 

      And if you want the blocks to behave correctly on different screens and you don’t need to flip the page left and right, read http://frontender.info/a-guide-to-flexbox/ . Everything is very simple.

        Perhaps the most compact solution on css via calc () with the addition of a whole pair of min-height and height properties

         main { min-height: calc(100vh - 70px); } footer { height: 50px; background: gray; } 
         <body> <main> <header>header</header> content </main> <footer>footer</footer> </body> 

          7. Solution through grid layout for ANY footer height

          This option is much more useful than # 6, since both the header and footer can be of any height — for their content and code are noticeably less — only 2 lines are needed. Use if writing using Grid Layout.

           html { height: 100%; } body { display: grid; grid-template-rows: auto 1fr auto; margin: 0; color: #fff; min-height: 100%; } header { background: blue; } main { background: red; } footer { background: black; } 
           <header role="banner"> header<br>header </header> <main role="main"> content </main> <footer role="contentinfo"> footer<br>footer<br>footer </footer> 

            Apply min-height: 95%; for .container, and height: 5%; - for .footer

            • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky