常用CSS布局(备以查用)

1. 水平居中6种

.parent {
    text-align: center;
}
.child {
    display: inline-block;
}
.child {
    margin: 0 auto;
    width: 100px;
}
.child {
    width: 100px;
    position: relative;
    left: 50%;
    margin-left: -50px;
}
.parent {
    position: relative;
}
.child {
    position: absolute;
    /* 水平拉满屏幕 */
    left: 0;
    right: 0;
    width: 300px;
    /* 拉满屏幕之后设置宽度,最后通过 margin 实现水平居中 */
    margin: auto;
}
.parent {
    position: relative;
}
.child {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}
.parent {
    display: flex;
    /* 方法一 */
    justify-content: center;
}
.child {
    /* 方法二 */
    margin: auto;
}
.parent {
    display: grid;
    /* 方法一 */
    justify-items: center;
    /* 方法二 */
    justify-content: center;
}
.child {
    /* 方法三 */
    margin: auto;
}

2. 垂直居中6种

.parent {
    height: 500px;
    /* 为父级容器设置行高 */
    line-height: 500px;
}
.child {
    /* 将子级元素设置为 inline-block 元素 */
    display: inline-block;
    /* 通过 vertical-align: middle; 实现居中 */
    vertical-align: middle;
}
.parent {
    position: relative;
}
.child {
	height: 100px;
	position: absolute;
	top: 50%;
	margin-top: -50px;
}
.parent {
    position: relative;
}
.child {
    position: absolute;
    /* 水平拉满屏幕 */
    top: 0;
    bottom: 0;
    width: 300px;
    /* 拉满屏幕之后设置宽度,最后通过 margin 实现水平居中 */
    margin: auto;
}
.parent {
    position: relative;
}
.child {
    position: absolute;
    left: 50%;
    transform: translateY(-50%);
}
.parent {
    display: flex;
    /* 方法一 */
    align-items: center;
}
.child {
    /* 方法二 */
    margin: auto;
}
.parent {
    display: grid;
    /* 方法一 */
    align-items: center;
    /* 方法二 */
    align-content: center;
}
.child {
    /* 方法三 */
    margin: auto;
    /* 方法四 */
    align-self: center;
}

3. 垂直水平居中7种(垂直水平居中方法一起用)

.parent {
	height: 300px;
	line-height: 300px;
	text-align: center;
}
.child {
	display: inline-block;
	vertical-aligh: middle;
}
.parent {
	position: relative;
}
.child {
	width: 100px;
	height: 100px;
	position: absolute;
	left: calc(50% - 50px);
	top: calc(50% - 50px);
}
.parent {
	position: relative;
}
.child {
	width: 100px;
	height: 100px;
	position: absolute;
	left: 50%;
	top: 50%;
	margin-left: -50px;
	margin-top: -50px;
}
.parent {
	position: relative;
}
.child {
	position: absolute;
	top: 0;
	bottom: 0;
	left: 0;
	right: 0;
	margin: auto;
}
.parent {
	position: relative;
}
.child {
	postion: absolute;
	left: 50%;
	top: 50%;
	transform: translate(-50%, -50%);
}
.parent {
	display: flex;
	/* 方法一 */
	align-items: center;
	justify-content: center;
}
.child {
	/* 方法二 */
	margin: auto;
}
.parent {
	display: grid;
	/* 方法一 */
	/* align-items: center;
	justify-items: center; */
	/* items缩写 */
	place-items: center;
	
	/* 方法二 */
	/* align-content: center;
	justify-content: center; */
	/* content缩写 */
	place-content: center;
}
.child {
	/* 方法三 */
	margin: auto;
	/* 方法四 */
	/* align-self: center;
	justify-self: center; */
	/* self缩写 */
	place-self: center;
}

4. 两列布局

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值