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

宽高自适应正方形 #235

Open
Sunny-117 opened this issue Nov 4, 2022 · 2 comments
Open

宽高自适应正方形 #235

Sunny-117 opened this issue Nov 4, 2022 · 2 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@huccct
Copy link

huccct commented May 5, 2023

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .container {
        display: flex;
      }
      .item {
        flex-basis: auto;
        flex-grow: 1;
        padding-bottom: 100%;
        background-color: #f1f1f1;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="item"></div>
    </div>
  </body>
</html>

@z1the3
Copy link

z1the3 commented May 9, 2023

<!-- 第一种方案,利用vw  -->
<style>
  .container{
    width: 50%;
    background: #ccc;
    height: 50vw;
  }
</style>

<body>
  <div class="container"></div>
</body>


<!-- 第二种方案,利用padding-top  -->
<style>
  .container {
    width: 50%;
    /* 该元素真实高度为0,我们看到的只是它往下偏移而露出的padding背景色 */
    /* padding-top后的百分比以父容器**宽度**为准 */
    padding-top: 50%;
    height: 0;

    top: 50%;
    left: 50%;
    position: absolute;
    transform: translate(-50%, -50%);
    background-color: antiquewhite;
  }
</style>

<body>
  <div class="container">
  </div>
</body>

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

3 participants