css居中 垂直居中

Front-end developing is beautiful, and it's getting prettier by the day. Nowadays we got so many concepts, methodologies, good practices and whatnot to make our work stand out from the rest. Javascript (along with its countless third party libraries) and CSS have grown so big, helping us create things that even a couple of years ago were deemed impossible. But there's one thing that's often left in the dark, I'm sure you've stumbled with it at least once, and we know its fix is not always very good looking: vertically centering elements.

前端开发很漂亮,而且一天比一天漂亮。 如今,我们有太多的概念,方法论,良好实践和其他让我们的工作脱颖而出的东西。 Javascript(以及无数的第三方库)和CSS变得如此庞大,帮助我们创建了甚至几年前都被认为不可能的事情。 但是有一件事经常在黑暗中遗留下来,我敢肯定您至少偶然发现过它一次,而且我们知道它的修复效果并不总是很好:垂直居中的元素。

View Demo观看演示

Now, there are many ways of addressing this issue: the table method, the height and half height method, the inserting-another-element-slash-pseudo-element-with-vertical-align-and-height-100% method, the calculate on load method, and more. Each of these seems dirtier than the last, it gets even more complicated when the element's height is unknown (be it a parent's or children's), and don't get me started on how bad it looks like when working with responsive layouts.

现在,有很多方法可以解决此问题:表格方法,高度和半高方法,使用垂直对齐和高度100%插入另一个元素斜杠假元素元素,计算负载法,等等。 这些元素似乎都比上一个更脏,当元素的高度未知时(无论是父元素还是孩子元素),它会变得更加复杂,并且不要让我开始了解使用响应式布局时它的外观如何糟糕。

For a good time I went with the "calculate on load" method. The premise was simple: get the parent's height, subtract the children's height, divide by 2, and inline the top property with the result as a px value. Essentially, 50% of the parent's height - 50% of the children's height, done. Here's a visual representation of it:

好一会儿我选择了“计算负载”方法。 前提很简单:获取父母的身高,减去孩子的身高,除以2,然后将top属性内联为px值。 本质上,完成了父母身高的50%-孩子身高的50%。 这是它的视觉表示:

Figure 1 - The red "210px" is the value we need to set on the "top" property.

图1-红色的“ 210px”是我们需要在“ top”属性上设置的值。

And I know what you are thinking, "Inlining CSS is bad!!!", but this was the least ugly solution. It didn't require to change any element's box model, waste its pseudo-elements, insert another element just for the sake of a fix, nor any other hackery. But as I said before, things got trickier when the element's dimensions were unknown.

而且我知道您在想什么,“内联CSS不好!”,但这是最不丑的解决方案。 它不需要更改任何元素的盒子模型,浪费其伪元素,仅出于修复目的而插入另一个元素,也不需要任何其他黑客手段。 但是正如我之前所说,当元素的尺寸未知时,事情变得更加棘手。

Sure, you could get over this by using a script that runs after the window has loaded, or on window/object resize, and then recalculate the positions. But what if you've got hundreds of elements, each getting resized to fit their parents frames? The script will take a huge performance hit. And more importantly, why complicate things so much when there's a simple, pure CSS solution?

当然,您可以通过使用脚本来克服此问题,该脚本在窗口加载后运行,或者在调整窗口/对象大小时运行,然后重新计算位置。 但是,如果您有数百个元素,每个元素的大小都经过调整以适合其父框架,该怎么办? 该脚本将极大地影响性能。 更重要的是,当有一个简单的纯CSS解决方案时,为什么要使事情变得如此复杂呢?

范例HTML (Sample HTML)

Our example will be base on the following HTML:

我们的示例将基于以下HTML:


<div class="parent">
<div class="children">I'm vertically centered!</div>
</div>

This is a frequent case: a parent with children we desire to be vertically centered.

这是一个常见的情况:一个有孩子的父母希望我们垂直居中。

输入CSS3转换 (Enter CSS3 Transforms)

One interesting thing about CSS transforms is that, when applying them with percentage values, they base that value on the dimensions of the element which they are being implemented on, as opposed to properties like top, right, bottom, left, margin, and padding, which only use the parent's dimensions (or in case of absolute positioning, which uses its closest relative parent).

关于CSS变换的一件有趣的事情是,当将它们与百分比值一起应用时,它们基于实现它们的元素的尺寸来确定该值,而不是像top,right,bottom,left,margin和padding这样的属性,仅使用父级的尺寸(或在绝对定位的情况下,使用其最接近的相对父级)。

