search
HomeWeb Front-endHTML TutorialCSS positioning DIV absolute bottom_html/css_WEB-ITnose

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
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment