黑马程序员 css中元素居中的学习心得

1.<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
div { margin:0 auto}//元素两边的边距自动等宽。
</style></head>

<body>
<div></div>
</body>
</html>

2.<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--

body { text-align:center;}//将body中元素居中对齐。
div { text-align:left; width:800px; height:500px; background:#000}
-->
</style></head>

<body>
<div></div>
</body>
</html>

3.<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
div { width:800px; position:absolute; height:500px; background:#000; left:50%; margin-left:-400px;}//将元素绝对定位,左边的距离为整个body的50%,再向左拉元素的宽度的一半。
-->
</style></head>

<body>
<div></div>
</body>
</html>

posted @ 2012-01-03 14:23  黑马--彭超  阅读(184)  评论(0编辑  收藏  举报