如何居中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?的更多相关文章

  1. 随滚动条滚动的居中div

    <!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  2. 如何居中div?如何居中一个浮动元素?

    如何居中div? 给div设置一个宽度,然后添加margin:0 auto属性 div{ width:200px; margin:0 auto; } 如何居中一个浮动元素? <!DOCTYPE ...

  3. css居中div的几种常用方法

    在开发过程中,很多需求需要我们居中一个div,比如html文档流当中的一块div,比如弹出层内容部分这种脱离了文档流等.不同的情况有不同的居中方式,接下来就分享下一下几种常用的居中方式. 1.text ...

  4. 屏幕居中(DIV/CSS) 的几种方法(转)

    1.利用table特性,在width and height all set 100%的时候,可以把容器嵌套在td内形成绝对居中,此时的被嵌套容器可为绝对或者相对大小.(非标准)注意不能加声明! 如是在 ...

  5. 居中div,居中浮动的元素

    定位法:position:absolute 如果子级div有定义宽和高的话就可以用这个方法.注意:margin-top,和margin-left的值均为高和宽值的一半 margin:auto法 这个也 ...

  6. 通过css 居中div的几种常用方法

    1.text-align:center方式 .center{ text-align:center; } center_text{ display:inline-block; width:500px } ...

  7. css div上下左右居中

    相信大家都会遇到这样的问题,要求一个块上下左右居中,在这里我总结了几个好用的方法 1.已知要居中的块width height 假设  content 要在f里上下左右居中 <div class= ...

  8. 单个未知大小图片在div里面垂直居中的方法。。。添加辅助元素挤一下位置达到居中

    单个未知大小图片在div里面垂直居中的方法...添加辅助元素挤一下位置达到居中   <div class="ServicesLiTopPic"> <i>&l ...

  9. [转]div里table居中的问题 Div与body顶部间隙

    本文转自:http://www.cnblogs.com/jinhui/archive/2008/09/24/1297729.html 将div的text-align设为center,然后将table的 ...

随机推荐

  1. WebService authentication

    http://blog.csdn.net/largestone_187/article/details/5734632 通过SoapHeader对用户口令进行验证,只有授权的用户才可以使用接口.确保了 ...

  2. C#基础之 派生类

    1: 当创建派生类的实例时,会自动调用基类的默认构造函数 namespace parentTest { public class Reader { public Reader() { Console. ...

  3. [Android Memory] Android内存管理、监测剖析

    转载自:http://blog.csdn.net/anlegor/article/details/23398785 Android内存管理机制: Android内存管理主要有:LowMemory Ki ...

  4. dependency:copy-dependencies使用,如何排除应用自身module

    相信用法参考:https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html#includeG ...

  5. builder pattern

    design patterns 结合书本和这个网站的这个系列的文章来看: https://www.tutorialspoint.com/design_pattern/builder_pattern.h ...

  6. c# word 操作

    public class WordOperate { #region 新建Word文档 /// <summary> /// 动态生成Word文档并填充内容 /// </summary ...

  7. nyoj237 游戏高手的烦恼(最小点覆盖)

    题目237 题目信息 pid=237" style="text-decoration:none; color:rgb(55,119,188)">执行结果 本题排行 ...

  8. 淘宝Diamond架构分析

    转载:http://blog.csdn.net/szwandcj/article/details/51165954 早期的应用都是单体的,配置修改后,只要通过预留的管理界面刷新reload即可.后来, ...

  9. oracle调优 浅析有效的游标管理

    浅析有效的游标管理 [思路分析] 能够把游标理解成共享的运行计划,当sql不被共享时.常规的解决思路有两个方向: 1.调整共享池的尺寸(共享池的库缓存区中共享运行计划): 2.sql书写时尽量重用绑定 ...

  10. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-PLC支持哪些PLC语言类型

    PLC的标准化语言,统称为IEC 61131-3,该规范下有五种不同的语言可以创建PLC程序,TwinCAT都支持. IL(指令列表): 每条指令都从一个新行开始并包含一个操作和一个或多个操作数,一条 ...