css 几种水平垂直居中的方法 及 弹性盒子

13 篇文章 0 订阅
  1. margin :auto 方法
    ==================

    给 要居中的元素 设置 margin :auto
    绝对定位 position :absolute;
    top:0;bottom:0;left: 0px;right: 0px;

    父元素要设置相对定位

  2. 负 margin 的方法


绝对定位 position :absolute;
top:50%; left: 50%px;
margin-top:-高度的一般;
margin-left:负宽度的一半

  1. display :table-cell 法

这个方法主要对父元素设置的比较多
父元素:  display :table-cell;
	         vertical-align:middle;
	         text-align:center;
要居中的元素:   
			vertical-align:middle;

4.弹性盒子法 display :flex;

(1)这个对父元素设置css就可以了
display :flex;
display:-webkit-flex;
align-items:center;
-webkit-align-items:center;
justify-content:center ;
(2)父元素设置display :flex;子元素设置margin:auto
父元素 {
display: -webkit-flex;
display: flex;
width: 400px;
height: 250px;
background-color: lightgrey;
}

要居中的元素 {
background-color: cornflowerblue;
width: 75px;
height: 75px;
margin: auto;
}

弹性盒子

弹性盒子设置
display: -webkit-flex;
display: flex;

如果父元素设置了display 属性的值为 flex ,如果子元素未设置宽度,那么所有子元素的宽度都是一样的宽度

1.给body 设置 direction: rtl;
direction 属性为 rtl (right-to-left),弹性子元素的排列方式也会改变,页面布局也跟着改变

2.flex-direction 顺序指定了弹性子元素在父容器中的位置。
flex-direction: row | row-reverse | column | column-reverse
flex-direction的值有:
row:横向从左到右排列(左对齐),默认的排列方式。
row-reverse:反转横向排列(右对齐,从后往前排,最后一项排在最前面。
column:纵向排列。
column-reverse:反转纵向排列,从后往前排,最后一项排在最上面。
#flex

flex布局

一、flex和align-items

#box {
width: 300px;
height: 300px;
background: #ddd;
display: flex;
align-items: center;
}

二、flex、flex-direction: column和justify-content: center;

#box {
width: 300px;
height: 300px;
background: #ddd;
display: flex;
flex-direction: column;
justify-content: center;
}
#child {
width: 300px;
height: 100px;
background: #08BC67;
line-height: 100px;
}

flex-direction: 主轴的排列方向(默认横轴 flex-direction: row)

justify-content: 主轴的对齐方式 (默认横轴对齐方式–水平)

align-items: 交叉轴的对齐方式(默认纵轴—垂直)

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值