Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4、如何实现一个宽度自适应,高度为宽度的一半的矩形,并且该矩形需要水平垂直居中(字节) #4

Open
shiwanjun1995 opened this issue Jan 18, 2022 · 0 comments

Comments

@shiwanjun1995
Copy link
Owner

shiwanjun1995 commented Jan 18, 2022

分析两个点:
1、宽度自适应,高度始终为宽度一半;【此处需要记住一个知识点:padding-top/bottom和margin-top/bottom都是相对于父元素的宽度来计算的,参考的是父容器的宽度。】
2、垂直水平居中。【可以利用flex容器水平轴和交叉轴的属性来设置】
<div class="fa"></div>

.fa {
width: 100%;
padding-bottom: 50%; // 高度由 padding 撑开,高度也就相对于父元素宽度
height: 0; // 同时需要将 height 设置为0,这是为了将元素高度完全交给 padding 负责
display: flex;
align-items: center;}
@shiwanjun1995 shiwanjun1995 changed the title 4、div 垂直水平居中,并完成 div 高度永远是宽度的一半(宽度可以不指定) 4、div 垂直水平居中,并完成 div 高度永远是宽度的一半(宽度可以不指定)(字节) Jan 18, 2022
@shiwanjun1995 shiwanjun1995 changed the title 4、div 垂直水平居中,并完成 div 高度永远是宽度的一半(宽度可以不指定)(字节) 4、如何实现一个宽度自适应,高度为宽度的一半的矩形,并且该矩形需要水平垂直居中(字节) Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant