Home >Web Front-end >HTML Tutorial >CSS positioning DIV absolute bottom_html/css_WEB-ITnose

CSS positioning DIV absolute bottom_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 12:26:591444browse

CSS positions the absolute bottom of the DIV

网页制作Webjx文章简介:CSS的简单在于它易学,CSS的困难在于寻找更好的解决方案。在CSS的世界里,似乎没有完美这种说法。所以,现在介绍的CSS绝对底部,只是目前个人见过的方案中比较完美的吧。

CSS的简单在于它易学,CSS的困难在于寻找更好的解决方案。在CSS的世界里,似乎没有完美这种说法。所以,现在介绍的CSS绝对底部,只是目前个人见过的方案中比较完美的吧。

先说我们为什么会使用到这个CSS底部布局解决方案:

当做一个页面时,如果页面内容很少,不足于填充一屏的窗口区域,按普通的布局,就会出现下面图片中的样子(也就是底部内容并没有位于窗口的底部,而留下了大量空白。

对于追未完美的设计师来说,这是不美观的。网上有一些解决方案,但会出现当改变窗口高度时,底部和正文重叠的BUG。尽管没有多少人会有事没事儿的去改变窗口高度,但设计嘛,追求的就是尽善尽美。

下面是我找到的一个比较完美的方法,来自国外的设计达人,纯CSS,可以实现: 当正文内容很少时,底部位于窗口最下面。当改变窗口高度时,不会出现重叠问题。

甚至,创造该CSS的人还专门成立一个网站介绍这个CSS底部布局方案。不知道他有没有去申请专利:)

HTML代码:



  

  

  

  



说明: 使用这个布局的前提,就是footer要在总的div容器之外,footer使用一个层,其它所有内容使用一个总的层。如果确实需要到添加其它同级层,那这个同级层就必须使用position:absolute进行绝对定位。

网页制作Webjx文章简介:CSS的简单在于它易学,CSS的困难在于寻找更好的解决方案。在CSS的世界里,似乎没有完美这种说法。所以,现在介绍的CSS绝对底部,只是目前个人见过的方案中比较完美的吧。

CSS代码:

下面是主要的CSS代码,让你的底部可以位于窗口的最下面:

html, body, #wrap {height: 100%;}
body > #wrap {height: auto; min-height: 100%;}
#main {padding-bottom: 150px;} /* 必须使用和footer相同的高度 */
#footer {position: relative;
margin-top: -150px; /* footer高度的负值 */
height: 150px;
clear:both;}

说明: 需要注意的就是#main的padding值、footer的高度和负margin值,需要保持一致。

就是这么简单,不过还没完。如果你的主体是使用悬浮布局,还得解决一些浏览器的兼容问题,这里使用的重点是为了Goolge Chrome。

对#main部份进行著名的Clearfix Hack:

.clearfix:after {content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;}
.clearfix {display: inline-block;}
/* Hides from IE-mac */
* html .clearfix { height: 1%;}
.clearfix {display: block;}
/* End hide from IE-mac */注: 该方案测试于两栏悬浮布局,兼容各大主流浏览器,包括Google Chrome。

P.S:

网络上之前比较知名的footer布局有 Ryan Faits 创造的,不过它的方法在HTML代码中会有一个空的div层。严格来说,是不符合语义网代码标准的。 另外,还有一篇 Exploring Footers article from A List Apart ,但使用了一些JavaScript代码。 这样一比较,上面看似简单的纯CSS,就显得伟大许多了。

Webpage production Webjx article introduction: The simplicity of CSS lies in its ease of learning, CSS The difficulty lies in finding better solutions. In the world of CSS, there seems to be no such thing as perfection. Therefore, the CSS introduced now is the absolute bottom, and it is just the most perfect solution among the solutions I have seen so far.

The simplicity of CSS lies in its ease of learning, but the difficulty of CSS lies in finding better solutions. In the world of CSS, there seems to be no such thing as perfection. Therefore, the CSS introduced now is the absolute bottom, and it is just the most perfect solution among the solutions I have seen so far.

First let’s talk about why we use this CSS bottom layout solution:

When making a page, if the page content is very small and not enough to fill the window area of ​​a screen, follow the normal layout , it will look like the picture below (that is, the bottom content is not located at the bottom of the window, leaving a lot of blank space.

For designers who pursue imperfection, This is not beautiful. There are some solutions on the Internet, but when changing the window height, there will be a bug that the bottom and the text overlap. Although not many people will change the window height without any trouble, the design is to pursue perfection. .

The following is a relatively perfect method I found, which can be achieved by a foreign design expert using pure CSS: When the text content is small, the bottom is at the bottom of the window. There will be no overlapping problem.

Even the person who created this CSS has set up a website to introduce this CSS bottom layout scheme. I don’t know if he has applied for a patent:)

HTML code:









Note: The prerequisite for using this layout is that the footer must be in the overall div container In addition, the footer uses a layer and everything else uses an overall layer. If you really need to add other sibling layers, then this sibling layer must use position:absolute for absolute positioning.

Webpage production Webjx article introduction: The simplicity of CSS lies in its ease of learning, and the difficulty of CSS lies in finding better solutions. In the world of CSS, there seems to be no such thing as perfection. Therefore, the CSS introduced now is the absolute bottom, and it is just the most perfect solution among the solutions I have seen so far.

CSS code:

The following is the main CSS code so that your bottom can be at the bottom of the window:

html, body, # wrap {height: 100%;}
body > #wrap {height: auto; min-height: 100%;}
#main {padding-bottom: 150px;} /* Must use footer Same height*/
#footer {position: relative;
margin-top: -150px; /* Negative value of footer height*/
height: 150px;
clear: both;}

Note: What needs to be noted is that the padding value of #main, the height of footer and the negative margin value need to be consistent.

It’s that simple, but it’s not over yet. If your main body uses a floating layout, you have to solve some browser compatibility issues. The focus here is on Google Chrome.

Perform the famous Clearfix Hack on the #main part:

.clearfix:after {content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;}
.clearfix {display: inline-block;}
/* Hides from IE-mac */
* html .clearfix { height: 1%;}
.clearfix {display: block;}
/* End hide from IE-mac */Note: This solution was tested on a two-column floating layout and is compatible with all major mainstream Browsers, including Google Chrome.

P.S:The previously well-known footer layout on the Internet was created by Ryan Faits, but its method will have an empty div layer in the HTML code. Strictly speaking, it does not comply with the Semantic Web code standards. Also, there is an Exploring Footers article from A List Apart , but it uses some JavaScript code. In this comparison, the seemingly simple pure CSS above appears much greater.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn