如何让div水平居中

| 阅读数:--次| 作者:html,css
摘要:如何让div水平居中:让整个div水平居中的使用相当频繁。例如,一般情况下都是让网页页面在浏览器中水平居中。代码实例: 蚂蚁部落 蚂蚁部落 上面的代码实现了将div水平居中效果,给对象添加margin:0px auto即可实现此效果。最后再补充一句,如果div没有设置宽度值的话,marg...

如何让div水平居中:

让整个div水平居中的使用相当频繁。例如,一般情况下都是让网页页面在浏览器中水平居中。

代码实例:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8"> 
<meta name="author" content="http://www.51texiao.cn/" />
<title>蚂蚁部落</title>
<style type="text/css"> 
.mytest{ 
  width:200px; 
  height:200px; 
  border:1px solid red; 
  margin:0px auto; 
} 
</style> 
</head> 
<body> 
<div class="mytest">蚂蚁部落</div> 
</body> 
</html>

上面的代码实现了将div水平居中效果,给对象添加margin:0px auto即可实现此效果。最后再补充一句,如果div没有设置宽度值的话,margin:0px auto是无效的。

返回顶部
学到老代码浏览 关闭浏览