遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

【转】div盒子水平垂直居中

 

 /*PC端,方式一:margin+position(不兼容IE6)*/

<!DOCTYPE html>
<html lang="en">
	<head>
	    <meta charset="UTF-8">
	    <title>子DIV居中</title>
	    <style type="text/css">
		    .wrap {
			    width: 200px;
			    height: 200px;
			    background: #00FF00;
			    position: relative;
			}

			.wrap .center {
			    width: 100px;
			    height: 100px;
			    background: #00FFFF;
			    /*关键点如下*/
			    margin: auto;
			    position: absolute;
			    left: 0;
			    right: 0;
			    top: 0;
			    bottom: 0;
			}
			
	    </style>
	</head>

	<body>
		<div class="wrap">
    		<div class="center"></div>
		</div>
	</body>
</html>

 

/*PC端,方式二:position+margin(兼容所有浏览器)*/

<!DOCTYPE html>
<html lang="en">
	<head>
	    <meta charset="UTF-8">
	    <title>子DIV居中</title>
	    <style type="text/css">
		    .wrap {
			    width: 200px;
			    height: 200px;
			    background: #00FF00;
			    position: relative;
			}

			.wrap .center {
			    background: #00FFFF;
			    position: absolute;
			    width: 100px;
			    height: 100px;
			    /*关键点如下*/
			    left: 50%;
			    top: 50%;
			    margin-left:-50px;/*对应中间盒子宽度的一半*/
			    margin-top:-50px;/*对应中间盒子高度的一半*/
			}
			
	    </style>
	</head>

	<body>
		<div class="wrap">
    		<div class="center"></div>
		</div>
	</body>
</html>

  

 

方案一:

div绝对定位水平垂直居中【margin:auto实现绝对定位元素的居中】,

兼容性:,IE7及之前版本不支持

复制代码
    div{
            width: 200px;
            height: 200px;
            background: green;
            position:absolute;
            left:0;
            top: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        }
复制代码

 

方案二:

div绝对定位水平垂直居中【margin 负间距】     这或许是当前最流行的使用方法。

复制代码
         div{
            width:200px;
            height: 200px;
            background:green;
            position: absolute;
            left:50%;
            top:50%;
            margin-left:-100px;
            margin-top:-100px;
        }        
复制代码

 

方案三:

div绝对定位水平垂直居中【Transforms 变形】

兼容性:IE8不支持;

复制代码
        div{
            width: 200px;
            height: 200px;
            background: green;
            position:absolute;
            left:50%;    /* 定位父级的50% */
            top:50%;
            transform: translate(-50%,-50%); /*自己的50% */
        }            
复制代码

 

方案四:

css不定宽高水平垂直居中

复制代码
     .box{

             height:600px;
             display:flex;
             justify-content:center;
             align-items:center;
               /* aa只要三句话就可以实现不定宽高水平垂直居中。 */
        }
        .box>div{
            background: green;
            width: 200px;
            height: 200px;
        }
复制代码

 

 

 

方案五:

将父盒子设置为table-cell元素,可以使用text-align:center和vertical-align:middle实现水平、垂直居中。比较完美的解决方案是利用三层结构模拟父子结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<p class="outerBox tableCell">
  </p><p class="ok">
    </p><p class="innerBox">tableCell</p>
  <p></p>
<p></p>
 
 
/*
table-cell实现居中
将父盒子设置为table-cell元素,设置
text-align:center,vertical-align: middle;
子盒子设置为inline-block元素
*/
.tableCell{
  display: table;
}
.tableCell .ok{
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}
.tableCell .innerBox{
  display: inline-block;
}

  

 

方案六:

对子盒子实现绝对定位,利用calc计算位置

复制代码
<p class="outerBox calc">
    </p><p class="innerBox">calc</p>
<p></p>


/*绝对定位,clac计算位置*/
.calc{
  position: relative;
}
.calc .innerBox{
  position: absolute;
  left:-webkit-calc((500px - 200px)/2);
  top:-webkit-calc((120px - 50px)/2);
  left:-moz-calc((500px - 200px)/2);
  top:-moz-calc((120px - 50px)/2);
  left:calc((500px - 200px)/2);
  top:calc((120px - 50px)/2);
}
复制代码

 

转:https://www.cnblogs.com/Julia-Yuan/p/6648816.html 

posted @ 2016-10-30 21:50  全栈测试笔记  阅读(141)  评论(0)    收藏  举报
浏览器标题切换
浏览器标题切换end