居中布局

<div class=”parent”>

<div class=”child”>demo</div>

</div>

1.水平居中

1>     方案一 inlink-block+text-align

.child {display:inlink-block;}

.parent {text-align:center;}

优点:兼容性好

缺点:子元素宽高不可设置

2>     方案二 table+margin

.child {display:table; margin:0 auto;}

优点:只需要设置child(自己)

3>     方案三 absolute+transform

.parent {position:relative;}

.child {position:absolute;left:50%;transform:translateX(-50%);}

优点:子元素不会对其他元素产生影响

缺点:兼容性问题

4>     方案四 flex+justify-content

.parent {display:flex;justify-content:center;}

优点:只需设置parent节点

或者设置为

.parent {display:flex;}

.child {margin:0 auto;}

缺点:flex低版本ie不支持

2.垂直居中

1>     方案一 table-cell+vertical-align

.parent {display:table-cell;vertical-align:middle;}

优点:只需设置父节点,兼容性好

2>     方案二 absolute+transform

.parent {position:relative;}

.child {position:absolute;top:50%;transform:translateY(-50%);}

优点:子元素不会对其他元素产生影响

缺点:兼容性问题

3>     方案三 flex+align-items

.parent {display:flex;align-items:center;}

优点:只需设置父节点

缺点:兼容性问题

3.水平和垂直均居中

1>     方案一 inline-block+text-align+table-cell+vertical-align

.parent {text-align:center;display:table-cell;vertical-align:middle;}

.child {display:inline-block;}

2>     方案二 absolute+transform

.parent {position:relative;}

.child { position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);}

3>     方案三 flex+justify-content+align-items

.parent {display:flex;justify-content:center;align-items:center;}

思路:了解属性值特性,分解问题。

转载自:http://www.cnblogs.com/xiaohangzi/p/6090995.html

CSS居中布局总结【转】的更多相关文章

  1. CSS居中布局方案

    基本结构 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  2. CSS居中布局总结

    居中布局 <div class="parent"> <div class="child">demo</div> </d ...

  3. 详解 CSS 居中布局技巧

    水平居中元素: 通用方法,元素的宽高未知 方式一:CSS3 transform .parent { position: relative; } .child { position: absolute; ...

  4. 详解CSS居中布局技巧

    本文转自:https://zhuanlan.zhihu.com/p/25068655#showWechatShareTip一.水平居中元素: 1.通用方法,元素的宽高未知方式一:CSS3 transf ...

  5. CSS 居中布局

    来源:http://www.cnblogs.com/QianBoy/p/8539077.html 水平居中 1)使用inline-block+text-align 原理:先将子框由块级元素改变为行内块 ...

  6. css居中布局的几种方式

    一.水平居中 若是行内元素,则直接给其父元素设置text-align: center即可 若是块级元素,则直接给该元素设置margin: 0 auto即可 若子元素包含浮动元素,则给父元素设置widt ...

  7. CSS居中布局

    一:水平居中方案: 1.行内元素 设置 text-align:center 2.定宽块状元素 设置 左右 margin 值为 auto 3.不定宽块状元素 a:在元素外加入 table 标签(完整的, ...

  8. css 居中布局方案

    position(transform css3  有些浏览器不兼容) <article id="one"> <section id="section&q ...

  9. CSS基础布局--居中对齐,左侧定宽右侧自适应

    CSS页面布局是web前端开发的最基本的技能,本文将介绍一些常见的布局方法,涉及到盒子布局,column布局,flex布局等内容.本文中,你可以看到一些水平垂直居中的方法,左侧固定宽度,右侧自适应的一 ...

随机推荐

  1. service和serviceImpl的选择

    同行中,有些同行公司的代码风格是service层=service接口+serviceImpl实现类: 而有的同行公司的代码风格是service层=service类: 为什么不一样呢? 以前没想过这个问 ...

  2. Excel—身份证生日提取

    一.只有18位的身份证号码 如: 身份证号 330682199302264000 41120019890823729X 231081199002256839 131101198203154666 36 ...

  3. codevs2645 Spore

    题目描述 Description 某陈和某Y 最近对一个游戏着迷.那是Electronic Arts 今年发布的优秀的模拟经营类游戏,Spore. 在Spore 中,玩家将经历从单细胞生物到星系的统治 ...

  4. mysql登录报错“Access denied for user 'root'@'localhost' (using password: YES”的处理方法

    使用/etc/mysql/debian.cnf文件中[client]节提供的用户名和密码: 文件内容: [client]host = localhostuser = debian-sys-maint ...

  5. MySQL关键字

    MySQL关键字 ADD ALL ALTER ANALYZE AND AS ASC ASENSITIVE BEFORE BETWEEN BIGINT BINARY BLOB BOTH BY CALL ...

  6. 优化MySQL数据库性能的八大方法

    本文探讨了提高MySQL 数据库性能的思路,并从8个方面给出了具体的解决方法. 1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就 ...

  7. shell--2.shell数组

    shell 数组 (1)定义数组 shell中,用括号表示数组,数组元素用空格分开,定义数组的一般形式 arrt_name=(val1 val2 val3) 或者 arry_name=(val1 va ...

  8. 使用bootstrap tooltip控件动态修改提示内容

    初始化控件之后即使修改了元素的title内容也不会更改提示信息,比如下面 $(element).attr('title','XXXXXX') 这样只会增加一个原生的title提示,如果保持原样显示必须 ...

  9. FreeImage编译及遇到问题解决

    FreeImage编译及遇到问题解决 1.下载freeImage源码包 wget http://downloads.sourceforge.net/freeimage/FreeImage3170.zi ...

  10. appium 处理动态控件

    环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html 知乎Android客户端登陆:http://www.cnblogs.com/tobe ...