1. <center>

text-align:center

在父容器里水平居中 inline 文字,或 inline 元素


  1. vertical-align:middle

垂直居中 inline 文字,inline 元素,配合 display:table ,display:table-cell,有奇效。


  1. line-height

与 height 联手,垂直居中文字


  1. margin:auto

示例:

  1. <``style``>
  2. #ex2_container { width:200px; background-color:yellow; }
  3. #ex2_content { margin:0px auto; background-color:gray; color:white; display:table; }
  4. </``style``>
  5. <``div id="ex2_container"><``div id="ex2_content">Hello World</``div``></``div``>

hacks, hacks(小技巧)

有许多 hacks,负 margin,影子元素 ::before 等。如果你的内容不是固定大小的话,它们大部分是很脆弱的。


  1. translate(-50%,-50%)

用 position 加 translate translate(-50%,-50%) 比较奇特,百分比计算不是以父元素为基准,而是以自己为基准。

示例:


  1. <``style``>
  2. #ex3_container { width:200px; height:200px; background-color:yellow; position:relative; }
  3. #ex3_content { left:50%; top:50%; transform:translate(-50%,-50%); -webkit-transform:translate(-50%,-50%); background-color:gray; color:white; position:absolute; }
  4. </``style``>
  5. <``div id="ex3_container"><``div id="ex3_content">Hello World</``div``></``div``>

这个技巧相当嚣张,同样适用于没固定大小的内容,min-width,max-height,overflow:scroll 等。

绝对定位居中

父容器元素:position: relative


  1. .Absolute-Center {
  2. width``: 50%``;
  3. height``: 50%``;
  4. overflow``: auto``;
  5. margin``: auto``;
  6. position``: absolute``;
  7. top``: 0``; left``: 0``; bottom``: 0``; right``: 0``;
  8. }

注意:高度必须定义,建议加 overflow: auto,防止内容溢出。

视口居中

内容元素:position: fixed,z-index: 999,记住父容器元素 position: relative


  1. .Absolute-Center.is-Fixed {
  2. width``: 50%``;
  3. height``: 50%``;
  4. overflow``: auto``;
  5. margin``: auto``;
  6. position``: fixed``;
  7. top``: 0``; left``: 0``; bottom``: 0``; right``: 0``;
  8. z-index``: 999``;
  9. }

响应式

百分比宽高,最大、最小宽度均可以,加 padding 也可以


  1. .Absolute-Center.is-Responsive {
  2. width``: 60%``;
  3. height``: 60%``;
  4. min-width``: 400px``;
  5. max-width``: 500px``;
  6. padding``: 40px``;
  7. overflow``: auto``;
  8. margin``: auto``;
  9. position``: absolute``;
  10. top``: 0``; left``: 0``; bottom``: 0``; right``: 0``;
  11. }

偏移

只要 margin: auto; 在,内容块将垂直居中,top, left, bottom, right 可以设置偏移。


  1. .Absolute-Center.is-Right {
  2. width``: 50%``;
  3. height``: 50%``;
  4. margin``: auto``;
  5. overflow``: auto``;
  6. position``: absolute``;
  7. top``: 0``; left``: auto``; bottom``: 0``; right``: 20px``;
  8. text-align``: right``;
  9. }

溢出

居中内容比父容器高时,防止溢出,加 overflow: auto (没有任何 padding 时,也可以加 max-height: 100%;)。


  1. .Absolute-Center.is-Overflow {
  2. width``: 50%``;
  3. height``: 300px``;
  4. max-height``: 100%``;
  5. margin``: auto``;
  6. overflow``: auto``;
  7. position``: absolute``;
  8. top``: 0``; left``: 0``; bottom``: 0``; right``: 0``;
  9. }

调整尺寸

resize 属性可以让尺寸可调。 设置 min- /max- 限制尺寸,确定加了 overflow: auto 。


  1. .Absolute-Center.is-Resizable {
  2. min-width``: 20%``;
  3. max-width``: 80%``;
  4. min-height``: 20%``;
  5. max-height``: 80%``;
  6. resize: both``;
  7. overflow``: auto``;
  8. margin``: auto``;
  9. position``: absolute``;
  10. top``: 0``; left``: 0``; bottom``: 0``; right``: 0``;
  11. }

