1.盒子水平垂直居中的方案
方案1-定位
<link rel="stylesheet" href="../../css/reset.min.css">
<style>
html,
body{
height: 100%;
overflow: hidden;
}
.box{
box-sizing: border-box;
width: 100px;
height: 50px;
line-height: 48px;
text-align: center;
font-size: 16px;
border: 1px solid skyblue;
background: plum;
}
/* 定位 */
body{
position: relative;
}
.box{
/* 子绝父相:让子元素相对于父元素定位 */
position: absolute;
/* 让盒子左上角垂直水平居中 */
top: 50%;
left: 50%;
/* 让盒子往左上角移动自身宽高的50% */
margin-top: -25px;
margin-left: -25px;
/* 缺点:必须得知道盒子的宽高 */
}
</style>
</head>
<body>
<div class="box" id="box"> box1 </div>
</body>
方案2-定位
<link rel="stylesheet" href="../../css/reset.min.css">
<style>
html,
body{
height: 100%;
overflow: hidden;
}
.box{
box-sizing: border-box;
width: 100px;
height: 50px;
line-height: 48px;
text-align: center;
font-size: 16px;
border: 1px solid skyblue;
background: plum;
}
/* 定位 */
body{
position: relative;
}
.box{
/* 子绝父相:让子元素相对于父元素定位 */
position: absolute;
/* 让盒子垂直水平居中 */
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
/*
优点:无需计算宽高
缺点:必须得有盒子的宽高
*/
}
</style>
</head>
<body>
<div class="box" id="box"> box1 </div>
</body>
方案3-定位
<link rel="stylesheet" href="../../css/reset.min.css">
<style>
html,
body{
height: 100%;
overflow: hidden;
}
.box{
box-sizing: border-box;
width: 100px;
height: 50px;
line-height: 48px;
text-align: center;
font-size: 16px;
border: 1px solid skyblue;
background: plum;
}
/* 定位 */
body{
position: relative;
}
.box{
/* 子绝父相:让子元素相对于父元素定位 */
position: absolute;
/* 让盒子左上角垂直水平居中 */
top: 50%;
left: 50%;
/* 让盒子往左上角移动自身宽高的50% */
transform: translate(-50%,-50%);
/*
优点:无需计算宽高,不需要具体宽高
缺点:不兼容
*/
}
</style>
</head>
<body>
<div class="box" id="box"> box1 </div>
</body>
方案4-display:flex
<link rel="stylesheet" href="../../css/reset.min.css">
<style>
html,
body{
height: 100%;
overflow: hidden;
}
.box{
box-sizing: border-box;
width: 100px;
height: 50px;
line-height: 48px;
text-align: center;
font-size: 16px;
border: 1px solid skyblue;
background: plum;
}
body{
/* 让子元素在主轴、侧轴居中排列*/
display: flex;
justify-content: center;
align-items: center;
/* 缺点:不兼容,移动端适用 */
}
</style>
</head>
<body>
<div class="box" id="box"> box1 </div>
</body>
方案6-js
<link rel="stylesheet" href="../../css/reset.min.css">
<style>
html,
body{
position: relative;
height: 100%;
overflow: hidden;
}
.box{
position: absolute;
box-sizing: border-box;
width: 100px;
height: 50px;
line-height: 48px;
text-align: center;
font-size: 16px;
border: 1px solid skyblue;
background: plum;
}
</style>
</head>
<body>
<div class="box" id="box"> box1 </div>
<script>
/*
获得屏幕与当前盒子的宽和高,用(当前屏幕的宽和高-盒子的宽和高)/2
*/
let html = document.documentElement,
winW = html.clientWidth,
winH = html.clientHeight,
boxW = box.offsetWidth,
boxH = box.offsetHeight;
box.style.left = (winW - boxW) / 2 + 'px';
box.style.top = (winH - boxH) / 2 + 'px';
</script>
</body>
*方案7-display:table-cell(一般不使用) *
.father{
display: table-cell;
vertical-align: middle;
text-align: center;
//=>宽高不能是百分比
}
重置样式表
reset.min.css
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,button,input,textarea,th,td{margin:0;padding:0}body{font-size:12px;font-style:normal;font-family:"\5FAE\8F6F\96C5\9ED1",Helvetica,sans-serif}small{font-size:12px}h1{font-size:18px}h2{font-size:16px}h3{font-size:14px}h4,h5,h6{font-size:100%}ul,ol{list-style:none}a{text-decoration:none;background-color:transparent}a:hover,a:active{outline-width:0;text-decoration:none}table{border-collapse:collapse;border-spacing:0}hr{border:0;height:1px}img{border-style:none}img:not([src]){display:none}svg:not(:root){overflow:hidden}html{-webkit-touch-callout:none;-webkit-text-size-adjust:100%}input,textarea,button,a{-webkit-tap-highlight-color:rgba(0,0,0,0)}article,aside,details,figcaption,figure,footer,header,main,menu,nav,section,summary{display:block}audio,canvas,progress,video{display:inline-block}audio:not([controls]),video:not([controls]){display:none;height:0}progress{vertical-align:baseline}mark{background-color:#ff0;color:#000}sub,sup{position:relative;font-size:75%;line-height:0;vertical-align:baseline}sub{bottom:-0.25em}sup{top:-0.5em}button,input,select,textarea{font-size:100%;outline:0}button,input{overflow:visible}button,select{text-transform:none}textarea{overflow:auto}button,html [type="button"],[type="reset"],[type="submit"]{-webkit-appearance:button}button::-moz-focus-inner,[type="button"]::-moz-focus-inner,[type="reset"]::-moz-focus-inner,[type="submit"]::-moz-focus-inner{border-style:none;padding:0}button:-moz-focusring,[type="button"]:-moz-focusring,[type="reset"]:-moz-focusring,[type="submit"]:-moz-focusring{outline:1px dotted ButtonText}[type="checkbox"],[type="radio"]{box-sizing:border-box;padding:0}[type="number"]::-webkit-inner-spin-button,[type="number"]::-webkit-outer-spin-button{height:auto}[type="search"]{-webkit-appearance:textfield;outline-offset:-2px}[type="search"]::-webkit-search-cancel-button,[type="search"]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-input-placeholder{color:inherit;opacity:.54}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}.clearfix:after{display:block;height:0;content:"";clear:both}