温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

css如何实现元素水平居中显示与固定布局和流式布局

发布时间:2021-10-14 14:23:42 来源:亿速云 阅读:131 作者:小新 栏目:移动开发

这篇文章给大家分享的是有关css如何实现元素水平居中显示与固定布局和流式布局的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。



首先,要 让元素水平居中,就必须得了解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如何实现元素水平居中显示与固定布局和流式布局”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css
AI