renpinghao 2019-06-28
非常简洁易懂,排名不分先后,开整!
HTML:
<div class="main"> <div class="content"></div> </div>
.main { position: relative; width: 800px; height: 500px; background-color: lightblue; } .content { position: absolute; width: 300px; height: 200px; top: 50%; left: 50%; margin-left: -150px; margin-top: -100px; background: lightpink; }
.main { position: relative; width: 800px; height: 500px; background-color: lightblue; } .content { position: absolute; width: 300px; height: 200px; top: 50%; left: 50%; transform: translate(-50%, -50%); background: lightpink; }
.main { position: relative; width: 800px; height: 500px; background-color: lightblue; } .content { position: absolute; width: 300px; height: 200px; top: 0; left: 0; right: 0; bottom: 0; background: lightpink; margin: auto; }
.main { width: 800px; height: 500px; background: lightblue; display: table-cell; /*标配*/ vertical-align: middle; /*标配*/ } .content { width: 300px; height: 200px; background: lightpink; margin: auto; /*标配*/ } /*三个标配一起使用才能发挥作用*/
.main { width: 800px; height: 500px; background-color: lightblue; } .content { width: 300px; height: 70px; background: lightpink; margin: auto; position: relative; top: calc((100% - 70px)/2); }
.main { display: flex; width: 800px; height: 500px; background-color: lightblue; justify-content: center; align-items: center; } .content { width: 300px; height: 200px; background: lightpink; }
.main { display: flex; width: 800px; height: 500px; background-color: lightblue; justify-content: center; text-align: center; } .content { width: 300px; height: 200px; background: lightpink; align-self: center; }
.main { display: flex; width: 800px; height: 500px; background-color: lightblue; } .content { width: 300px; height: 200px; background: lightpink; margin: auto; }
.main { display: grid; width: 800px; height: 500px; background-color: lightblue; justify-content: center; align-content: center; } .content { width: 300px; height: 200px; background: lightpink; }
.main { display: grid; width: 800px; height: 500px; background-color: lightblue; justify-content: center; align-items: center; } .content { width: 300px; height: 200px; background: lightpink; }
.main { display: grid; width: 800px; height: 500px; background-color: lightblue; justify-content: center; } .content { width: 300px; height: 200px; background: lightpink; align-self: center; }
.main { width: 800px; height: 500px; display: grid; background: lightblue; } .content { width: 300px; height: 200px; margin: auto; background: lightpink; }
.main { width: 800px; height: 500px; display: grid; place-content: center; /*这是justy-content与align-items的结合写法*/ background-color: lightblue; } .content { width: 300px; height: 200px; background: lightpink; }
.main { display: grid; width: 800px; height: 500px; background-color: lightblue; place-items: center; } .content { width: 300px; height: 200px; background: lightpink; }
.main { margin: auto; width: 800px; height: 500px; background-color: lightblue; display: grid; grid-template-columns: 1fr auto 1fr; grid-template-rows: 1fr auto 1fr; grid-template-areas: '. . .' '. amos .' '. . .'; } .content { width: 300px; height: 200px; background: lightpink; grid-area: amos; }
好啦,十五种方法Get!