设为首页 加入收藏

TOP

css元素水平垂直居中
2019-09-17 15:21:23 】 浏览:11
Tags:css 元素 水平 垂直 居中

温习一下元素水平垂直居中的几种方法

元素有具体宽度

1、absolute+负边距

.LV_center{
  border: 1px solid red;
  position: absolute;
  width: 100px;
  height: 100px;
  top:50%;
  left: 50%;
  margin-top:-原高度/2 ;
  margin-left: -原高度/2
}

2、absolute+calc

.LV_center{
  border: 1px solid red;
  position: absolute;
  width: 100px;
  height: 100px;
  top:calc(50% - 原高度/2);
  left:calc(50% - 原高度/2);
}

 3、absolute+margin auto

.LV_center{
  border: 1px solid red;
  position: absolute;
  width: 100px;
  height: 100px;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  margin: auto;
}

元素宽度不定

1、absolute+transform,css3特性2d平移

.LV_center{
  border: 1px solid red;
  position:absolute;
  top:50%;
  left:50%;
  transform:translate(-50%,-50%);
}

2、line-height,外层设置行高,内层继承行高

.box{
  line-height: 300px;
}
.LV_center{
  border: 1px solid red;
  display: inline-block;
  line-height: inherit;
  vertical-align: middle;
}

3、table-cell,模拟td特性,让元素像表格里的td

.box{
  border: 1px solid red;
  width: 300px;
  height: 300px;
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}
.LV_center{
  border: 1px solid red;
  display: inline-block;
}

4、flex弹性盒子

.LV_center{
border: 1px solid red;
  display: flex;
  justify-content: center;  //主轴对齐方式
  align-items: center;  //位于主轴中心
}

推荐一位讲得更详细的 : https://www.jianshu.com/p/1b3337214941

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇element-ui Upload 上传组件源码.. 下一篇毕业生想学习web前端,应该怎么学..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目