五、完美的解决方案
那么我们综合上面两种方法就可以得到一个完美的解决方案,不过这要用到CSShack的知识。
ExampleSourceCode
- DIV#wrap{
- display:table;
- border:1pxsolid#FF0099;
- background-color:#FFCCFF;
- width:760px;
- height:400px;
- _position:relative;
- overflow:hidden;
- }
- DIV#subwrap{
- vertical-align:middle;
- display:table-cell;
- _position:absolute;
- _top:50%;
- }
- DIV#content{
- _position:relative;
- _top:-50%;
- }
至此,一个完美的居中方案就产生了。
广州网站建设,网站建设,广州网页设计,广州网站设计
SourceCodetoRun
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>52CSS.com</title>
- <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
- <styletypestyletype="text/css">
- body{font-size:12px;font-family:tahoma;}
- DIV#wrap{
- display:table;
- border:1pxsolid#FF0099;
- background-color:#FFCCFF;
- width:760px;
- height:400px;
- _position:relative;
- overflow:hidden;
- }
- DIV#subwrap{
- vertical-align:middle;
- display:table-cell;
- _position:absolute;
- _top:50%;
- }
- DIV#content{
- _position:relative;
- _top:-50%;
- }
- </style>
- </head>
- <body>
- <DIVidDIVid="wrap">
- <DIVidDIVid="subwrap">
- <DIVidDIVid="content"><pre>现在我们要使这段文字垂直居中显示!
- DIV#wrap{
- border:1pxsolid#FF0099;
- background-color:#FFCCFF;
- width:760px;
- height:500px;
- position:relative;
- }
- DIV#subwrap{
- position:absolute;
- border:1pxsolid#000;
- top:50%;
- }
- DIV#content{
- border:1pxsolid#000;
- position:relative;
- top:-50%;
- }</pre>
- </DIV>
- </DIV>
- </DIV>
- </body>
- </html>
[可先修改部分代码再运行查看效果]
垂直居中vertical-align的值是middle,而水平居中align的值是center,虽然同是居中但关键字不同。
广州网站建设,网站建设,广州网页设计,广州网站设计



