利用CSS实现对象的垂直中心有许多不同的方法,困难的是选择其正确的方法。 我来说明我看到的好方法和如何建立好的中央网站。
使用CSS实现垂直居中并不容易。 某些浏览器也有无效的方法。 看看垂直集中对象的五种不同方法及其各自的优缺点。 (请查看测试页。 有简单的说明。 )
方法1
此方法在表中设置了几种div显示方式,因此可以使用表的vertical-align property属性。
contentgoeshere.wrapper { display : table; }
. cell {
display :表蜂窝;
垂直对准:米;
}
好处:
content可以动态更改高度(不需要在CSS中定义)。 如果wrapper没有足够的空间,则content不会被截断
缺点:
在internet explorer (I E8 beta )中无效,有很多嵌套标签) (实际上没那么糟,是另一个主题)。
方法2 :
该方法使用绝对位置的div,将其top设置为50%,top margin设置为负content高度。 也就是说,对象必须在CSS中指定固定高度。
因为有固定高度,所以您可能想在内容中指定overflow :自动。 这样一来,如果内容过多,就会显示滚动条以防止内容溢出。
内容goes here.content {
位置: absolute;
top:50%; height:240px;
margin-top:-120px; /* negative half of the height */
}
好处:
应用于所有浏览器
不需要嵌套标签
缺点:
如果没有足够的空间,内容将消失。 (如果div位于主体中,用户缩小浏览器窗口时不显示滚动条。)。
方法3
此方法在content元素之外插入div。 该div height:50%; 玛姬- bottom :-content height;
清除内容浮动并将其居中显示。
Content here .floater {
浮点:左;
height:50%;
玛格琳- bottom :-120 px;
}
. content {
clear:both;
height:240px;
position:relative;
}
好处:
应用于所有浏览器
如果没有足够的空间(例如,窗口折叠),content不会被截断,而是显示滚动条
缺点:
我唯一能想到的就是需要额外的空元素(没那么糟。 又是另一个话题)。
方法4
该方法使用的是具有恒定宽度和高度div的position:absolute。 此div设置为top:0; bottom:0; 但是,因为它有一定的高度,所以实际上不能把与上下的间隔定为0。 因此,margin:auto; 将被定位在中央。 使用margin :自动; 将块级元素垂直居中很容易。
内容here.content {
位置: absolute;
top:0;
bottom:0;
left:0;
right:0;
margin :自动;
height:240px;
width:70%;
}
好处:
很简单
缺点:
在ie (ie8 beta )中无效
如果没有足够的空间,content将被截断,但不显示滚动条
方法5
这种方法只能将单行文本居中。 只需将线高度设置为对象的高度值,即可将文本居中。
内容here.content {
height:100px;
line-height:100px;
}
好处:
应用于所有浏览器
如果没有足够的空间,则无法截断
缺点:
仅对文本有效(块级元素无效) ) ) )。
多行的情况下,断词比较差
对于较小的元素(如按钮文本和单行文本居中),此方法非常有用。
哪个方法?
我最喜欢的是方法3,缺点很少。 因为内容
会清除浮动,所以可以在它上面放置别的元素,并且当窗口缩放时,居中的 content 不会把另外的元素盖住。看例子。
Title Content Here.floater {
float:left; height:50%; margin-bottom:-120px;
}
.top {
float:right; width:100%; text-align:center;
}
.content {
clear:both; height:240px; position:relative;
}
现在你知道是怎么回事了,现在我们开始创建一个简单但是有趣的网站。最终的样子是这样的:
步骤一
以语义化标签开始是很好的。下面是我们的页面构成:
.floater/*把 content 置中*/
.contred/*centre 盒*/
.side
.logo
.nav/*无序列表*/
.content
.bottom/*放置版权等*/
这是我用到的 xhtml 代码:
A Centred Company
** A Company** - [Home](#)- [Products](#)
- [Blog](#)
- [Contact](#)
- [About](#)
Holisticly re-engineer value-added outsourcing after process-centric collaboration and idea-sharing. Energistically simplify impactful niche markets via enabled imperatives. Holisticly predominate premium innovation after compelling scenarios. Seamlessly recaptiualize high standards in human capital with leading-edge manufactured products. Distinctively syndicate standards compliant schemas before robust vortals. Uniquely recaptiualize leveraged web-readiness vis-a-vis out-of-the-box information.
Heading 2Efficiently embrace customized web-readiness rather than customer directed processes. Assertively grow cross-platform imperatives vis-a-vis proactive technologies. Conveniently empower multidisciplinary meta-services without enterprise-wide interfaces. Conveniently streamline competitive strategic theme areas with focused e-markets. Phosfluorescently syndicate world-class communities vis-a-vis value-added markets. Appropriately reinvent holistic services before robust e-services.
Copyright notice goes here步骤二:
现在我们开始用一些基本的 CSS 来给页面添加样式。把以下代码放入在我们的 html 页面顶部被引入的 style.css。
html, body { margin:0; padding:0; height:100%; }
body { background:url('page_bg.jpg') 50% 50% no-repeat #FC3; font-family:Georgia, Times, serifs; }
.floater { position:relative; float:left; height:50%; margin-bottom:-200px; width:1px; }
.centered { position:relative; clear:left; height:400px; width:80%; max-width:800px; min-width:400px; margin:0 auto; background:#fff; border:4px solid #666; }
.bottom { position:absolute; bottom:0; right:0; }
.nav { position:absolute; left:0; top:0; bottom:0; right:70%; padding:20px; margin:10px; }
.content { position:absolute; left:30%; right:0; top:0; bottom:0; overflow:auto; height:340px; padding:20px; margin:10px; }
在我们能够把 content 垂直居中之前, body 和 html 应该被拉伸到 100% 的高度。由于 height
在 padding 和 margin 之内,所以我们要把它们设成 0 以防止因为很小的 margin 出现滚动条。
floater 的 margin-bottom 是 content 高度(400px)的一半, -200px。
现在可以看到一下效果:
#centred 的宽度为 80%。这可以市网页随着显示器的大小而变化。一般称作流体布局。设置 min-width 和
max-width 以避免网页过大或者过小。 但是 IE 不支持 min/max-width。显然可以用固定宽度来代替。
因为 #centred 是相对定位的,在它里面我们可以用绝对定位来定位元素。设置 #content 的 overflow:auto; 以避免滚动条的出现。IE 不怎么喜欢 overflow:auto; 除非我们指定高度(不是 top 和 bottom 的定位,也不是 %)
因此我们给它指定高度。
步骤三
最后要做的就是再添加点样式,让页面好看点。从目录开始吧。
.nav ul { list-style:none; padding:0; margin:20px 0 0 0; text-indent:0; }
.nav li { padding:0; margin:3px; }
.nav li a { display:block; background-color:#e8e8e8; padding:7px; margin:0; text-decoration:none; color:#000; border-bottom:1px solid #bbb; text-align:right; }
.nav li a::after { content:'»'; color:#aaa; font-weight:bold; display:inline; float:right; margin:0 2px 0 5px; }
.nav li a:hover, .nav li a:focus { background:#f8f8f8; border-bottom-color:#777; }
.nav li a:hover::after { margin:0 0 0 7px; color:#f93; }
.nav li a:active { padding:8px 7px 6px 7px; }
需要注意的是 #centred 的圆角。 CSS3 中,应该有 border-radius 属性来设定圆角的半径(可参考 CSS3之旅: border-radius(圆角) – 糖伴西红柿)。现在的
流行的浏览器都还不支持,除非用 -moz(Molilla Firefox) 或者 -webit(Safari/Webkit) 前缀.
兼容性注意事项
如你所想,IE 是唯一添麻烦的浏览器。
#floater 必须指定宽度,否则在任意版本 IE 中,它都啥也不干
IE 6 中目录被周围太多的空间打断
IE 8 有多余空间(作者遗漏)
更多的想法
利用居中的网页可以做很多有意思的事情。我在重新设计 SWFObject Generator 2.0(使用 SWFObject2.0 生成代码)使用了这个想法。这里有另外的一个想法。
资料
以下是我参考的一些资料,推荐阅读。
糖伴西红柿说:
水平居中经常用,其实垂直居中也很有用的。平时用的最多的应该是方法五了,算是个小技巧吧。
相关
Related Posts
图片水平垂直置中
…搞定网页打印自动分页问题
设计过程出现一个困难问题,一个php生成的HTML页面有多个表格,要求必须自动分开打印到几张A4纸张上面.经过询问资深人士,结果统一回答,HTML页面无法做打印时自动分页面打印.于是乎正准备放弃这个分页面打印念头,郁闷着被笑话着被困扰着. 一个表格被分开打印到两张A4上,多难看啊.... 无意中翻阅CSS,发现里面有两语句:page-break-beforepage-break-after 分别对应的 参数是: auto; always; left; right;…
推荐10款非常优秀的 HTML5 开发工具
HTML5 发展如火如荼,随着各大浏览器对 HTML5 技术支持的不断完善以及 HTML5 技术的不断成熟,未来 HTML5 必将改变我们创建 Web 应用程序的方式。今天这篇文章向大家推荐10款优秀的HTML5开发工具,帮助你更高效的编写…