三种方式实现元素水平居中显示与固定布局和流式布局概念理解

首先,要 让元素水平居中,就必须得了解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,且兼容性很好,能在最广泛的浏览器下表现一致。 

原文地址:https://www.cnblogs.com/jq-melody/p/4482653.html