css 调整相邻容器的高度

edqdpe6u  于 2023-04-01  发布在  其他
关注(0)|答案(1)|浏览(159)

我是一个初学者,我目前正在尝试创建一个简单的容器布局,其中包括一个头部容器,由3个较小的容器填充。容器中充满了图像,当页面的宽度变得太小时,中心容器中的图像开始收缩,这正是我想要的现在的问题是,我想整个头部垂直收缩,但其他2个容器只是坚持自己的大小,无论我所尝试的。
例如,我的标题总宽度是640px,高度是128px

Left = 64x128
Center = Whatever is left between L and R
Right = 128x128

Picture to visualize everything
How it looks when the width is changing
当中心缩小例如20px高度时,我希望它是什么:

Left = 64x108 (keeps its width but adjusts the height based on center)
Center = Still hatever is left between L and R
Right = 108x108 (adjusts both, width and height based on center height)

当空间不足时,它会缩小并保持其纵横比,这意味着它在水平方向上总是完美的,但由于L和R没有缩小,它在垂直方向上不再合适。我如何使L和R从中心得到它们的高度?
我的当前代码:
index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <div class="page">
            <div class="header">
                <div class="menubtn"></div>
                <div class="logo"></div>
                <div class="avatar"></div>
            </div>
            <div class="main"></div>
        </div>
    </body>
</html>

style.css

body,html {
    height: 100%;
    margin: 0;
    padding: 0;
}

.page {
    border: 2px solid blue;
    margin: 0;
    padding: 0;
    margin: 0 auto;
    max-width: 640px;
    width: 100%;
    height: 80%;
    z-index: 1;
}

.header {
    border: 2px solid green;
    position: fixed;
    width: 100%;
    max-width: 640px;
    height: 100%;
    max-height: 128px;
}

.menubtn {
    border: 2px solid red;
    position: absolute;
    width: 64px;
    height: 128px;
}

.logo {
    border: 2px solid yellow;
    position: absolute;
    left: 64px;
    right: 128px;
    width: auto;
    height: 100%;
}

.logo img {
    width: 100%;
}

.avatar {
    border: 2px solid black;
    position: absolute;
    right: 0;
    width: 128px;
    height: 128px;
}

.avatar img {
    max-width: 100%;
    max-height: 100%;
    margin: 4px;
    width: 120px;
    height: 120px;
}

请忽略它看起来有多乱,我删除了大量不相关的东西,使所有的东西都变短,很多东西都是剩余的,或者首先就不应该在那里哈哈
这样的东西只需要CSS就可以实现吗?还是需要脚本?

qzwqbdag

qzwqbdag1#

你想要这个吗?

body,
    html {
      height: 100%;
      margin: 0;
      padding: 0;
    }

    .page {
      border: 2px solid blue;
      margin: 0;
      padding: 0;
      margin: 0 auto;
      max-width: 640px;
      width: 100%;
      height: 80%;
      z-index: 1;
    }

    .header {
      border: 2px solid green;
      position: fixed;
      width: 100%;
      max-width: 640px;
      display: flex;
      align-items: stretch;
    }

    .menubtn {
      border: 2px solid red;
      width: 64px;
      min-width: 64px;
      object-fit: cover;
    }

    .menubtn img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .logo {
      border: 2px solid yellow;
      width: auto;
      min-width: 60%;
      min-height: min-content;
    }

    .logo img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .avatar {
      border: 2px solid black;
      padding: 4px;
    }

    .avatar img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
<!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>
  </style>
</head>

<body>
  <div class="page">
    <div class="header">
      <div class="menubtn">
        <img
          src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOAAAADgCAMAAAAt85rTAAAA8FBMVEX//////w4AAAD/AAAFBQWfn5+aAAD4+PjR0dHu7u76+g7w8A2wsLDr6+ttbW3e3t7Q0Avu7g2mpgmBgYE6Ojr39w4wMAPHxws2NgOsrKylpaXl5eXh4QxrawZwcAbW1gy8vLy6ugrDw8NBQUFycnKRkQgkJCRlZWXX19cbGxsPDw89PQNdXQUYGAEsLCzm5g2FhQdFRQRVVVWRkZEeHgF4eAdhYQWdnQl8fHyxsQokJAJNTQQqKgNVVQUKCgGNAACxAADbAABYWFgzA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ir6lSBB8XgNLZaCJV/UILXAJzbO3wZVQrFX7wNX8rq86WRFQyaW+RzHY28bNUdiCWr3uvsMeYlqiHqopj0M0v7CnmCZHYYaq9xVw1pphgRnorwlz3gI0YZuVocAdQkLw49ro23GTQkBJerj9bSFDYZKYwbNSwnW54oaGBpWNgqcU9kAb6MM2J1ePLaBhj1qA9hBdm4KGB3aAhuTHmWumhGawezD8URK4tYbiWXO6gXt1hOrCfgPujmKGT4EtpjqOccUNbowQpFBtr+ED8t30JpTMoBGj5xxe/IQGeE14QoNS05IL3B5FsedhcJ8HXSe4oiCNW9tz23HpOs6c2/yvEa+6x20I9B2nxB28at3HSBEn6JnGI80ch8cP4spVcNtVtfTxc5zOPUM/lq/InSip96h29z7+RzHYbdyJkrokzBKC3bWe25Gyu+H34tF9Il3HTdCyatHpfbvBv1r/SUN1EcOZ1tiNoMakXHFt1qh5gvowtJ4Gaw6Zti/dL13L0axp0YkhC46P6xKqO/aR6IUKtXckFMeit73hVmnfmwB27caV8GVrPOqr6HfsxFFbXw4qd1Px3MVXpwnjRunE671zvPR3NZA8nDT8tr5a96GsB94T9Q1395MbZdGab1ZEp1kAiKeThEJ2038MM5IPg7Yt6Tve2I8N5+nXLgxb66f3htZ20FBm2LgdVHYPd3Z8v2duJ29PHhukc5/HOmIwVGR8FyOH2nPjvl+ZtHdt2242Dw+bzdk/7N1J//7o4dZ3w2XEcX0N2t0X6f1u8LA04bwXz5nzpdF18ODC46QV4ZHrTpyOz4NHGAbdyKemm+NobXN1qxWBVqAgW5hqJ3dW7cQ4M91IZ6rD4FETcd1T2BY8AqRP69ON4OH743xcMGReeqDUGk8VhWu3V8/ELlJISGdPC6OphHy93toslIxaciSkS5lOvbo17R4LWZ1tD0961Xrn9H18NT/MN9wvFkvzff0LrVYnc1o6KCpvwZ8gQYIECRIkSJAgQYIECRIkSJAgQYIECRIs8X8/TCGLW/tPGQAAAABJRU5ErkJggg=="></img>
      </div>
      <div class="logo">
        <img src="https://picsum.photos/536/354"></img>
      </div>
      <div class="avatar">
        <img
          src="https://images.unsplash.com/photo-1481349518771-20055b2a7b24?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NHx8cmFuZG9tfGVufDB8fDB8fA%3D%3D&w=1000&q=80"></img>
      </div>
    </div>
    <div class="main"></div>
  </div>
</body>

</html>

相关问题