css+div实现居中实例教程

Demo of middled vertical align

Author: Spenser Lee, Liberty Studio
Originally posted on BlueIdea Forum

Table of centents:

  1. Single line countainer with/without a fixed height
  2. Align multi-line container which does not have a fixed height
  3. Simulating table layout in container with a fixed height
  4. IE's solution
  5. A perfect compounded sample

Case One: Single line countainer with/without a fixed height

If you only want to display a container which only holds a single line of text, you can set line-height property to height property, then set overflow to hidden.

Sample:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

Core code:

.middle-demo-1{
	height: 4em;
	line-height: 4em;
	overflow: hidden;
}
Notes:
  1. I strongly recommend you use relative size in height and line-height property. Why? You can simply set the font size larger if your browser support it. When it gets large enough, you will see the countainer is stretched and the height is no longer equal to line-height property, thus, the layout is messed up. Using relative size as em, ex or % will let your countainer stretch with the content.
  2. overflow: hidden is a must. Why? Same as above. Just ensure height and line-height are always equal.
Pros:
  1. Fits in both block elements and inline elements.
  2. Capable of all 5th-gen browsers.
Cons:
  1. Text length is limited. Max with only one line.
  2. Does not work well on none text contents such as images and objects.

Back

Case Two: Align multi-line container which does not have a fixed height

In this case, we should simply set a pair of fixed equivalences to padding-top and padding-bottom attribute. It works on both IE and non-IE browsers.

Sample:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dignissim diam eu sem. Proin nunc ante, accumsan sollicitudin, sodales at, semper sed, ipsum. Etiam orci. Vestibulum magna lectus, venenatis nec, tempus ac, dictum vel, lorem.

Core code:

.middle-demo-2{
	padding-top: 24px;
	padding-bottom: 24px;
}
Pros:
  1. Fits in both block elements and inline elements.
  2. Works on none text contents as fine as text contents.
  3. Capable of all 5th-gen browsers. Might need a little tune-up for the box model bug of IE5 though.
Cons:
  1. You can not assign height in this solution.

Back

Case Three: Simulating table layout in container with a fixed height

CSS offers a set of very convenient display atrribute values called display: table, display: table-row, display: table-cell and other display values begin with table-. It offers a way to simulate table layout in all elements. As a result, any element with display: table-cell, vertical-align: middle and a fixed height will display exactly like a table cell.

Sample: (You may not be able to view the effect on IE)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dignissim diam eu sem. Proin nunc ante, accumsan sollicitudin, sodales at, semper sed, ipsum. Etiam orci. Vestibulum magna lectus, venenatis nec, tempus ac, dictum vel, lorem.

Core code:

.middle-demo-3{
	display: table-cell;
	height: 300px;
	vertical-align: middle;
}
Notes:
  1. display: table-cell must work with other elements with display: table value sets in order to form a literal table. Or it might cause unexceptable bugs.
  2. Sadly IE series (including the latest IE 7 beta) does not support any of display: table values, so it won't work in IE.
Pros:
  1. It has the most perfect render for vertical-align: middle align.
Cons:
  1. It only works in latest versions of non-IE browsers, such as Mozilla, Firefox, Netscape 8, Opera 8, and Safari.

Back

Case Four: IE's solution

Since IE is a lame browser when it comes to ANYTHING, so you can't use the solution above simply because IE does not recognize it at all. However, there has been nothing yet you can not code with CSS if you have already coded it with table. It even offers you possibility to layout your page that tables can not do!

So what's the solution for IE? Like what we always do: Hit IE's back with the BUGS it offers!

Sample: (You are able to view the correct result only on IE)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dignissim diam eu sem. Proin nunc ante, accumsan sollicitudin, sodales at, semper sed, ipsum. Etiam orci. Vestibulum magna lectus, venenatis nec, tempus ac, dictum vel, lorem.

.middle-demo-4{
	height: 300px;
	position: relative;
}
.middle-demo-4 div{
	position: absolute;
	top: 50%;
	left: 0;
}
.middle-demo-4 div div{
	position: relative;
	top: -50%;
	left: 0;
}

See? Don't ask me why it worked. This hack is based on years of study in IE's own stupid layout model and it works very well, even in IE5 given the box width hack. I won't tell you the theory behind all these. It's my top secret, with which I solved a lot of other cross browser layout problems, and I'm not going to share it with you. But you are free to use this IE hack It's kinda handful in most occassions.

Pros:
  1. The only perfect vertical align method in IE. It works even better then add automatic paddings.
Cons:
  1. After all it's a CSS hack. We can avoid it if not for IE.

Back

Case Five: A perfect compounded sample

Here's the perfect compounded solution on vertical centering that works on almost all latest browsers.

Sample: (This works on almost all browsers)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dignissim diam eu sem. Proin nunc ante, accumsan sollicitudin, sodales at, semper sed, ipsum. Etiam orci. Vestibulum magna lectus, venenatis nec, tempus ac, dictum vel, lorem.