Knowing this, we could apply the same "calculate on load" method's small equation, but now adapting it to just CSS. First, we move the element in question down to the middle of it's parent using top: 50%, then we pull it back up by half of said element, with transform: translateY(-50%). Of course, the element must be relative, absolute, or fixed. If we look back to Figure 1, this is exactly what we have achieved.

知道了这一点,我们可以应用相同的“按负载计算”方法的小方程式,但现在将其仅用于CSS。 首先,我们使用top:50%将有问题的元素向下移动到其父元素的中间,然后使用transform:translateY(-50%)将其拉回所述元素的一半。 当然,元素必须是相对的,绝对的或固定的。 如果回头看图1,这正是我们所实现的。


.children{
background: #ffdb4c;
height: 300px;
position: relative;
top: 50%;
transform:          translateY(-50%);
}

As we are using percentages, these elements will be vertically centered no matter how tall their parents are, or what unknown and random changes they experiment while the user interacts with them.

当我们使用百分比时,这些元素将垂直居中,无论其父母的身高多高,还是在用户与他们互动时他们尝试进行哪些未知且随机的变化。

View Demo观看演示

Finally, a few warnings: If you want to centralice a relative positioned element, its parent must have a height value, i.e., it won't work if said parent's height is set to auto. And of course, as most CSS3 features and properties, transforms don't work in IE 8 and earlier versions.

最后,一些警告:如果要集中放置相对位置的元素,则其父元素必须具有height值,即,如果将所述父元素的高度设置为auto,则它将不起作用。 当然,由于大多数CSS3功能和特性,转换在IE 8和更早版本中均不起作用。

翻译自: https://davidwalsh.name/css-vertical-center

css居中 垂直居中


http://www.taodudu.cc/news/show-3391475.html

相关文章:

  • Linux 文件系统写-ext2流程
  • 内存微粒_减少室内微粒空气污染的策略,第2部分中的第1部分
  • git 修复中间版本_如何修复git中的错误并且不留痕迹
  • balance_dirty_pages_ratelimited分析
  • 清洁保养水槽洗碗机_如何在洗碗机中清洁肮脏的键盘(不破坏它)
  • java 合并和拆分单元格_如何轻松合并和拆分电子书
  • 陕西省2019年初中计算机试题,陕西省2019年英语中考试题及答案解析(Word版).pdf
  • 九上仁爱英语计算机作文,仁爱英语九上英语作文.doc
  • 2015 c语言高考题,高考英语真题全国卷
  • MIT6.830-lab5-B+ Tree Index(数据库的索引B+树的搜索、插入、删除窃取、删除合并)
  • vasp phonopy消除虚频个人经验总结
  • Phonopy源码剖析读取Vasp等软件的输出文件
  • 云服务器Ubuntu安装Phonopy教程
  • Phonopy 内网 安装 天河二号
  • 非root用户安装Anaconda3 + setuptools + phonopy模块
  • python61到08使用说明书_phonopy中文使用说明
  • bash+vasp+ShengBTE自动计算材料热运输性质脚本
  • ScSb的热电性质——热输运性质计算(力常数)
  • phonopy 在window下的安装
  • linux ubuntu 下 phonopy 的安装步骤
  • Quantum Espresso + Phonopy 计算声子过程
  • Linux界面下运行vaspkit,linux下超详细教程安装phonopy - 第一原理 - 小木虫 - 学术 科研 互动社区...
  • 自动安装包括ase、phonopy、pymatgen和q-robot工具的anaconda(python)程序
  • 基于VASP+phonopy+shengbte计算声子相干的热学性质
  • vasp phonopy 计算声子(有限位移法)
  • linux medea 软件安装,linux下超详细教程安装phonopy
  • vasp-phonopy编译
  • VASP+phonopy计算声子群速与数据导出
  • vasp+phonopy计算声子谱及后处理
  • linux操作系统安装phonopy

