垂直居中

网页css自适应高度下垂直居中文字

匿名 (未验证) 提交于 2019-12-02 16:28:20
1、关于css自适应高度下垂直居中文字,非定位类的方法实现 : 分析:使用vertical-align方法可以使元素垂直居中,但是只是针对支持vertical的元素,如table 、td 等,div和span是不支持的,所以要模拟table元素. (position会影响性能,能优则优) 2、实现方法 父元素设置display : table; 子元素设置display: table-cell 并设置 vertical-align: middle -END- 文章来源: 网页css自适应高度下垂直居中文字

元素的水平居中以及垂直居中

冷暖自知 提交于 2019-12-02 16:28:05
元素的水平居中 1)行内元素的水平居中:text-align 2)块级元素的水平居中:margin:0 auto 元素的垂直居中 1)行内元素:line-height设置为与父级元素的高度一样大 2)块级元素:可以给父级使用相对定位,子级使用绝对定位并margin:auto;,将top、left、right、bottom为0 3)给父级和子级都加绝对定位,再给子级添加top:calc(50% - 子级元素高度一半) left:calc(50% - 子级元素宽度一半) 4)给父级相对定位,子级绝对定位:left:50%;top:50%; margin-left:-子级元素宽度一半;margin-top:-子级元素高度一半 5)给父级一个display:flex; align-items:center;再给子级添加:margin:0 auto; 来源: https://www.cnblogs.com/szxEPoch/p/11754493.html

小div在大div中垂直居中方式

别说谁变了你拦得住时间么 提交于 2019-12-02 16:21:06
  代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>div居中</title> <style> * { margin:0; padding:0; } .content { width:400px; height:400px; background:orange; border: 1px solid green; position:relative; margin: 100px auto; } .nav { width:80px; height:80px; line-height:80px; text-align:center; background:green; margin: auto; position:absolute; left: 0; top: 0; bottom:0; right: 0; } </style> </head> <body> <div class="content"> <div class="nav">这是内容</div> </div> </body> </html>   效果: 来源: https://www.cnblogs.com/songtianfa/p/11752422.html

使层垂直居中的display:table-cell方法,多浏览器兼容

本秂侑毒 提交于 2019-12-02 16:19:07
ie7和ie6都不能识别 display: table-cell;(但是支持display: block等css1的属性!) ie8以及以后版本可以识别! ie7和ie6能识别vertical-align:middle; 如果盒模型中间只有几段文字的话,设置line-height就可以实现居中显示了! 如果要实现盒模型中的div居中,可以参考以下代码: div#wrap { display: table; border: 1px solid #FF0099; background-color: #FFCCFF; width: 760px; height: 400px; *position: relative; overflow: hidden; } div#subwrap { vertical-align: middle; display: table-cell; *position: absolute; *top: 50%; } div#content { *position: relative; *top: -50%; } 效果: 这个是多浏览器兼容的,但是得写几层div~ 但是想在层或img在层中垂直水平居中该怎样做呢?下一篇会涉及到。。。。 来源: CSDN 作者: sunny327 链接: https://blog.csdn.net/sunny327/article

html--垂直居中

江枫思渺然 提交于 2019-12-02 15:50:50
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> *{ margin: 0; padding: 0;} #test{ width: 200px; height: 200px; /*内联元素水平居中*/ line-height: 200px; margin: 0 auto; text-align:center; background: pink; } </style> </head> <body> <div id="test"> test </div> </body> </html> 2 已经知道块元素的高宽垂直方案 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> *{ margin: 0; padding: 0;} /*已知宽高的水平垂直居中方案*/ #wrap{ position: relative; width: 150px; height: 150px; background: pink; margin: 0 auto; } #inner{ position: absolute; left: 50%

垂直居中布局

£可爱£侵袭症+ 提交于 2019-12-02 12:32:45
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 8 <style> 9 *{ 10 margin: 0; 11 padding: 0; 12 } 13 .main{ 14 width:300px; 15 height:400px; 16 position: relative; 17 top:0; 18 left:0; 19 background-color: antiquewhite; 20 } 21 .center{ 22 position: absolute; 23 width:100px; 24 height:100px; 25 top:50%; 26 left:50%; 27 background-color: red; 28 transform: translate(-50%,-50%); 29 } 30 31 </style> 32 <body> 33 34 35 <div class="main"> 36 <div class="center"> 37 </div> 38 </div> 39 </body> 40 </html> 效果: 第二种:flex布局垂直居中 1 <!DOCTYPE html> 2