图像

图像同样适用,设置 height: auto;


  1. .Absolute-Center.is-Image {
  2. width``: 50%``;
  3. height``: auto``;
  4. margin``: auto``;
  5. position``: absolute``;
  6. top``: 0``; left``: 0``; bottom``: 0``; right``: 0``;
  7. }

可变高度

高度必须定义,但可以是百分比或 max-height。不想定义高度的话,用 display: table (需要考虑 Table-Cell 兼容性)。


  1. .Absolute-Center.is-Variable {
  2. display``: table;
  3. width``: 50%``;
  4. overflow``: auto``;
  5. margin``: auto``;
  6. position``: absolute``;
  7. top``: 0``; left``: 0``; bottom``: 0``; right``: 0``;
  8. }

负 margin

确切知道宽高,负 margin 是宽和高的一半。


  1. .is-Negative {
  2. width``: 300px``;
  3. height``: 200px``;
  4. padding``: 20px``;
  5. position``: absolute``;
  6. top``: 50%``; left``: 50%``;
  7. margin-left``: -170px``; /* (width + padding)/2 */
  8. margin-top``: -120px``; /* (height + padding)/2 */
  9. }

Table-Cell

结构:


  1. <``div class="Pos-Container is-Table">
  2. <``div class="Table-Cell">
  3. <``div class="Center-Block">
  4. ``
  5. </``div``>
  6. </``div``>
  7. </``div``>

样式:


  1. .Pos-Container.is-Table { display``: table; }
  2. .is-Table .Table-Cell {
  3. display``: table-cell``;
  4. vertical-align``: middle``;
  5. }
  6. .is-Table .Center-Block {
  7. width``: 50%``;
  8. margin``: 0 auto``;
  9. }

FlexBox


  1. .Pos-Container.is-Flexbox {
  2. display``: -webkit-box;
  3. display``: -moz-box;
  4. display``: -ms-flexbox;
  5. display``: -webkit-flex;
  6. display``: flex;
  7. -webkit-box-align: center``;
  8. -moz-box-align: center``;
  9. -ms-flex-align: center``;
  10. -webkit-align-items: center``;
  11. align-items: center``;
  12. -webkit-box-pack: center``;
  13. -moz-box-pack: center``;
  14. -ms-flex-pack: center``;
  15. -webkit-justify-``content``: center``;
  16. justify-``content``: center``;
  17. }

这里推荐一下我的前端学习交流群:784783012,里面都是学习前端的,如果你想制作酷炫的网页,想学习编程。自己整理了一份2018最全面前端学习资料,从最基础的HTML+CSS+JS【炫酷特效,游戏,插件封装,设计模式】到移动端HTML5的项目实战的学习资料都有整理,送给每一位前端小伙伴,有想学习web前端的,或是转行,或是大学生,还有工作中想提升自己能力的,正在学习的小伙伴欢迎加入学习。

点击:加入

原文地址:https://segmentfault.com/a/1190000016896535

