css元素子元素自适应大小的两种方法

简单说下css中子元素自适应大小的两种办法

1. 子元素设置百分比

<div class="box">
	<div class="content"></div>
</div>
.box{
	margin:100px auto;
	width: 100px;
	height: 100px;
	background: rgba(0, 0, 0, 0.6);
}
.content{
	width: 80%;
	height: 80%;
	margin-top: 10%;
	margin-left: 10%;
	background-color: palevioletred;
}
$('.box').click(function(){
	$(this).height('500px');
	$(this).width('500px')
})

在这里插入图片描述
2. 子元素不设置宽高(必须),通过定位来实现元素自适应大小
补充说下,我也是听好朋友说的才发现还有这种操作,感觉很神奇~~哈哈

.content{
	position: absolute;
	top: 10%;
	left: 10%;
	bottom: 10%;
	right: 10%;
	background:sandybrown
}

在这里插入图片描述
在开发过程中可以根据不同情况合理运用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值