CSS布局——一个DIV上下左右水平居中垂直居中布局

一、位于父类左上角

图片演示

html代码

    <div class="div-big">
		<div class="div-small">1</div>
	</div>

css代码

/* 父类 */
.div-big{
	width: 600px;
	height: 400px;
	background-color: lightgreen;/*绿背景色*/
}
/* 子类 */
.div-big .div-small{
	width: 100px;
	height: 100px;
	background-color: lightcoral;/*红背景色*/
}

一、位于父类上部水平居中

图片演示

html代码

    <div class="div-big">
		<div class="div-small">1</div>
	</div>

css代码

/* 父类 */
.div-big{
	width: 600px;
	height: 400px;
	background-color: lightgreen;/*绿背景色*/
}
/* 子类 */
.div-big .div-small{
	width: 100px;
	height: 100px;
	background-color: lightcoral;/*红背景色*/
	margin: 0 auto;/*重要*/ /*水平居中*/
}

一、位于父类右上角

图片演示

html代码

	<div class="div-big">
		<div class="div-small">1</div>
	</div>

css代码

/* 父类 */
.div-big{
	width: 600px;
	height: 400px;
	background-color: lightgreen;/*绿背景色*/
	display: flex;/*重要*/ /*横向排列*/
	justify-content:flex-end;/*重要*/ /*在结尾处*/
}
/* 子类 */
.div-big .div-small{
	width: 100px;
	height: 100px;
	background-color: lightcoral;/*红背景色*/
}

一、位于父类左侧垂直居中

图片演示

html代码

	<div class="div-big">
		<div class="div-small">1</div>
	</div>

css代码

/* 父类 */
.div-big{
	width: 600px;
	height: 400px;
	background-color: lightgreen;/*绿背景色*/
	display: flex;/*重要*/ /*横向排列*/
	align-items: center; /*重要*/ /*子类居中*/
}
/* 子类 */
.div-big .div-small{
	width: 100px;
	height: 100px;
	background-color: lightcoral;/*红背景色*/
}

一、位于父类中心

图片演示

html代码

	<div class="div-big">
		<div class="div-small">1</div>
	</div>

css代码(方法一)

/* 父类 */
.div-big{
	width: 600px;
	height: 400px;
	background-color: lightgreen;/*绿背景色*/
	display: flex;/*重要*/ /*横向排列*/
	align-items: center; /*重要*/ /*子类居中*/
}
/* 子类 */
.div-big .div-small{
	width: 100px;
	height: 100px;
	background-color: lightcoral;/*红背景色*/
	margin: 0 auto; /*重要*/ /*子类居中*/
}

css代码(方法二)

/* 父类 */
.div-big{
	width: 600px;
	height: 400px;
	background-color: lightgreen;/*绿背景色*/
	display: flex;/*重要*/ /*横向排列*/
}
/* 子类 */
.div-big .div-small{
	width: 100px;
	height: 100px;
	background-color: lightcoral;/*红背景色*/
	margin: 0 auto; /*重要*/ /*子类居中*/
	align-self: center; /*重要*/ /*子类居中*/
}

一、位于父类右侧垂直居中

图片演示

html代码

	<div class="div-big">
		<div class="div-small">1</div>
	</div>

css代码

/* 父类 */
.div-big{
	width: 600px;
	height: 400px;
	background-color: lightgreen;/*绿背景色*/
	display: flex;/*重要*/ /*横向排列*/
	align-items: center;/*重要*/ /*垂直方向居中*/
	justify-content: flex-end;/*重要*/ /*水平方向底部*/
}
/* 子类 */
.div-big .div-small{
	width: 100px;
	height: 100px;
	background-color: lightcoral;/*红背景色*/
}

一、位于父类左下角

图片演示

html代码

	<div class="div-big">
		<div class="div-small">1</div>
	</div>

css代码

/* 父类 */
.div-big{
	width: 600px;
	height: 400px;
	background-color: lightgreen;/*绿背景色*/
	display: flex;/*重要*/ /*横向排列*/
	align-items: flex-end;/*重要*/ /*横向上底部*/
}
/* 子类 */
.div-big .div-small{
	width: 100px;
	height: 100px;
	background-color: lightcoral;/*红背景色*/
}

一、位于父类下部水平居中

图片演示

html代码

	<div class="div-big">
		<div class="div-small">1</div>
	</div>

css代码

/* 父类 */
.div-big{
	width: 600px;
	height: 400px;
	background-color: lightgreen;/*绿背景色*/
	display: flex;/*重要*/ /*横向排列*/
	align-items: flex-end;/*重要*/ /*横向上底部*/
}
/* 子类 */
.div-big .div-small{
	width: 100px;
	height: 100px;
	background-color: lightcoral;/*红背景色*/
	margin: 0 auto;/*重要*/ /*水平居中*/
}

一、位于父类右下角

图片演示

html代码

	<div class="div-big">
		<div class="div-small">1</div>
	</div>

css代码

/* 父类 */
.div-big{
	width: 600px;
	height: 400px;
	background-color: lightgreen;/*绿背景色*/
	display: flex;/*重要*/ /*横向排列*/
	align-items: flex-end;/*重要*/ /*垂直方向底部*/
	justify-content: flex-end;/*重要*/ /*水平方向底部*/
}
/* 子类 */
.div-big .div-small{
	width: 100px;
	height: 100px;
	background-color: lightcoral;/*红背景色*/
}

相对绝对实现上下左右水平居中垂直居中请看下一篇文章。

CSS布局——一个DIV上下左右水平居中垂直居中布局(relative-absolute)_qq591840685的专栏-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值