CSS问题。我想让一些内容在一个div里是靠左的显示,然后让这个div在屏幕里是居中显示,要怎么才能实现?

我的代码要怎么修改才行呢?

<!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=UTF-8" />
<title>Register</title>
<style>
.header{
text-align:left;
}

</style>
</head>

<body>
<form action="result.jsp" name="registerInfoForm" method="post">
<div class="header">
用户名:
<input name="usernametext" type="text" />

密码: <input name="passwordText" type="text" /> <br><br>
你喜欢:<input name="favoriteCB" type="checkbox" />足球
<input name="favoriteCB" type="checkbox" />篮球
性别:<input type="radio" name="sex"/>男 <input type="radio" name="sex"/>女 <br><br>
你的计算机:<br>
<textarea name="computerInfo" style="width:300px;height:auto"></textarea> <br><br>
你的计算机的操作系统:<input type="text" name="OSInfo" /> <br><br>
你所在地:
<select name="city">
<option value="beijing">北京
<option value="shanghai">上海
</select><br><br>
<input type="submit" name="Ok" value="提交" />
<input type="reset" value="全部重置" />
</div>
</form>
</body>
</html>

第1个回答  2015-04-02

其实一楼说的是对的,想要居中需要有绝对宽度,然后margin:0 auto;

第2个回答  2015-04-01
.header{
width: 800px;
margin: 0 auto;
text-align:left;
}

先给div一个宽度,不能是100%;
然后加margin值,左右设成auto。追问

不行,虽然不是靠左了,但也不是居中的

第3个回答  2015-04-01
给div设置为margin:0 auto; div盒子中的可以设置为float:left;
第4个回答  2015-04-02
<style>
div{
width:500px;margin:40px auto;
}

</style>
要设置一个宽,然后用margin来实现!你宽设置太多了就会靠左!本回答被提问者采纳