css居中 垂直居中_CSS垂直居中相关推荐

  1. css 垂直居中_CSS垂直居中的另类实现

    前言 众所周知,"css如何实现元素垂直居中?"已经是一个老生常谈的问题了,这个问题目前已经有了许多解决方案,这些方案也都有各自适用的场景以及优缺点,大致如下: flex布局 gr ...

  2. CSS居中——水平居中、垂直居中方法

    水平居中 1.行内或类行内元素(如文本.链接) 在块级父元素中用CSS样式实现行内元素水平居中,只需要设置:text-align: center; 这种方法可以让 inline / inline-bl ...

  3. text文字垂直居中_CSS垂直居中,你会多少种写法?

    来源 | https://wintc.top/article/4 CSS控制居中是前端开发中非常常用的布局技能,本文列出几种CSS控制元素居中的几种方法.谈及HTML元素居中展示,涉及到水平居中和垂直 ...

  4. 高度不定垂直居中_CSS垂直居中的七个方法

    我们在编辑一个版面,通常都会用到水平居中和垂直居中来设计,而水平居中很好处理,不外乎就是设定margin:0 auto:或是text-align:center:,就可以轻松解决掉水平居中的问题,但一直 ...

  5. html盒子嵌套居中,css在盒子中垂直居中和固定居中

    顶部固定居中 我是固定的 .w960{ width: 960px; margin:0 auto; } .fixed{ position: absolute; top:0; left: 0; right ...

  6. h5文字垂直居中_CSS居中的常用方式以及优缺点

    前言 居中是页面开发中经常遇到的问题. 使用合适的.简单的.兼容性好的居中方式是我们页面仔在整个工作生涯中都要面对的问题. text-align:center 来看这个例子,一张图片和文字进行居中.如 ...

  7. css垂直居中怎么设置?文字上下居中和图片垂直居中

    css 居中分css垂直居中和css水平居中,水平居中平时比较常用,这里我们主要讲css上下居中的问题.垂直居中又分为css文字上下居中和css图片垂直居中,下面我们就分别来介绍一下. css文字上下 ...

  8. css如何设置文本垂直居中显示,css中怎么设置文本居中?css文本垂直居中的设置方法...

    在网页设计的过程中,有时候可能为了布局美观可能需要让文本居中,那么,怎么设置文本居中呢?本篇文章将给大家介绍关于css设置文本垂直居中的方法. 首先我们要知道通过css实现元素的水平居中较为简单:对文 ...

  9. input文字垂直居中_CSS的带文字居中分析

    CSS居中的方法有特别多,这里只介绍最基本的方法. CSS中分块级元素和内联元素,但是块级元素或者内联元素都可以设置display,最常用的三种display--inline.block.inline ...

最新文章

  1. c/c++/MFC 调用 js 函数代码
  2. java 获取字符串长度_ava练习实例:java字符串长度与Java String charAt() 方法 (建议收藏)...
  3. windows.h有哪些函数
  4. java char 空字符串_java判断char是否为空的方法
  5. webdriver---API---(java版) 高级应用
  6. 程序人生:女程序员的求职奋斗史
  7. 溢价28倍!罗永浩的直播公司要卖了:“真还传”提前上演!
  8. ACR2010_常规医疗环境下TNF拮抗剂对RA骨侵蚀的修复作用
  9. linux 文件大小 自动变化 写,Linux下自动清理超过指定大小文件的方法
  10. iOS最好用的弹出框
  11. EDA技术实用教程VHDL篇--Quartus II 13.1实用教程--工程建立
  12. 共建信创生态,助力组织国产化进程
  13. 大疆RoboMaster技术总监:我是如何成为一名机器人工程师的
  14. C++游戏编程教程(五)——项目实战
  15. Ubuntu 14.04安装 skype
  16. sklearn.metrics.multilabel_confusion_matrix
  17. Fragment和Activity之间的通信
  18. 腾讯互娱web后端面经分享
  19. 项目源代码迁移到另一个gitlab的方法(保留原来的提交记录)
  20. 浅析阿里云API网关的产品架构和常见应用场景

热门文章

  1. 世界那么大,我想去看看。Django仿制微信朋友圈九宫格相册(1)
  2. 【CentOS】Not a valid JAR: /usr/local/src/hadoop
  3. Linux运维工程师学习:Linux系统基本知识
  4. LGD扩产OLED面板可能错过了最佳时机
  5. BZOJ 3597 [Scoi2014]方伯伯运椰子
  6. nmap和nc扫描工具
  7. Oracle gsd服务是什么,如何处理11gR2 RAC下oc4j和gsd服务为OFFLINE状态 | 旺旺知识库
  8. 《华尔街日报》:阿里最大威胁来自腾讯
  9. 急求TWindowsMediaPlayer循环播放的问题的问题!
  10. 隐藏添加删除程序中的程序在控制面板中的“添加/删除程序”项