元素距左右屏幕为10px 高度为宽度的50% 垂直居中

<style>
	*{padding:0;margin:0;box-sizing:border-box;}
	html,body{height:100%;}
	.a{height:100%;margin: 0 10px;display:flex;align-items:center;}
	.b{width:100%;padding-bottom:50%;background-color:red;position:relative;}
	.c{width:100%;height:100%;font-size:30px;display:flex;justify-content:center;align-items:center;position:absolute;}
</style>
<body>
	<div class="a">
		<div class="b">
			<div class="c">A</div>
		</div>
	</div>
	
</body>

其中padding-bottom:50%是相对于父元素的width,由于.b的width和.a的width相同,所以.b的高度就是.b的padding的高度就是.b的宽度的一半。
.b为position:relative,.c为position:absolute,所以.c的宽高的百分比是相对于.b的content+padding的值来说的\

使用calc和vw

<style>
        *{padding:0;margin:0;box-sizing: border-box;}
        html,body{height: 100%;}
        .f{height: 100%;width:100%;}
        .a{background-color: blue;width:calc(100vw - 40px);margin-left: 20px;height:calc(50vw - 20px);
        position: relative;top:50%;transform: translateY(-50%) }
        .c{width:100%;height: 100%;display: flex;justify-content: center;align-items: center;font-size:60px;color:white;}
    </style>
    <body>
    <div class="f">
            <div class="a">
                <div class="c">A</div>
            </div>
    </div>
</body>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值