1.flex方法
<style> .flex{ width: 200px; height: 200px; box-sizing: border-box; border: 1px solid red; float: left; display: flex; justify-content: center; align-items: center; } .flex > div{ width: 100px; height: 100px; background-color: green; display: flex; justify-content: center; align-items: center; } </style> <body> <div class="flex"> <div>flex</div> </div> </body>
2.定位
<style> .ablot{ width: 200px; height: 200px; float: left; box-sizing: border-box; border: 1px solid red; position: relative; } .ablot > div{ width: 100px; height: 100px; background-color: green; text-align: center; line-height: 100px; position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; } </style> <bosy> <div class="ablot"> <div>定位</div> </div> </body>
<style> .ding{ width: 200px; height: 200px; float: left; box-sizing: border-box; border: 1px solid red; position: relative; } .ding > div{ width: 100px; height: 100px; background-color: green; text-align: center; line-height: 100px; position: absolute; left: 50%; top: 50%; margin: -50px 0 0 -50px; } </style> <body> <div class="ding"> <div>定位</div> </div> </body>
3.transform
<style> .transform{ width: 200px; height: 200px; float: left; box-sizing: border-box; border: 1px solid red; position: relative; } .transform > div{ width: 100px; height: 100px; background-color: green; text-align: center; line-height: 100px; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } </style> <body> <div class="transform"> <div>transform</div> </div> </body>
4.table-cell
<style> .table{ width: 200px; height: 200px; float: left; box-sizing: border-box; border: 1px solid red; display: table-cell; text-align: center; line-height: 200px; } .table > div{ width: 100px; height: 100px; display: inline-block; background-color: green; text-align: center; line-height: 100px; } </style> <body> <div class="table"> <div>table</div> </div> </body>
最后看效果
相关文章
- 01-15让div水平垂直居中的方法
- 01-15css元素水平垂直居中
- 01-15CSS布局--垂直水平居中
- 01-15div 水平垂直居中
- 01-15块状元素水平垂直居中
- 01-15CSS垂直居中
- 01-15img标签在div中水平垂直居中--两种实现方式
- 01-15如何让一个div垂直水平居中,分别用css和css3的多种方法实现
- 01-15当div有边框图片的时候,怎么实现内部的p标签的水平和垂直居中
- 01-15div中嵌套div水平居中,垂直居中