博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
让元素水平和垂直居中的方法总结
阅读量:4699 次
发布时间:2019-06-09

本文共 1047 字,大约阅读时间需要 3 分钟。

1.绝对定位和固定定位的方法

.div1{  width:100px;  height:100px;  position:absolute;  left:50%;  top:50%;  margin-left:-50px;  margin-top:-50px;  background: red;}

 

2.绝对定位和固定定位加margin:auto实现

.div1{  position:absolute;  width:100px;  height:100px;  margin:auto;  left:0;  top:0;  right:0;  bottom:0;  background: green;}

3.table-cell实现

  .box{            width: 300px;            height: 300px;          display: table-cell;            vertical-align: middle;            text-align: center;            border:1px solid #000;        }.box div{          width:100px;            height:100px;            margin:0 auto;            background-color:red;        }

4.针对行内元素

div{        height:100px;        text-align:center;        line-height:100px;    }

5.利用css3的属性flex

.box{

  width: 500px;
  height: 500px;
  display:flex;
  justify-content:center;
  align-items:center;
  border: 1px solid #666;
}
.div1{
  width:100px;
  height:100px;
  background-color:red;
}

<div class="box">

  <div class="div1"></div>
</div>

转载于:https://www.cnblogs.com/dglblog/p/8572378.html

你可能感兴趣的文章
Java中JavaScript unescape与escape函数算法
查看>>
js的基础要点
查看>>
C#/IOS/Android通用加密解密方法
查看>>
Web API 简单示例
查看>>
返璞归真 asp.net mvc (4) - View/ViewEngine
查看>>
ADO.Net对Oracle数据库的操作【转载】
查看>>
Contoso 大学 - 6 – 更新关联数据
查看>>
RESTful API 设计指南
查看>>
Windows 10正式版的历史版本
查看>>
hdu4057Rescue the Rabbit(ac自动机+dp)
查看>>
五个实用又有趣的网站
查看>>
并发之初章Java内存模型
查看>>
ThreadLocal可以解决并发问题吗?
查看>>
爬虫抓取表格中的数据
查看>>
Jfinal集成Spring
查看>>
JQuery操作class
查看>>
Android 4.0 NDK Updated
查看>>
SAP FI配置步骤
查看>>
Basic Level 1006. 换个格式输出整数 (15)
查看>>
josephus问题
查看>>