最近在上html的课程,好久没有细致的研究html了。发现一个问题,text-align:center 样式并不能控制ie8下的内容居中。到网上查了下,找到解决办法。
1、Ie浏览器-工具-兼容性视图设置-在兼容性视图中显示所有网站。
2、通过书写样式来解决
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
body{
text-align:center;
width:100%;
background-color:green;
}
div{
text-align:left;
width:600px;
margin:0 auto;
border:1px solid #333fff;
background-color:#cccfff;
}
</style>
</head>
<body >
<div >测试div居中</div>
<div style="width:200px;">测试div居中</div>
</body>
</html>
标红这个样式很重要,没有这个样式,就不会居中了。
3、<body><div style="width:400px; background-color:green;height:300px; position:relative;left:50%;margin-left:-200px;"></div></body>
第三种方法,是通过left样式,让div左边正好还正中。但我们需要的是div的中心在正中,那么就再加一个margin-left的属性,值赋成div宽度的一半,当前div就真的居中了,挺巧妙的。