博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
盒子模型中的div居中
阅读量:7013 次
发布时间:2019-06-28

本文共 1725 字,大约阅读时间需要 5 分钟。

hot3.png

说到居中的问题,或许大多数童鞋都会想到text-align:center; margin:0 auto; vertical-align:middle; line-height:height; ……的确,这些属性在某种程序上可以达到居中的效果。但是否是适用于每一种情况呢?我们先来温习一下这些个属性值的用处。
    text-align:center;                    行内元素的水平居中显示;
    margin:0 auto;                       固宽盒子在浏览器中的居中显示效果;
    vertical-align:middle;             行内元素的垂直居中显示;
    line-height:height;                 针对于单行文字垂直居中显示;
====================================
HTML:
<div class="A">
     <div class="B">测试</div>
</div>
----------------------------------
CSS:
第一种方法:(常用)
.A{  position: relative;  height:500px; width:500px; background-color:#FF0000;}
.B{  position: absolute; top:50%; left:50%; height:250px; width:250px; background-color:#FFF;  margin:-125px 0 0 -125px;  }
第二种方法:
.A{  position: relative;  height:500px; width:500px; background-color:#FF0000;  overflow:hidden; }
.B{  position: relative;  height:250px; width:250px; background-color:#FFF;  margin:-125px auto 0; top:250px; }
第三种方法:(IE有点问题)
.A{  position: relative;  height:500px; width:500px; background-color:#FF0000; }
.B{  position: absolute;  height:250px; width:250px; background-color:#FFF;  margin:auto; top:0; left:0; bottom:0;  right:0; }
第四种方法:(用到CSS3,不考虑IE,只在火狐做了测试,其他可加前缀)
.A{
position:relative;  height:500px; width:500px; background-color:#FF0000; display:-moz-box; -moz-box-pack: center; -moz-box-align:center; }
.B{height:250px; width:250px; background-color:#FFF;}
第五种方法:(IE6/7兼容)
.A{  display:table-cell; height:500px;width:500px;background-color:#FF0000; vertical-align: middle;text-align: center;
*font-size:438.6px;/*font-size的值为该父元素的高度除以1.14得到的值,即500/1010=438.6*/ }
.B{  display:inline-block; height:250px; width:250px; background-color:#FFF; 
*display:inline; *zoom:1;/*ie6/7实现inline-block*/
*vertical-align: middle;
font-size:12px;/*恢复正常字体大小*/ }

转载于:https://my.oschina.net/u/1388854/blog/204012

你可能感兴趣的文章
Android之Gson
查看>>
HTML5 input type 属性所有值
查看>>
java中的==、equals()、hashCode()源码分析
查看>>
安卓开发_浅谈Fragment之ListFragment
查看>>
HDU 3613 Best Reward 正反两次扩展KMP
查看>>
[AFUI]App Framework
查看>>
view类的setVisibility
查看>>
zepto.js 源码解析
查看>>
HTTP状态码大全
查看>>
使用ASP.NET Web API 2创建OData v4 终结点
查看>>
MyBatis简单的增删改查以及简单的分页查询实现
查看>>
Android快捷支付SDK Demo resultStatus={4001};memo={參数错误};result={}问题
查看>>
urllib2中自定义opener
查看>>
Hadoop快速入门
查看>>
MySql_安装及简单命令
查看>>
CSDN markdown 编辑器 第四篇 LaTex语法
查看>>
Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp
查看>>
ubuntu14安装redis
查看>>
DICOM:C-GET与C-MOVE对照剖析
查看>>
什么是跨域请求
查看>>