Problem Description I want to use CSS to achieve the top fixed effect: Try to implement margin-top and position:fixed, the code is as follows: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Test</title> <style type="text/css"> .header { position: fixed; height: 20px; width: 100%; } .content { margin-top: 30px; } .aside { float: left; width: 200px; background: orange; } .main { overflow:auto; background: yellow; } </style> </head> <body> <div class="header">123</div> <div class="content"> <div class="aside">aside</div> <div class="main">main</div> </div> </body> </html> As a result, the header is not positioned at the top, but the margin-top distance of the content is vacated: According to the definition of position:fixed, the header has been separated from the document flow and should not be affected by the content layout, but this is not the case. Problem exploration 1. Change margin-top of content to padding-top: the expected effect can be achieved. TBD: Detailed test code and effect diagram will be added later ( ̄∇ ̄)... Summarize It all comes down to the impact of margin-top collapse on position:fixed. First, for a position:fixed element, if top is not specified, its reference origin in the vertical direction is the upper edge of the content of the body box model. If top is specified, its reference origin in the vertical direction is what we often call the upper boundary of the viewport, and the same applies to left and horizontal directions. The reference origin here refers to the reference object when the fixed element is laid out. Once determined, the position of the fixed element will no longer change even if the page is pulled down and the upper boundary of the body moves up. Secondly, because of the margin-top collapse problem, after setting the margin-top of the content, the content part of the body will move down, that is, it will move down with reference to the origin, so the fixed element will leave space for the margin-top. Therefore, this problem can be solved from two aspects: 1. Change the reference origin to the viewport: set the top of the fixed element. 2. Solve the margin-top collapse problem. For more methods, see the link below: 1) Set padding-top to body. Just bear with it for now~I will perfect it after I’m busy with this period (sad face)(sad face). . .
The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. |
<<: Web page printing thin line table + page printing ultimate strategy
>>: Detailed tutorial on building a JMeter+Grafana+Influxdb monitoring platform with Docker
1. Delete folders Example: rm -rf /usr/java The /...
Result: Implementation Code html <div id="...
1. MySQL download address; http://ftp.ntu.edu.tw/...
I was watching Tik Tok some time ago and thought ...
MySQL bidirectional backup is also called master-...
1. Concept They are all attributes of Element, in...
<br />Original text: http://www.mikkolee.com...
netem and tc: netem is a network simulation modul...
Table of contents 1. Use closures 2. Use ES6 clas...
Table of contents MySQL multiple instances Multi-...
Directly to the configuration file server { liste...
GTID-based replication Introduction GTID-based re...
1. In the control panel, uninstall all components...
Table of contents 1. Replace the apply method, ge...
Table of contents Backend: Rails API part Front-e...