Background Many website designs generally consist of two parts: content + footer. The content contains the main content of the website, and the footer displays the website's registration information, etc. Due to the uncertainty of the website's content height, the following two situations may occur: 1. When there is less content, the footer is fixed at the bottom of the page. As shown below: 2. When the content is long, the footer slides behind the content, as shown in the red box in the following figure: This requirement is still very common on the PC side. I also encountered this problem in my own application. Today I summarized several methods to achieve this layout. Method 1: Use js to calculate Why did I use js control first? To be honest, when I first encountered this problem, I directly used js to solve it (mainly because I knew js could definitely achieve it, so I didn’t spend time thinking of other methods) The main idea is: after the page is loaded, calculate the screen height - the actual height of the content. If the difference is greater than To determine the height of the footer, add a fixed position to the footer style to fix it at the bottom of the screen. The demo code is as follows: <!DOCTYPE html> <html> <head> <title>footer bottom effect</title> <style type="text/css"> div { margin: 0, padding: 0; box-sizing: border-box; position: relative; } html, body { width: 100%; height: 100%; } #container { width: 100%; height: 100%; } #content { background: blue; } #footer { width: 100%; height: 100px; background: red; } .footer-fixed { position: fixed; left: 0; bottom: 0; } </style> </head> <body> <div id="container"> <div id="content"> content </div> <div id="footer"> footer </div> </div> <script type="text/javascript"> let height = document.getElementById('container').clientHeight - document.getElementById('content').clientHeight; // Here we add another class to the footer to make it fixed if (height > 100) document.getElementById('footer').classList.add('footer-fixed'); </script> </body> </html> Based on the principle of never using js if CSS can solve the problem, although this method is the easiest to think of, it is still not recommended. Moreover, this CSS code needs to obtain the clientHeight, which will cause the page to be reflowed and redrawn. From a performance perspective, it is not recommended. Method 2: Use flex layout + min-height justify-content: space-between in flex layout; with the super useful min-height, it can just meet the effect of sinking the footer when the content is insufficient The demo code is as follows: <!DOCTYPE html> <html> <head> <title>footer bottom effect</title> <style type="text/css"> div { margin: 0; padding: 0; box-sizing: border-box; position: relative; } html, body { width: 100%; height: 100%; margin: 0; padding: 0; } #container { width: 100%; // Key code // Although the height of the container is unknown, a minimum height can be set, which is beneficial to the layout min-height: 100%; display: flex; flex-direction: column; justify-content: space-between; } #content { background: blue; } #footer { width: 100%; height: 100px; background: red; } </style> </head> <body> <div id="container"> <div id="content"> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> </div> <div id="footer"> footer </div> </div> </body> </html> Min-height is really a very useful CSS attribute. It can easily achieve the bottoming effect when combined with flex. Method 3: Use flex + margin-top This trick was learned when talking about the wonderful uses of margin auto. In the flex formatting context, margin auto will automatically distribute the remaining space. Here we can use margin-top:auto on the footer to achieve the bottoming effect. <!DOCTYPE html> <html> <head> <title>footer bottom effect</title> <style type="text/css"> div { margin: 0; padding: 0; box-sizing: border-box; position: relative; } html, body { width: 100%; height: 100%; margin: 0; padding: 0; } #container { width: 100%; min-height: 100%; display: flex; flex-direction: column; } #content { background: blue; } #footer { width: 100%; height: 100px; background: red; margin-top: auto; // key code} </style> </head> <body> <div id="container"> <div id="content"> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> </div> <div id="footer"> footer </div> </div> </body> </html> Summary: The above three methods have no side effects. In fact, there are other ways to achieve this sinking effect, but they will affect other layouts. I will not go into details here. I will update it later when there is a better solution. |
<<: Let's talk in detail about the props attributes of components in Vue
>>: Display ellipsis effect when table cell content exceeds (implementation code)
1. Check whether the MySQL service is started. If...
1. Back button Use history.back() to create a bro...
Starting from MySQL 5.7, many security updates ha...
Table of contents 1. System environment 2. Operat...
This article shares the specific code of vue elem...
You must have inspiration to design a website. Goo...
This article shares the installation tutorial of ...
Recently I changed Apache to nginx. When I moved ...
MySQL regular sorting, custom sorting, and sortin...
The picture is used as the background and the lin...
Simple function: Click the plug-in icon in the up...
React Lifecycle Two pictures to help you understa...
The code looks like this: <!DOCTYPE html> &...
1. Installation apt-get install mysql-server requ...
Lock classification: From the granularity of data...