As a front-end siege engineer, when making Web pages, I have encountered CSS horizontal and vertical centering. I think everyone has studied or written about it, especially the vertical centering, which is even more annoying. The following is This article mainly introduces to you the solutions to using CSS to set the vertical centering of elements. With these methods, you will not have to worry about it. Friends who need it can refer to it.
Preface
The vertical centering of elements is also a problem we often encounter in daily web page layout. This article mainly introduces it to you. Regarding the solution to using CSS to set the vertical centering of elements, the article introduces a variety of solutions to various situations. I believe it will be helpful to friends who encounter this problem. I won’t say much below, let’s take a look. See the detailed introduction.
html code:
<p class="parent"> <p class="child">Text here</p> </p>
Since you set the vertical centering of the child element, you need to know the height of the parent element to know Where is the so-called center, right? Just like if you want to stand in the middle of a distance, then you first need to know how long the distance is before you can know where the middle is.
Note, all my The percentage height and width are all based on settings such as html,body {width: 100%;height: 100%;}
. If you do not set it like this, .parent is the parent of p. The element is body, and if you don’t set the width and height of body, you may not see the effect. The aspect ratio of the .parent p is relative to its parent element, so you need to confirm the .parent p when using it. The parent element has set width and height.
(1) Inline text is vertically centered
css code:
.parent { height: 100px; border: 1px solid #ccc; /*设置border是为了方便查看效果*/ } .child { line-height: 100px; }
(2) Vertically center non-text within the line (take img as an example)
html code :
<p class="parent"> <img src="/static/imghwm/default1.png" data-src="image.png" class="lazy" alt="" /> </p>
css code
.parent { height: 100px; border: 1px solid #ccc; /*设置border是为了方便查看效果*/ } .parent img { //注意此时应该保证图片自身的高度或者你设置的高度小于父元素的200px的行高,不然你看不出来居中的效果. line-height: 100px; }
(3) Vertically center block-level elements of unknown height
html code:
<p class="parent"> <p class="child"> <!--.child的高度未知,父元素要有高度--> sddvsds dfvsdvds </p> </p>
The first method ( No need to add padding):
css code:
.parent { width: 100%; height: 100%; position: relative; /*display: table;*/ } .child { width: 500px; border: 1px solid #ccc; /*设置border是为了方便查看效果*/ position: absolute; top: 50%; transform: translateY(-50%); }
Second method (without transform):
css code:
.parent { position: relative; width: 100%; height: 100%; } .child { width: 500px; border: 1px solid #ccc; position: absolute; top: 0; bottom: 0; left: 0; right: 0; height: 30%; margin: auto; }
The third method (requires padding):
css code:
#parent { padding: 5% 0; } #child { padding: 10% 0; }
Fourth method:
(Use display: table , this method is also suitable for centering inline text elements):
css code:
.parent { width: 100%; height: 100%; display: table; } .child { display: table-cell; vertical-align: middle; }
The fifth method ( Flex layout, compatibility needs to be considered here!)
css code:
.parent { width: 100%; height: 100%; /*这里一定要写高度奥!*/ display: flex; align-items: center; justify-content: center; }
(4 ) Block-level elements of known height are vertically centered
html code:
<p class="parent"> <p class="child"> <!--.child的高度已知,父元素高度已知--> sddvsds dfvsdvds </p> </p>
css code:
#parent { height: 300px; } #child { height: 40px; margin-top: 130px; /*这个只为父元素的高度减去这个元素的高度除以二计算得到的*/ border: 1px solid #ccc; }
The above are some of the feasible methods that I have discovered and personally tested. There should be other methods
The above is the detailed content of Summary: Use css to set the element to vertical center. For more information, please follow other related articles on the PHP Chinese website!

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
