如何居中div?
如何居中div?
水平居中

1 //给div设置一个宽度,然后添加margin:0 auto属性
2
3 div{
4 width:200px;
5 margin:0 auto;
6 }

让绝对定位的div居中

1 div {
2 position: absolute;
3 width: 300px;
4 height: 300px;
5 margin: auto;
6 top: 0;
7 left: 0;
8 bottom: 0;
9 right: 0;
10 background-color: pink; /* 方便看效果 */
11 }

水平垂直居中一

1 //确定容器的宽高 宽500 高 300 的层
2 //设置层的外边距
3
4 div {
5 position: relative; /* 相对定位或绝对定位均可 */
6 width:500px;
7 height:300px;
8 top: 50%;
9 left: 50%;
10 margin: -150px 0 0 -250px; /* 外边距为自身宽高的一半 */
11 background-color: pink; /* 方便看效果 */
12
13 }

水平垂直居中二

1 // 未知容器的宽高,利用 `transform` 属性
2
3 div {
4 position: absolute; /* 相对定位或绝对定位均可 */
5 width:500px;
6 height:300px;
7 top: 50%;
8 left: 50%;
9 transform: translate(-50%, -50%);
10 background-color: pink; /* 方便看效果 */
11
12 }

水平垂直居中三

1 //利用 flex 布局
2 //实际使用时应考虑兼容性
3
4 .container {
5 display: flex;
6 align-items: center; /* 垂直居中 */
7 justify-content: center; /* 水平居中 */
8
9 }
10 .container div {
11 width: 100px;
12 height: 100px;
13 background-color: pink; /* 方便看效果 */
14 }

如何居中div?的更多相关文章
- 随滚动条滚动的居中div
<!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- 如何居中div?如何居中一个浮动元素?
如何居中div? 给div设置一个宽度,然后添加margin:0 auto属性 div{ width:200px; margin:0 auto; } 如何居中一个浮动元素? <!DOCTYPE ...
- css居中div的几种常用方法
在开发过程中,很多需求需要我们居中一个div,比如html文档流当中的一块div,比如弹出层内容部分这种脱离了文档流等.不同的情况有不同的居中方式,接下来就分享下一下几种常用的居中方式. 1.text ...
- 屏幕居中(DIV/CSS) 的几种方法(转)
1.利用table特性,在width and height all set 100%的时候,可以把容器嵌套在td内形成绝对居中,此时的被嵌套容器可为绝对或者相对大小.(非标准)注意不能加声明! 如是在 ...
- 居中div,居中浮动的元素
定位法:position:absolute 如果子级div有定义宽和高的话就可以用这个方法.注意:margin-top,和margin-left的值均为高和宽值的一半 margin:auto法 这个也 ...
- 通过css 居中div的几种常用方法
1.text-align:center方式 .center{ text-align:center; } center_text{ display:inline-block; width:500px } ...
- css div上下左右居中
相信大家都会遇到这样的问题,要求一个块上下左右居中,在这里我总结了几个好用的方法 1.已知要居中的块width height 假设 content 要在f里上下左右居中 <div class= ...
- 单个未知大小图片在div里面垂直居中的方法。。。添加辅助元素挤一下位置达到居中
单个未知大小图片在div里面垂直居中的方法...添加辅助元素挤一下位置达到居中 <div class="ServicesLiTopPic"> <i>&l ...
- [转]div里table居中的问题 Div与body顶部间隙
本文转自:http://www.cnblogs.com/jinhui/archive/2008/09/24/1297729.html 将div的text-align设为center,然后将table的 ...
随机推荐
- iOS -- 解决iOS11中navigationBar上使用initWithCustomView按钮图片错位 frame无效
在iOS11上当使用如下代码设置时 UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)]; shareButto ...
- Centos7下ZABBIX安装全记录
安装之前务必关闭SELINUX Install Repository with MySQL database : rpm -i https://repo.zabbix.com/zabbix/3.4/r ...
- python3+django使用celery执行某些任务失败的解决方案
.在celery 的worker启动窗口设置export PYTHONOPTIMIZE=1 export PYTHONOPTIMIZE=1 /usr/local/python36/bin/celery ...
- git新建和删除远程分支
创建远程分支: 新建本地分支 git checkout -b branch_name 推送到远程分支,分支名字和本地分支名字相同 git push origin branch_name:branch_ ...
- 怎样用bat批量重命名文件夹和文件
很早以前本人写过重命名文件夹的文章,发现其中稍有不完善的地方,其主要功能在文件夹名前统一加上字符,或者在文件夹名后统一加上字符,有网友反应功能太单一.今天我又仔细研究了一下bat批处理代码,分别能完全 ...
- NTFS数据流和web安全
NTFS流简单介绍: NTFS因为它的稳定性 强大的功能 以及它所提供的安全性而成为一种更优越的文件系统,NTFS交换数据流(ADSs)是为了和Macintosh的HFS文件系统兼容而设计的,它使用资 ...
- java的poi技术读取Excel[2003-2007,2010]
这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...
- 项目笔记:导出Excel功能
1.前台这块: var ids=""; $.post("${basePath}/assets/unRegDeviceAction_getDeviceIds.do" ...
- HDU 4632 Palindrome subsequence(区间dp)
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ...