Heim >Web-Frontend >H5-Tutorial >用CSS固定footer在底部的疑问?
<code class="language-html"><span class="nt"></span>
<span class="nt"></span>
<span class="nt"><style><span class="na">type=</style></span><span class="s">"text/css"</span><span class="nt">></span>
<span class="nt">html</span><span class="o">,</span><span class="nt">body</span><span class="p">{</span><span class="k">height</span><span class="o">:</span><span class="m">100%</span><span class="p">}</span>
<span class="nc">.footer</span> <span class="p">{</span><span class="k">margin-top</span><span class="o">:-</span><span class="m">30px</span><span class="p">;</span><span class="k">height</span><span class="o">:</span><span class="m">30px</span><span class="p">;</span><span class="k">background-color</span><span class="o">:</span><span class="m">#eee</span><span class="p">;}</span>
<span class="nc">.wrap</span><span class="p">{</span><span class="k">min-height</span><span class="o">:</span><span class="m">100%</span><span class="p">}</span>
<span class="nc">.main</span><span class="p">{</span><span class="k">padding-bottom</span><span class="o">:</span><span class="m">30px</span><span class="p">;</span><span class="k">overflow</span><span class="o">:</span><span class="k">hidden</span><span class="p">;}</span>
<span class="nt"></span>
<span class="nt"></span>
<span class="nt"></span>
<span class="nt"><div> <span class="na">class=</span><span class="s">"wrap"</span><span class="nt">></span>
<span class="nt"><div> <span class="na">class=</span><span class="s">"main"</span><span class="nt">></span>这里是网页的主体<span class="nt"></span>
</div></span>
<span class="nt"></span>
</div></span>
<span class="nt"><div> <span class="na">class=</span><span class="s">"footer"</span><span class="nt">></span>这里是页脚<span class="nt"></span>
</div></span>
<span class="nt"></span>
<span class="nt"></span>
</code>
- 对于一个非根元素,如果其position值是relative或static(默认),包含块的设置为最近的块级框、表单元格或行内块祖先框的内容边界构成。
- 对于一个非根元素,如果其position值是absolute,包含块则由最近的position值不是static的祖先元素(可以是任何类型)。这个过程如下:
- 如果这个祖先是块级元素,包含块则设置为该元素的内边距边界;换句话说,就是由边框界定的区域。
- 如果这个祖先是行内元素,包含块则设置为该祖先元素的内容边界。在从左向右读的语言中,包含块的上边界和左边界是该祖先元素中第一个框内容区的上边界和左边界,包含块的下边界和右边界是最后一个框内容区的下边界和右边界。
- 如果没有祖先,元素的包含块定义为初始包含块。
当footer的position设置为relative的时候,它的包含块就是leftcolumn,所以bottom的值设置为负(-100px)表示为远离leftcolumn下边框100px。