I'm not going to give you the full code of this one. But it's not difficult to write it yourself, with the solution of case 3 and 4, and a little knowledge in IE/non-IE CSS hackers. And actually the hacking style is not limited, so I don't want to force you to use my hacking style neither. Stop being lazy, and write the code yourself!

Browser capability:
  1. Works on: IE6+, Mozilla 1.5+, Netscape Navigator 7+, Opera 7+, Firefox 1.0+ and Safari 1.0+
  2. On IE5, you might need to apply the box model hacker to make the height correct.
  3. Untested: WebOmini, Konqueror.
  4. Fails on: Other browsers not reffered in the list above.

Back

Copyright Spenser Lee, 2006 Liberty Studio

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索ie
css实现div垂直居中、css实现div水平居中、css div 居中、css div 垂直居中、css设置div居中,以便于您获取更多的相关知识。

时间: 2024-07-29 09:25:59

css+div实现居中实例教程的相关文章

CSS横向导航菜单实例教程

CSS横向导航菜单实例教程2,又一款css导航菜单.   <ul id="nav">      <li><a href="http://www.alixixi.com/">Div+CSS教程</a></li>      <li><a href="http://www.alixixi.com/" id="current">CSS布局实例</

css li应用基础实例教程

 代码如下 复制代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&qu

CSS标签切换代码实例教程 比较漂亮_经验交流

我们的设计越来越追求一种简洁的风格,希望在有限的空间内展示更多的内容.与此同时我们发现一些问题,内容的简单排列总使页面很长.滚屏很多才能将显示的内容布局完毕.YAHOO与网易率先应用了标签切换的布局方式,打破了常规布局的局限性,在相同尺寸的区域内,可以放置更多的内容.我们只需要点击不同的选项卡或链接就能展开内容,这并不需要打开新的网页,只是在同一页内完成. 一.标签切换总体的实现思路: 实现这种标签切换的布局有多种方式,也流传着各种不同的代码,我们应用DIV CSS进行布局,首先来整理一下思路,

css div 横向居中写法

本文章就单独讲一下关于css教程 div 横向居中写法的用法,并且在ie,ie7,firefox兼容问题. 横向居中(centering) 这是一个简单的技巧,但是值得再说一遍,因为我看见太多的新手问题都是问这个:css如何横向居中?您需要定义元素的宽,并且定义横向的margin,假如您的布局包含在一个层(容器)中,您可以这样定义使它横向居中: #wrap { width:760px; /* 修改为您的层的宽度 */ margin:0 auto; } 但是ie5/win不能正确显示这个定义,我们

css div自适应宽度实例

当内容超出父级元素宽度,在未设置white-space:nowrap时会自动换行,设置了white-space:nowrap样式又不能关联到滚动条延伸的部分,解决方案如下:       1.在父级元素添加white-space:nowrap属性:       2.计算(每一行)内容宽度:            a.将内容拆分,包括缩进.图片和文字.            b.利用<又谈换行情况处理>中在body末尾添加非换行dom元素计算文字宽度.            c.叠加拆分的小块宽度

div相对定义实例教程

css div网页布局中相对定义也是很重要的,现在我大多数都是相对定义来定义div块在网页中的位置,这样容易维护等,好了我们现在来看看语法吧. 定义方式: #id,#classname{ position:relative } 上面我们是说一个div的唯一编号或类名来操作div的属性与显示方式了. 下面我们看看定位的实例吧. h2.pos_left { position:relative; left:-20px } <h2>未定义的html元素</h2> <h2 class=

css [滑动效果]Rollovers 实例教程

翻转的CSS       由三奇,又名Halikidiki : 2005年1月19 现在正在一个我自己,我敢肯定,所有网页设计师在某个时候在其设计寿命想改变一个图像到另一个时,鼠标辗过(通常使用的联系) .最通用的方式实现这一目标是利用JavaScript和有很多教程了.但是,你(如果你像我这样的东西)停顿某处教程,跳到直线年底获得代码,然后只要复制并粘贴到您的网站上.现在,这个工作和一切,你不明白,为什么它的作品.或者,如果任何事情都会发生错误你怎么解决?翻车的CSS更简单,不需要任何东西,除

CSS Div完美居中方法

CSS中有很多(水平/垂直)居中的方法今天刚好看到一个新的方法,嘿嘿,自我感觉一下非常好用.而且以前我也没见过.所以今天就记一下.下次备用 HTML  代码如下 复制代码 <DIV class="demo center"> <DIV></DIV></DIV>CSS .center{ /* 需要宽度支持但自适应 */  position:relative; } .center>*{ /* ie8+,ie7-请使用js或者其它方法 */

css dt dl dd实例教程

   <dl id="msgdl">                  <dd>                     <span id="commentname">                     <?php                    echo $rs['gx_title'];               ?>                     </span>