CSS 居中【整合】的更多相关文章

  1. vue—你必须知道的 js数据类型 前端学习 CSS 居中 事件委托和this 让js调试更简单—console AMD && CMD 模式识别课程笔记(一) web攻击 web安全之XSS JSONP && CORS css 定位 react小结

    vue—你必须知道的   目录 更多总结 猛戳这里 属性与方法 语法 计算属性 特殊属性 vue 样式绑定 vue事件处理器 表单控件绑定 父子组件通信 过渡效果 vue经验总结 javascript ...

  2. CSS居中demo

      <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...

  3. css居中那点事儿

    css居中那点事儿 在css中对元素进行水平居中是非常简单的,然而使元素垂直居中就不是一件简单的事情了,多年以来,垂直居中已经成为了CSS领域的圣杯,因为它是极其常见的需求,但是在实践中却不是一件简单 ...

  4. css居中学习笔记

    css居中学习笔记 一.水平居中 以下面的代码为例: <body> <div class="parent"> <div class="chi ...

  5. CSS居中完全解决方案

    上次面试面试官问到了,问了个定宽局中和不定宽局中,下来我把所有有关CSS居中都总结了一下 原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 水平居中 行内元素 把行内元素 ...

  6. 理解CSS居中

    我想很多在前端学习或者开发过程中,肯定会遇到如何让你的元素居中的问题,网上google肯定会有很多的解决方法.今天我就个人的项目与学习经验谈谈个人理解css如何让元素居中. 要理解css的居中,首先必 ...

  7. CSS 居中大全【转】

    我看最近微博流行 CSS 居中技术,老外码农争相写相关的文章,一篇赛一篇的长啊,我把几篇归纳总结了一下,算是笔记. 孔乙己曾说:“茴香豆的回字有四种写法”,万一哪天有个面试官问你:“居中一共有几种写法 ...

  8. CSS居中的实现用法实例

    转载的一篇文章,讲解css内容居中的. 网上有关css 居中的文章不胜枚举,不过大多没有做系统的总结.这里分享的这篇有关css居中的文章,个人感觉不错,值得收藏. 一.水平居中1,将元素水平居中(us ...

  9. 各种div+css居中方式调整(转载)

    盘点8种CSS实现垂直居中水平居中的绝对定位居中技术 分类: 前端开发2013-09-11 21:06 24959人阅读 评论(3) 收藏 举报 绝对居中垂直居中水平居中CSS居中代码   目录(?) ...

  10. CSS居中方法

    css居中方法非常多,根据工作的实际情况采用恰当方法,可取到事半功倍的效果. 就常见的一些居中方法整理如下: 代码如下: <div class="con"> <d ...

随机推荐

  1. hdu1203--D - I NEED A OFFER!(转化01背包)

    D - I NEED A OFFER! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  2. 多版本号并发控制(MVCC)在实际项目中的应用

    近期项目中遇到了一个分布式系统的并发控制问题.该问题能够抽象为:某分布式系统由一个数据中心D和若干业务处理中心L1,L2 - Ln组成:D本质上是一个key-value存储,它对外提供基于HTTP协议 ...

  3. CLLocationManagerDelegate的解说

    1.//新的方法.登陆成功之后(旧的方法就无论了) - (void)locationManager:(CLLocationManager *)manager      didUpdateLocatio ...

  4. Kinect for Windows SDK v2.0 开发笔记 (十五) 手势帧

     (转载请注明出处) 使用SDK: Kinect for Windows SDK v2.0 public preview1409 同前面,由于SDK未完毕,不附上函数/方法/接口的超链接. 这次最 ...

  5. C语言播放声音最简单的两种方法

    1. 假设仅须要播放波形文件wav格式的声音,非常easy.仅仅需一句话: PlaySound(TEXT("Data\\1.wav"), NULL, SND_FILENAME | ...

  6. luogu3369 【模板】 普通平衡树 Splay

    题目大意 维护一个数据结构,满足以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多个相同的数,因输出最小的排名) 查询排名为x ...

  7. SwiftUI 官方教程(四)

    SwiftUI 官方教程(四) 4. 自定义 Image View 搞定名称和位置 view 后,我们来给地标添加图片. 这不需要添加很多代码,只需要创建一个自定义 view,然后给图片加上遮罩.边框 ...

  8. (转)es6用法

    如何在浏览器中使用es6的语法呢? 方法一:引入文件相关js文件 <script src="traceur.js"></script> <script ...

  9. jQueryDOM操作模块(二)

    DOM模块 1.优化框架结构 目标:在实现功能基础上优化代码使得框架更简单易用 1.1 简化存储DOM元素的容器 - elements 目标:使用 this 作为容器 1.1.1 使用 element ...

  10. GEF入门笔记

    最近项目中需要用到Eclipse GEF框架进行画图,故将平时学习笔记更新到博客中,便于查阅 自己画的一个GEF基本结构     最基本流程 1.创建model(包括数据域.在界面中的布局.图片索引等 ...