三种方式实现元素水平居中显示与固定布局和流式布局概念理解
发布时间:2013-01-11 11:00:22 作者:佚名
我要评论
css中让文本居中的属性很简单就可以实现,那就是设置text-align:center即可,让元素水平居中,相信对于许多网页设计师而言都不会陌生,首先,要 让元素水平居中,就必须得了解css设计中固定布局和流式布局两者的概念
css中让文本居中的属性很简单就可以实现,那就是设置text-align:center即可。而我这里所说的“元素”实际上是指容器,如果要有一个贴切点的标签,那应该可以用div来表示。
让元素水平居中,相信对于许多网页设计师而言都不会陌生。但是有的时候,自己就在想,为什么要让元素水平居中?是出于什么原因呢?都是一点自己的见解,蛮写下来...
首先,要 让元素水平居中,就必须得了解css设计中固定布局和流式布局两者的概念。它们之间的直观区别就看是否给元素设置了宽度。下面是两段代码,用来简单地说明固定布局和流式布局的区别。
1、固定布局demo:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>position-layout</title>
<style type="text/css">
.wrapper{width:750px;position:relative;margin:0 auto;text-align:left;}
.contentArea{width:450px;position:absolute;top:0;left:150px;height:500px;background:#96c;}
.leftPanel{width:150px;position:absolute;top:0;left:0;height:500px;background:#999;}
.rightPanel{width:150px;position:absolute;top:0;left:600px;height:500px;background:#06C;}
</style>
</head>
<body>
<div class="wrapper">
<div class="contentArea"></div>
<div class="leftPanel"></div>
<div class="rightPanel"></div>
</div>
</body>
</html>
2、流式布局demo:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>col3-margin-layout</title>
<style type="text/css">
.contentArea{margin:0 160px;height:500px;background:#96c;}
.leftPanel{width:150px;float:left;height:500px;background:#999;}
.rightPanel{width:150px;float:right;height:500px;background:#06C;}
</style>
</head>
<body>
<div class="wrapper">
<div class="leftPanel"></div>
<div class="rightPanel"></div>
<div class="contentArea"></div>
</div>
</body>
</html>
通过上面两个例子,可以得出:流式布局不存在元素水平居中的可能,因为它都是满屏显示的。只有固定布局,因为限宽,所以就有了让元素水平居中的可能。
其次,固定布局的实现也不一定要让元素水平居中,之所以这么做,是为了让浏览器的两边能够留出平均的旁白,而不是只有一边是一大片空白,影响美观。
都是些浅显的知识,下面进入主题。
============================================================================
让元素水平居中的三种方式,我将分别进行介绍。如下:
1、自动外边距法。
这是目前网页设计人员最熟悉的一种方法,它需要给容器设置宽度,并设置margin:auto样式。下面是一段代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>position-layout</title>
<style type="text/css">
.wrapper{width:750px;margin:0 auto;position:relative;}
.contentArea{width:450px;position:absolute;top:0;left:150px;height:500px;background:#96c;}
.leftPanel{width:150px;position:absolute;top:0;left:0;height:500px;background:#999;}
.rightPanel{width:150px;position:absolute;top:0;left:600px;height:500px;background:#06C;}
</style>
</head>
<body>
<div class="wrapper">
<div class="contentArea"></div>
<div class="leftPanel"></div>
<div class="rightPanel"></div>
</div>
</body>
</html>
通过这段代码,可以发现,这种方式在在目前各种主流浏览器下(包括ie6)都能很好的显示,只有在ie6以下的版本不生效,元素依然向左排列。如果不考虑低版本浏览器的问题,那么它将是最便捷的。
2、文本居中和自动外边距的结合使用。
这种方式可以解决ie6以下版本不支持margin:0 auto的 问题,它的用法就是在body里设置text-align:center样式。具体代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>position-layout</title>
<style type="text/css">
body{text-align:center;}
.wrapper{width:750px;position:relative;margin:0 auto;text-align:left;}
.contentArea{width:450px;position:absolute;top:0;left:150px;height:500px;background:#96c;}
.leftPanel{width:150px;position:absolute;top:0;left:0;height:500px;background:#999;}
.rightPanel{width:150px;position:absolute;top:0;left:600px;height:500px;background:#06C;}
</style>
</head>
<body>
<div class="wrapper">
<div class="contentArea"></div>
<div class="leftPanel"></div>
<div class="rightPanel"></div>
</div>
</body>
</html>
在这里,text-align:center被作为css hack来使用,因为它本属于文本的样式,用在body里来实现元素居中的样式,做了本不属于自己该做的事...
3、负外边距法。
这种方式的实现方式比前两种复杂。它得结合定位来使用。具体代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>negative-margin-element-center</title>
<style type="text/css">
.wrapper{width:750px;position:relative;left:50%;margin-left:-375px;}
.contentArea{width:450px;position:absolute;top:0;left:150px;height:500px;background:#96c;}
.leftPanel{width:150px;position:absolute;top:0;left:0;height:500px;background:#999;}
.rightPanel{width:150px;position:absolute;top:0;left:600px;height:500px;background:#06C;}
</style>
</head>
<body>
<div class="wrapper">
<div class="contentArea"></div>
<div class="leftPanel"></div>
<div class="rightPanel"></div>
</div>
</body>
</html>
首先,让容器相对文档向右偏移50%,然后,将容器的左外边距设置为负的容器宽度的一半,即可实现元素的水平居中显示。这种方式没有hack,且兼容性很好,能在最广泛的浏览器下表现一致。
以上就是我所知道的三种实现元素水平居中的方法,都比较简单,写下来就当是一次知识的回顾总结吧。
让元素水平居中,相信对于许多网页设计师而言都不会陌生。但是有的时候,自己就在想,为什么要让元素水平居中?是出于什么原因呢?都是一点自己的见解,蛮写下来...
首先,要 让元素水平居中,就必须得了解css设计中固定布局和流式布局两者的概念。它们之间的直观区别就看是否给元素设置了宽度。下面是两段代码,用来简单地说明固定布局和流式布局的区别。
1、固定布局demo:
复制代码
代码如下:<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>position-layout</title>
<style type="text/css">
.wrapper{width:750px;position:relative;margin:0 auto;text-align:left;}
.contentArea{width:450px;position:absolute;top:0;left:150px;height:500px;background:#96c;}
.leftPanel{width:150px;position:absolute;top:0;left:0;height:500px;background:#999;}
.rightPanel{width:150px;position:absolute;top:0;left:600px;height:500px;background:#06C;}
</style>
</head>
<body>
<div class="wrapper">
<div class="contentArea"></div>
<div class="leftPanel"></div>
<div class="rightPanel"></div>
</div>
</body>
</html>
2、流式布局demo:
复制代码
代码如下:<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>col3-margin-layout</title>
<style type="text/css">
.contentArea{margin:0 160px;height:500px;background:#96c;}
.leftPanel{width:150px;float:left;height:500px;background:#999;}
.rightPanel{width:150px;float:right;height:500px;background:#06C;}
</style>
</head>
<body>
<div class="wrapper">
<div class="leftPanel"></div>
<div class="rightPanel"></div>
<div class="contentArea"></div>
</div>
</body>
</html>
通过上面两个例子,可以得出:流式布局不存在元素水平居中的可能,因为它都是满屏显示的。只有固定布局,因为限宽,所以就有了让元素水平居中的可能。
其次,固定布局的实现也不一定要让元素水平居中,之所以这么做,是为了让浏览器的两边能够留出平均的旁白,而不是只有一边是一大片空白,影响美观。
都是些浅显的知识,下面进入主题。
============================================================================
让元素水平居中的三种方式,我将分别进行介绍。如下:
1、自动外边距法。
这是目前网页设计人员最熟悉的一种方法,它需要给容器设置宽度,并设置margin:auto样式。下面是一段代码:
复制代码
代码如下:<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>position-layout</title>
<style type="text/css">
.wrapper{width:750px;margin:0 auto;position:relative;}
.contentArea{width:450px;position:absolute;top:0;left:150px;height:500px;background:#96c;}
.leftPanel{width:150px;position:absolute;top:0;left:0;height:500px;background:#999;}
.rightPanel{width:150px;position:absolute;top:0;left:600px;height:500px;background:#06C;}
</style>
</head>
<body>
<div class="wrapper">
<div class="contentArea"></div>
<div class="leftPanel"></div>
<div class="rightPanel"></div>
</div>
</body>
</html>
通过这段代码,可以发现,这种方式在在目前各种主流浏览器下(包括ie6)都能很好的显示,只有在ie6以下的版本不生效,元素依然向左排列。如果不考虑低版本浏览器的问题,那么它将是最便捷的。
2、文本居中和自动外边距的结合使用。
这种方式可以解决ie6以下版本不支持margin:0 auto的 问题,它的用法就是在body里设置text-align:center样式。具体代码如下:
复制代码
代码如下:<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>position-layout</title>
<style type="text/css">
body{text-align:center;}
.wrapper{width:750px;position:relative;margin:0 auto;text-align:left;}
.contentArea{width:450px;position:absolute;top:0;left:150px;height:500px;background:#96c;}
.leftPanel{width:150px;position:absolute;top:0;left:0;height:500px;background:#999;}
.rightPanel{width:150px;position:absolute;top:0;left:600px;height:500px;background:#06C;}
</style>
</head>
<body>
<div class="wrapper">
<div class="contentArea"></div>
<div class="leftPanel"></div>
<div class="rightPanel"></div>
</div>
</body>
</html>
在这里,text-align:center被作为css hack来使用,因为它本属于文本的样式,用在body里来实现元素居中的样式,做了本不属于自己该做的事...
3、负外边距法。
这种方式的实现方式比前两种复杂。它得结合定位来使用。具体代码如下:
复制代码
代码如下:<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>negative-margin-element-center</title>
<style type="text/css">
.wrapper{width:750px;position:relative;left:50%;margin-left:-375px;}
.contentArea{width:450px;position:absolute;top:0;left:150px;height:500px;background:#96c;}
.leftPanel{width:150px;position:absolute;top:0;left:0;height:500px;background:#999;}
.rightPanel{width:150px;position:absolute;top:0;left:600px;height:500px;background:#06C;}
</style>
</head>
<body>
<div class="wrapper">
<div class="contentArea"></div>
<div class="leftPanel"></div>
<div class="rightPanel"></div>
</div>
</body>
</html>
首先,让容器相对文档向右偏移50%,然后,将容器的左外边距设置为负的容器宽度的一半,即可实现元素的水平居中显示。这种方式没有hack,且兼容性很好,能在最广泛的浏览器下表现一致。
以上就是我所知道的三种实现元素水平居中的方法,都比较简单,写下来就当是一次知识的回顾总结吧。
相关文章
- 2009-06-19介绍一下行内元素和块级元素,这个很重要,因为有的属性只能用于块元素,而有的正好相反,在一定的情况下,它们也可以相互转换,比如用display来进行设置,感兴趣的朋友可2013-05-06使用纯CSS实现未知尺寸的图片(但高宽都小于200px)在200px的正方形容器中水平和垂直居中,下面是一个权衡的相对结构干净,CSS简单的解决方法2013-04-17宽度自适应达到水平居中在网页制作中很常见而且很实用,本文整理搜集了一些实用的自适应宽度的水平居中技巧,感兴趣前端工程师们可以借鉴一下,或许对你有所帮助2013-04-05关于图片垂直居中的话题想必大家在论坛或者是百度搜索列表中看到了不少了吧,烦人的是没有具体或者相当详细的解决方法,希望本文所整理的知识点可以帮助到你2013-03-22在CSS中加了margin:0 auto;却没有效果,不能居中的问题;它的本意就是上下边界为0,左右根据宽度自适应!其实就是~~水平居中的意思,接下来为大家介绍下两个典型的错误引起2013-03-15
div垂直居中的N种方法 单行/多行文字(未知高度/固定高度)
接下来将介绍下:div垂直居中的N种方法包括:单行垂直居中/多行未知高度文字的垂直居中/多行文本固定高度的居中/在InternetExplorer中的解决方案等等感兴趣的你可不要错过了2013-02-17单选框和复选框面积很小,不容易点击,造成许多用户的困扰,用户体验不佳,所以表单元素的垂直居中让很多网页布局师为之而困扰,想实现垂直居中效果还真需要一番功夫,还好2013-02-16元素及文本的居中包括(层的横向居中/层的垂直居中/绝对居中)以及文本挣开div的题,也在这里给予了解决方法,感兴趣的朋友可以了解下啊,希望本文可以帮助到你2013-01-18简单总结一下html中元素的 水平居中、垂直居中、绝对居中的实现方式,感兴趣的朋友可以了解下哦2013-01-07


最新评论