作家
登录

div水平垂直居中的完美解决方案

作者: 来源:www.28hudong.com 2012-11-19 22:25:09 阅读 我要评论

让div居中对齐使用margin-left:auto;margin-right:auto; 可以让你的div居中对齐。 .style{margin-left:auto;margin-right:auto;} 缩写形式为: .style{margin:0 auto;} 数字0 表示上下边距是0。可以按照需要设置成不同的值。 查看下面的例子: <!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" lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>居中div演示效果</title> <style type="text/css"> .align-center{ margin:0 auto; /* 居中 这个是必须的,,其它的属性非必须 */ width:500px; /* 给个宽度 顶到浏览器的两边就看不出居中效果了 */ background:red; /* 背景色 */ text-align:center; /* 文字等内容居中 */ } </style> </head> <body> <div class="align-center">居中div演示效果</div> </body> </html> 提示:您可以先修改部分代码再运行注意,需要加上上面的那句 才能生效,要是不想要这句的话,也可以给body加一个属性: body{text-align:center;} 另外,让div内的内容(包括文字及图片)居中的代码是: text-align:center; div居中的完美解决方案! (水平垂直居中)1,关于居中使用css为:position:fixed;left:50%;top:50%;margin-left:width/2;margin-top:height/2; 对于ie6,只能把position:改成absolute; <!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" lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>水平垂直居中div演示效果</title> <style type="text/css"> .align-center{ position:fixed;left:50%;top:50%;margin-left:width/2;margin-top:height/2; } </style> </head> <body> <div class="align-center">水平垂直居中div演示效果</div> </body> </html> 提示:您可以先修改部分代码再运行

  推荐阅读

  用CSS设置表格Table的细边框的比较好用的方法

设置Table的细边框通常有这么几种方式: 1、设置边框的BORDER=0 、cellspacing=1,设置Table的背景色为所要的边框色,再设置所有的td背景色为白色,这样细边框就显露出来了。这个方法有些邪气,看似不太正宗,但>>>详细阅读


本文标题:div水平垂直居中的完美解决方案

地址:http://www.17bianji.com/kaifa2/CSS/16613.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)