<center>

text-align:center

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


vertical-align:middle

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


line-height

与 height 联手,垂直居中文字


margin:auto

示例:



<``style``>

#ex2_container { width:200px; background-color:yellow; }

#ex2_content { margin:0px auto; background-color:gray; color:white; display:table; }

</``style``>

<``div id="ex2_container"><``div id="ex2_content">Hello World</``div``></``div``>

hacks, hacks(小技巧)

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


translate(-50%,-50%)

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

示例:


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

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

绝对定位居中

父容器元素:position: relative


.Absolute-Center { width``: 50%``; height``: 50%``; overflow``: auto``; margin``: auto``; position``: absolute``; top``: 0``; left``: 0``; bottom``: 0``; right``: 0``; }

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

视口居中

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


.Absolute-Center.is-Fixed { width``: 50%``; height``: 50%``; overflow``: auto``; margin``: auto``; position``: fixed``; top``: 0``; left``: 0``; bottom``: 0``; right``: 0``; z-index``: 999``; }

响应式

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


.Absolute-Center.is-Responsive { width``: 60%``; height``: 60%``; min-width``: 400px``; max-width``: 500px``; padding``: 40px``; overflow``: auto``; margin``: auto``; position``: absolute``; top``: 0``; left``: 0``; bottom``: 0``; right``: 0``; }

偏移

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


.Absolute-Center.is-Right { width``: 50%``; height``: 50%``; margin``: auto``; overflow``: auto``; position``: absolute``; top``: 0``; left``: auto``; bottom``: 0``; right``: 20px``; text-align``: right``; }

溢出

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


.Absolute-Center.is-Overflow { width``: 50%``; height``: 300px``; max-height``: 100%``; margin``: auto``; overflow``: auto``; position``: absolute``; top``: 0``; left``: 0``; bottom``: 0``; right``: 0``; }

调整尺寸

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


.Absolute-Center.is-Resizable { min-width``: 20%``; max-width``: 80%``; min-height``: 20%``; max-height``: 80%``; resize: both``; overflow``: auto``; margin``: auto``; position``: absolute``; top``: 0``; left``: 0``; bottom``: 0``; right``: 0``; }

图像

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


.Absolute-Center.is-Image { width``: 50%``; height``: auto``; margin``: auto``; position``: absolute``; top``: 0``; left``: 0``; bottom``: 0``; right``: 0``; }

可变高度

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


.Absolute-Center.is-Variable { display``: table; width``: 50%``; overflow``: auto``; margin``: auto``; position``: absolute``; top``: 0``; left``: 0``; bottom``: 0``; right``: 0``; }

负 margin

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


.is-Negative { width``: 300px``; height``: 200px``; padding``: 20px``; position``: absolute``; top``: 50%``; left``: 50%``; margin-left``: -170px``; /* (width + padding)/2 */ margin-top``: -120px``; /* (height + padding)/2 */ }

Table-Cell

结构:


<``div class="Pos-Container is-Table"> <``div class="Table-Cell"> <``div class="Center-Block"> `` </``div``> </``div``> </``div``>

样式:


.Pos-Container.is-Table { display``: table; } .is-Table .Table-Cell { display``: table-cell``; vertical-align``: middle``; } .is-Table .Center-Block { width``: 50%``; margin``: 0 auto``; }

FlexBox


.Pos-Container.is-Flexbox { display``: -webkit-box; display``: -moz-box; display``: -ms-flexbox; display``: -webkit-flex; display``: flex; -webkit-box-align: center``; -moz-box-align: center``; -ms-flex-align: center``; -webkit-align-items: center``; align-items: center``; -webkit-box-pack: center``; -moz-box-pack: center``; -ms-flex-pack: center``; -webkit-justify-``content``: center``; justify-``content``: center``; }

这里推荐一下我的前端学习交流群: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. QQ好友列表数据模型封装

    QQ好友中的信息较多.假设我们单独从plist 中直接取出数据 是能够解决这个问题 可是相当复杂.以为列表中分组 .每组中还有不同信息 大致模型是 数组套数组  数组套字典 所以我们要封装数据模型 / ...

  2. LeetCode: Word Ladder [126]

    [题目] Given two words (start and end), and a dictionary, find the length of shortest transformation s ...

  3. 简单list转化为tree结构

    1 @Override public List<Module> getTree() { List<Module> list = moduleMapper.queryList() ...

  4. [SCOI 2010] 股票交易

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1855 [算法] 单调队列优化动态规划 [代码] #include<bits/s ...

  5. php模版静态化技术

    PHP页面的静态化很有必要,尤其是在CMS系统中,一些内容一旦生成,基本上不会有变化,这时如果用html将页面静态化,无疑会减少服务其解析PHP页面的负担.以下是看书学来的PHP静态化技术,记录之以备 ...

  6. Hyper和Vmware冲突,Device/Credential Guard 不兼容

    切换到VM的时候,采用关闭策略 1.PS管理员关闭命令 bcdedit /set hypervisorlaunchtype off 2.系统设置,启用或关闭Windows功能那里,关闭Hyper-V ...

  7. SwiftUI 官方教程(三)

    3. 用 Stacks 组合 View 在上一节创建标题 view 后,我们来添加 text view,它用来显示地标的详细信息,比如公园的名称和所在的州. 在创建 SwiftUI view 时,我们 ...

  8. 爬虫中之Requests 模块的进阶

    requests进阶内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个 ...

  9. A - Antipalindrome

    Problem description A string is a palindrome if it reads the same from the left to the right and fro ...

  10. WPF MVVM 关闭窗体

    由于程序采用MVVM模式同时有些操作需要单独窗口来进行处理.因此就会产生窗口关闭问题, 由于是MVVM和需要操作弹出窗口中操作的内容因此就需要在mvvm进行统一处理. 网上查了几种方法采用其中一种 不 ...