CSS flex样式垂直居中

拈花ヽ惹草 提交于 2019-12-02 11:48:43
文章目录 文章参考 问题描述 flex 个人理解 对子元素影响 作用自身的样式 作用于子控件 案例(水平垂直居中) 方法一(改变方向) 方法二(让flex的子元素水平垂直居中) 文章参考 Flex 布局教程:语法篇 问题描述 由于div默认是没有高度的,如果设置了高度,默认是从左到右,从上到下的顺序来排布; 如果要做垂直居中,就需要计算div控件的高度,如果内容变多或者变少,又会导致定位不准确,因此,最稳妥的办法就是让浏览器自己去根据div的高度居中显示 flex 个人理解 对子元素影响 设为 Flex 布局以后,子元素的float、clear和vertical-align属性将失效。 作用自身的样式 flex-direction 属性决定主轴的方向(即项目的排列方向) flex-wrap属性定义,如果一条轴线排不下,如何换行。 flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。 justify-content属性定义了项目在主轴上的对齐方式。 align-items 属性定义项目在交叉轴上如何对齐。 align-content 属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。 作用于子控件 order 属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。 flex-grow

CSS实现水平垂直居中的几种方式

拥有回忆 提交于 2019-12-02 10:58:39
准备工作,假设有如下html <div class="wrapper"> <div class="box"> </div> </div> 需要设置宽高 1.position + 负margin .wrapper { position : relative ; } .box { width : 100px ; height : 100px ; position : absolute ; left : 50% ; top : 50% ; margin-left : -50px ; margin-top : -50px ; } 2. position + margin:auto .wrapper { position : relative ; } .box { width : 100px ; height : 100px ; position : absolute ; left : 0 top : 0 ; right : 0 ; bottom : 0 ; margin : auto ; } 3.position + calc() .wrapper { position : relative ; } .box { width : 100px ; height : 100px ; position : absolute ; left : calc ( 50% - 50px ) ; top :

css实现水平、垂直居中的几种方法

谁说我不能喝 提交于 2019-12-02 09:50:37
用text-align: center来实现水平居中(只能实现文本的水平居中) //html <div class="center"> 文本水平居中 </div> //css .center{width: 200px;height: 100px;background-color: #a036b4; text-align: center; /*水平居中*/ } 2.用margin:auto实现块级元素的水平居中 <div class="center"> <div class="child_div"></div> </div> .center{width: 200px;height: 100px;background-color: #a036b4; } .child_div{width: 50px;height: 50px;background-color: blue;margin: 0 auto;/*水平居中*/} 3.使用浮动float来实现水平居中,用float:left;left:50%;同时margin-left:自身宽度的一半; <div class="center"> <div class="child_div"></div> </div> .center{width: 200px;height: 100px;background-color: #a036b4

CSS实现垂直居中的常用方法

时光怂恿深爱的人放手 提交于 2019-12-02 09:50:24
在前端开发过程中,盒子居中是常常用到的。其中 ,居中又可以分为水平居中和垂直居中。水平居中是比较容易的,直接设置元素的margin: 0 auto就可以实现。但是垂直居中相对来说是比较复杂一些的。下面我们一起来讨论一下实现垂直居中的方法。 首先,定义一个需要垂直居中的div元素,他的宽度和高度均为300px,背景色为橙色。代码如下: <! DOCTYPE html > < html lang = "en" > < head > < meta charset = "UTF-8" > < title > index </ title > <style> .content { width : 300px ; height : 300px ; background : orange ; } </style> </ head > < body > < div class = "content" ></ div > </ body > </ html > 效果如下: 我们需要使得这个橙色的div居中,到底该怎么办呢?首先我们实现水平居中,上面已经提到过了,可以通过设置margin: 0 auto实现水平居中,代码如下: <! DOCTYPE html > < html lang = "en" > < head > < meta charset = "UTF-8" > < title >