博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
css布局学习资料
阅读量:7170 次
发布时间:2019-06-29

本文共 4689 字,大约阅读时间需要 15 分钟。

http://www.barelyfitz.com/screencast/html-training/css/positioning/
http://www.noupe.com/css/css-layouts-40-tutorials-tips-demos-and-best-practices.html
http://css-tricks.com/
http://www.quirksmode.org/css/clearing.html

Clearing floats

No compatibility problems. Incredible, isn't it?

.

A common problem with float-based layouts is that the floats' container doesn't want to stretch up to accomodate the floats. If you want to add, say, a border around all floats (ie. a border around the container) you'll have to command the browsers somehow to stretch up the container all the way.

The problem

Let's try it. This is the CSS we'll use throughout the page:

div.container { 	border: 1px solid #000000; }  div.left { 	width: 45%; 	float: left; }  div.right { 	width: 45%; 	float: right; }

Now this happens:

The left column.

The left column.

The left column.

The left column.

The right column.

The right column.

The right column.

We want the black border to go around both our floating columns. That doesn't happen, though. The container div itself has no height, since it only contains floating elements. Therefore the border stubbornly stays at the top of the floating columns.

The old solution

The old solution to this problem required you to add an extra element with clear: both to the container. Once it was in place the container contained a non-floating element, which means it stretches itself up to accomodate it:

The left column.

The left column.

The left column.

The left column.

The right column.

The right column.

The right column.

clearer div with
clear: both

This can be done either by adding the extra element in the HTML source code (as the example above does) or by using the to generate it. Since Explorer (Win and Mac) doesn't support :after this second solution was mainly of theoretical interest.

In any case, adding an HTML element for presentation's sake is something we've learned to avoid. Unfortunately the problem could not be solved in any other way, until now.

The new solution

It was Alex Walker who a new, simpler solution, though he gives the credits for actually inventing it to . In any case, the solution seems to be:

div.container { 	border: 1px solid #000000; 	overflow: auto; 	width: 100% }

The width is necessary to trigger "hasLayout" in Explorer Windows (except for 7). This works:

The left column.

The left column.

The left column.

The left column.

The right column.

The right column.

The right column.

Now the border neatly fits around the floated elements.

The tricky bits

A few points merit extra attention:

  1. The trick works with three of the four overflow values: auto, hidden and scroll. Of course, using the last value will show scrollbars, regardless of whether they're needed or not.
  2. Some browsers also need a width or height for the container div.
  3. The browser may show scrollbars when the content escapes beyond the specified width.

width or height required

The use of a width or height declaration is required to make the effect work in Explorer Windows and Opera. If you don't include it Explorer Windows continues to show the border at the top of the columns, as if there were no overflow. Opera completely hides the content of the container.

A width: 100% is a neat starting point, although more complicated layouts may require other values.

Explorer Mac: hidden

If Explorer Mac is still important to you, use overflow: hidden. This browser always shows scrollbars when you use overflow: auto; and I'm not sure why.

Unwanted scrollbars

As , this solution might cause unwanted scrollbars if the content escapes from the container box. Personally I wonder if this will be a serious problem. overflow: hidden makes sure that no scrollbars will ever be visible. Of course some content might be hidden instead, but if we make very sure that

  1. the height of the container remains flexible (ie. "as much as needed")
  2. the widths of the combined floats never exceed the width of the container, and preferably remain slightly smaller to allow for some flexibility

this problem will never creep up.

On the other hand, never say never. In pure tests like the ones above the effect works fine. In real-world sites, where there's plenty more CSS to confuse browsers, something will eventually go wrong somewhere. Until we reach that point, though, this technique will do nicely.

The complete effect

For completeness' sake, here's the code you need:

div.container { 	border: 1px solid #000000; 	overflow: hidden; 	width: 100%; }  div.left { 	width: 45%; 	float: left; }  div.right { 	width: 45%; 	float: right; }

The left column.

The left column.

The left column.

The left column.

The right column.

The right column.

The right column.

转载地址:http://ipbzm.baihongyu.com/

你可能感兴趣的文章
算法与数据结构知识点
查看>>
在单位成功实验的PIX配置
查看>>
centos6.x使用dd命令制作u盘启动
查看>>
如何使用Wireshark抓包
查看>>
mysql 时间函数用法 集合
查看>>
技术宅男既要提升编程技术也要加强沟通能力
查看>>
开源计划--格瓦拉梦想(GUEVARA‘S DREAM)
查看>>
show full columns 和 checking privileges的说明
查看>>
电信网络拓扑图自动布局之总线
查看>>
数据库启动时报ORA-00845错误解决方法
查看>>
查询阿里云存储文件并导出excle 保存到本地
查看>>
WebService-—调用第三方提供的webService服务
查看>>
LVM报错:resize2fs: Bad magic number in super-block
查看>>
从开发到部署会用到的 Docker 命令
查看>>
access数据库转mysql数据库
查看>>
CISCO服务器配置RAID步骤
查看>>
利用makefile文件编译c++源文件
查看>>
VS 0xC0000005 运行错误分析
查看>>
达观数据 hive
查看>>
mysql主从同步监控脚本
查看>>