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

[html] 第610天 写一个布局,它的宽度是不固定的100%,如何让它的宽度始终是高度的一半呢? #3312

Open
haizhilin2013 opened this issue Dec 15, 2020 · 4 comments
Labels
html html

Comments

@haizhilin2013
Copy link
Collaborator

haizhilin2013 commented Dec 15, 2020

第610天 写一个布局,它的宽度是不固定的100%,如何让它的宽度始终是高度的一半呢?

3+1官网

我也要出题

@haizhilin2013 haizhilin2013 added the html html label Dec 15, 2020
@wheatup
Copy link

wheatup commented Dec 16, 2020

利用paddingmargin使用百分比时以元素宽度为基准的特性,可以以此固定元素的宽高比:

<div class="ratio" style="--ratio: 0.5;"></div>
.ratio {
  background-color: salmon;
  position: relative;
}

.ratio::before {
  content: '';
  display: block;
  padding-top: calc(1 / var(--ratio) * 100%);
}

@shuaibichuang
Copy link

width:50vh;
height:100vh;

@FreddyZeng
Copy link

em,宽度为1.0,高度为0.5

@wind8866
Copy link

原理:padding-bootom如果是百分比,是相对于父元素宽度的百分比。

<div class="wh-wrap">
  <div class="width-height2"></div>
</div>
.wh-wrap {
  width: 100px;
}
.width-height2 {
  width: 100%;
  padding-bottom: 200%;
  background: #daf;
}

@haizhilin2013 haizhilin2013 changed the title [html] 第610天 写一个布局,它的宽度是不固定的100%,如果让它的宽度始终是高度的一半呢? [html] 第610天 写一个布局,它的宽度是不固定的100%,如何让它的宽度始终是高度的一半呢? Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
html html
Projects
None yet
Development

No branches or pull requests

5 participants