css垂直居中

css子元素在父元素居中显示

徘徊边缘 提交于 2019-11-27 08:02:37
今天在做开发任务的时候遇到了一个最基本的子集div在夫级div中水平垂直居中的问题,就总结了下我们经常用到的方法(此外由于自己能力问题,有误望指出批评) 一:通过定位来实现 1.如果子元素的大小是已知的,可以通过对子元素定位,并且margin值分别减去自己宽高的一半(上码) <style> .father { width: 200px; height: 200px; border: 1px solid red; position: relative; } .child { width: 100px; height: 100px; background-color: #00BCBC; position: absolute; top: 50%; left: 50%; margin-top: -50px; margin-left: -50px; } </style> <body> <div class="father"> <div class="child"></div> </div> </body> 效果图: 2.我们子元素的大小不明确的情况下,有两种实现方式 (1)用margin为auto实现 <style> .father { width: 200px; height: 200px; border: 1px solid red; position: relative; }

css子元素在父元素居中显示

扶醉桌前 提交于 2019-11-27 05:17:35
今天在做开发任务的时候遇到了一个最基本的子集div在夫级div中水平垂直居中的问题,就总结了下我们经常用到的方法(此外由于自己能力问题,有误望指出批评) 一:通过定位来实现 1.如果子元素的大小是已知的,可以通过对子元素定位,并且margin值分别减去自己宽高的一半(上码) <style> .father { width: 200px; height: 200px; border: 1px solid red; position: relative; } .child { width: 100px; height: 100px; background-color: #00BCBC; position: absolute; top: 50%; left: 50%; margin-top: -50px; margin-left: -50px; } </style> <body> <div class="father"> <div class="child"></div> </div> </body> 效果图: 2.我们子元素的大小不明确的情况下,有两种实现方式 (1)用margin为auto实现 <style> .father { width: 200px; height: 200px; border: 1px solid red; position: relative; }

前端学习 之 CSS(三)

∥☆過路亽.° 提交于 2019-11-27 02:49:12
九 :浮动 浮动是 css 里面布局最多的一个属性,也是很重要的一个属性。 float :表示浮动的意思。 属性值: none: 表示不浮动,默认 left: 表示左浮动 right :表示右浮动 例: html 内容: <div class="box1">第一个div</div> <div class="box2">第二个div</div> <span>一个span</span> View Code css 内容: *左浮动*/ .box1 { width: 300px; height: 300px; background-color: red; float: left; } /*右浮动*/ .box2 { width: 400px; height: 400px; background-color: green; float: right; } /*左浮动*/ span { float: left; width: 100px; height: 200px; background-color: yellow; } View Code 效果图: 出现的效果图,三个元素并排显示, .box1 和 span 因为是左浮动,紧挨在一起,这种现象贴边。 .box2 盒子因为右浮动,所以紧靠着右边。 浮动四大特性的学习是必不可少的: 1. 浮动的元素脱标 2. 浮动的元素互相贴靠 3.

CSS垂直居中的8种方法

不想你离开。 提交于 2019-11-27 02:13:39
CSS垂直居中的8种方法 1、 通过verticle-align:middle实现CSS垂直居中。 通过vertical-align:middle实现CSS垂直居中是最常使用的方法,但是有一点需要格外注意,vertical生效的前提是元素的display:inline-block。 2、 通过display:flex实现CSS垂直居中。 随着越来越多浏览器兼容CSS中的flexbox特性,所以现在通过“display:flex”实现CSS水平居中的方案也越来越受青睐。 通过display:flex实现CSS垂直居中的方法是给父元素display:flex;而子元素align-self:center; 这个跟CSS水平居中的原理是一样的,只是在flex-direction上有所差别,一个是row(默认值),另外一个是column。 3、 通过伪元素:before实现CSS垂直居中。 具体方式是为父元素添加伪元素:before,使得子元素实现垂直居中。 4、 通过display:table-cell实现CSS垂直居中。 给父元素display:table,子元素display:table-cell的方式实现CSS垂直居中。 5、 通过隐藏节点实现CSS垂直居中。 创建一个隐藏节点#hide,使得隐藏节点的height值为剩余高度的一半即可。 这种方法也适用于CSS水平居中,原理一样。

CSS3/CSS之居中解析(水平+垂直居中、水平居中,垂直居中)

旧巷老猫 提交于 2019-11-27 00:52:04
首先,我们来看下垂直居中: (1)、如果是单行文本,则可以设置的line-height的数值,让其等于父级元素的高度! <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .box{ background: green; height:200px; } a {height:100%; line-height: 200px; color:red; } </style> </head> <body> <div class="box"> <a href="">ggg </a> </div> </body> </html> (2)、如果元素是行内块级元素,一般会使用diaplay:inline-block,vertical-align:middle,还有一个伪元素让元素内容处于容器中央!给父元素添加伪元素! <!DOCTYPE html> <html lang="en"> <head> <meta

CSS垂直居中的方法

依然范特西╮ 提交于 2019-11-27 00:39:01
方法1:display:flex+align-items 只需要给父元素设置属性 1 <style> 2 .box { 3 position: absolute; 4 width: 100px; 5 height: 100px; 6 border: 1px solid red; 7 8 display: flex; 9 justify-content:center; 10 align-items:center; 11 } 12 </style> 13 <div class="box"> 14 <span>center</span> 15 </div> 方法2:绝对定位和负边距 top、margin-top的区别: 1、top、bottom、left、right是绝对定位,必须设置position为absolute。 margin一系列设置是相对定位。注意:如果用top等,而position未设置为absolute,那设置是不起作用的。 2、top这些在绝对定位的前提下,这个绝对定位,是相对body 或者 position:relative的父级元素的绝对定位。margin的相对定位,是指相对相邻元素的定位。 1 .box{ 2 position: relative; 3 width: 200px; 4 height: 200px; 5 border: 1px solid red;

css水平居中的几种方式

对着背影说爱祢 提交于 2019-11-26 13:05:57
1. 子元素为行内元素时,父元素使用 text-align: center; 实现子元素的水平居中; 2. 子元素为块级元素时,    2.1. 将子元素设置 margin-left:auto; margin-right:auto; 实现居中;    2.2. 将子元素设置 display: inline-block,然后使用text-align:center实现居中;    2.3. 根据父元素采用绝对定位,右移父元素宽度的一半,再左移自身宽度的一半; 3. 父元素使用flex布局,    3.1. 子元素无论是行内还是块级,采用 align-self: center; 实现水平居中;    3.2. 子元素无论是行内还是块级,采用 margin: 0 auto; 实现水平居中; 实例代码如下: <style> * {   margin: 0; }   .outer-box1{     width: 200px;     height: 200px;     border: 5px solid black;     /* 对父元素设置此样式,则其内的行内元素实现居中 */     text-align: center;     position: relative;   }   .box1{     width: 50px;     height: 50px;    

垂直水平居中的几种方法

耗尽温柔 提交于 2019-11-26 12:12:06
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title></title> 6 7 <style type="text/css"> 8 9 *{ 10 margin: 0; 11 padding: 0; 12 } 13 14 .box{ 15 width: 400px; 16 height: 400px; 17 background: skyblue; 18 } 19 .cont{ 20 width: 200px; 21 height: 200px; 22 background: pink; 23 } 24 25 26 /*方法一:IE8+*/ 27 .box{ 28 display: table-cell; 29 text-align: center; 30 vertical-align: middle; 31 } 32 .cont{ 33 display: inline-block; 34 vertical-align: middle; 35 } 36 37 /*方法二:IE8+*/ 38 /* .box{ 39 position: relative; 40 } 41 .cont{ 42 position: absolute; 43 top: 0; 44 left: 0; 45 bottom: 0