DaZeng:居中问题的方法总结

10.21-10.25之 WEB任务(一)

1、一周复习hcj,HTML一般用的也就那些东西,了解一下行内元素、块级元素、行内块元素。
2、清除浮动(好像是五种还是六种方式)
3、BFC(概念、触发、作用、应用)
4、居中问题(水平垂直居中方法,大概有十来种,你自己总结一下)
5、圣杯布局(flex、float、position要会,尤其前两种,gird和table了解就行)
6、轮播图:js原生做弄清楚原理有箭头,实现了写导航点自动播放然后无缝

注:就是不是让你全背下来,但是常用的要会,不能写什么都要翻文档,菜鸟或者w3c都可以 。

上一篇:BFC的概念、触发、作用及应用

四、居中问题的方法总结
元素水平居中
  • 方法一:最简单的当然就是margin: 0 auto
    HTML:
<body>
		<div class="father">
			<div class="son">!我是居中!</div>
		</div>
	</body>

CSS:

.father{
				width: 18.75rem;
				height: 18.75rem;
				background-color: lightblue;
				/* margin: 0 auto; */
			}
			
			.son{
				width: 6.25rem;
				height: 6.25rem;
				background-color: gray;
				text-align: center;//文字水平居中
				line-height: 6.25rem;//文字行高和height相同文字垂直居中
				margin: 0 auto;
			}

图示:

  • 方法二:display: inline-block;
    HTML:不变
    CSS:
<style>
			.father{
				width: 18.75rem;
				height: 18.75rem;
				background-color: lightblue;
				text-align: center;//行内元素son水平居中
			}
			
			.son{
				width: 6.25rem;
				height: 6.25rem;
				background-color: gray;
				line-height: 6.25rem;
				display: inline-block;//因为inline改变不了宽高,block块级元素操作不了居中,inline-block刚刚好
			}
		</style>

效果图和上图一样

元素水平垂直居中
  • 方法一:位置
    父:position:relative
    子:position:absolute left:50% top:50%
    然后子元素在margin中对应的left和top减去自身的宽度或用transform: translate(-50%,-50%);

    废话不多说,上CSS代码:
			.father{
				width: 18.75rem;
				height: 18.75rem;
				background-color: lightblue;
				position: relative;
			}
			
			.son{
				width: 6.25rem;//等同于100px,HBuilder自动换算的
				height: 6.25rem;
				background-color: gray;
				line-height: 6.25rem;
				text-align: center;
				position: absolute;
				left: 50%;
				top: 50%;
				margin: -50px 0 0 -50px;//这里的-50px就是自身宽度*百分比计算出的。
				/* transform: translate(-50%,-50%);   margin和transform方法任选其一,效果相同*/
			}

  • 方法二:flex布局
    要点:都在father身上做改变
    html不变,css如下:
			.father{
				width: 18.75rem;
				height: 18.75rem;
				background-color: lightblue;
				display: flex;
				justify-content: center;//使子元素水平居中
				align-items: center;//使子元素垂直居中
			}
			
			.son{
				width: 6.25rem;
				height: 6.25rem;
				background-color: gray;
				line-height: 6.25rem;
				text-align: center;
			}
  • 方法三:table-cell布局
    table-cell相当于表格的td,td为行内元素,无法设置宽和高,所以要嵌套一层在html中,并设置dispiay:inline-block
    HTML:
<div class="father">
			<div class="son">
				<div class="grandson">居中啦!</div>
			</div>
		</div>

CSS:

			.father{
				width: 18.75rem;
				height: 18.75rem;
				background-color: lightblue;
				display: table;
			}
			.son{
				background-color: pink;
				display: table-cell;
				vertical-align: middle;//垂直布局
				text-align: center;//水平布局
			}
			
			.grandson{
				width: 6.25rem;
				height: 6.25rem;
				background-color: gray;
				display: inline-block;
				text-align: center;
				line-height: 100px;
			}

下一篇:圣杯布局、双飞翼布局

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Da Zeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值