分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

CSS(3)实现水平垂直居中

发布时间:2023-09-06 01:54责任编辑:熊小新关键词:CSS

方法一:

div{ ???position: absolute; ???/*position: fixed 也可以*/ ???left: 0; ???right: 0; ???top: 0; ???bottom: 0; ???width: 100px; ???height: 100px; ???margin: auto; ???background-color: green;}
<div></div>

方法二:

div{ ???position: absolute; ???/*position: fixed 也可以*/ ???left: 50%; ???top: 50%; ???width: 100px; ???height: 100px; ???margin-left: -50px; ???margin-top: -50px; ???background-color: red;}
<div></div>

方法三:

div{ ???position: absolute; ???/*position: fixed 也可以*/ ???left: 50%; ???top: 50%; ???width: 100px; ???height: 100px; ???transform: translate(-50%,-50%); ???background-color: green;}
<div></div>

方法四:

body{ ???display: flex; ???justify-content: center;/*使内容水平居中*/ ???align-items: center;/*使内容垂直居中*/}div{ ???width: 100px; ???height: 100px; ???background-color: green;}
<div></div>

方法五:

div{ ???position: absolute; ???left: calc((100%-100px)/2); ???top: calc((100%-100px)/2); ???width: 100px; ???height: 100px; ???background-color: red;}
<div></div>

方法六:

.table{ ???display: table;/*此元素会作为块级表格来显示(类似 <table>),表格前后带有换行符*/ ???width: 100%; ???height: 100%; ???????????}.ok{ ???display: table-cell;/*此元素会作为一个表格单元格显示(类似 <td> 和 <th>)*/ ???text-align: center;/*水平居中*/ ???vertical-align: middle;/*垂直居中*/}.innerBox{ ???display: inline-block; ???width: 100px; ???height: 100px; ???background-color: red;}
<div class=‘table‘> ???<div class=‘ok‘> ???????<div class=‘innerBox‘></div> ???</div></div>

CSS(3)实现水平垂直居中

原文地址:https://www.cnblogs.com/fuyinjuan/p/9016987.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved