【问题标题】:How to shrink divs vertically while keeping aspect ratio and location relative to other divs?如何在保持纵横比和相对于其他 div 的位置的同时垂直缩小 div?
【发布时间】:2017-03-15 07:06:59
【问题描述】:

在 HTML 中,默认情况下图像倾向于保持其纵横比,这使得在调整窗口大小时可以轻松地水平缩小图像并保持比例:

在 gif 中,请注意图像如何不仅保持纵横比,而且保持在一起。它们不只是在自己的容器中变小。这是我想要完成的,但是是垂直的。 (当窗口从下往上拖动时)。

这个简单演示的所有代码如下。目前,如果您使 sn-p 全屏并垂直缩小窗口,则所有内容都会像这样被剪裁:

我想将所有内容都保留在屏幕上,就像水平缩放时一样。我希望它以完全相同的方式表现,但是当窗口被缩放时垂直。

我尝试将每个元素的宽度设为auto 或将其值设为display: inline-block。有什么想法吗?

/* //////////////////////////// IMPORTS ////////////////////////// */
@import url( "https://necolas.github.io/normalize.css/latest/normalize.css" );

/* //////////////////////////// INITIAL ////////////////////////// */
body {
  max-width: 1024px;
  margin: auto;
  text-align: center;
}
img, footer {
  width: 100%;
}
html {
  height: 100%;
}
p {
  margin: 0;
}
.img-contr {
  width: 33.333%;
  float: left;
}

/* /////////////////////////// CORE CLASSES ////////////////////// */
.clr-fix::after {
  content: "";
  display: block;
  clear: both;
}
.v-cntr {
  position: relative;
  top: 50%;
  transform: translateY( -50% );
}
<head>
  <style>
    * { 
        box-sizing: border-box;
        overflow: hidden;
        outline: 1px solid #f00;
      }
  </style>
</head>
<body class="v-cntr">

  <!-- -------------------------- HEADER ------------------------ -->
  <header>
    <img src="http://svgshare.com/i/xH.svg" 
         alt="blue old fashioned car"
    >
  </header>

  <!-- --------------------------- MAIN ------------------------- -->
  <main class="clr-fix">
    <div class="img-contr">
      <img src="http://svgshare.com/i/xe.svg" alt="car key">
    </div>
    <div class="img-contr">
      <img src="http://svgshare.com/i/wz.svg" alt="seat belt">
    </div>
    <div class="img-contr">
      <img src="http://svgshare.com/i/vu.svg" alt="car pedals">
    </div>
  </main>

  <!-- -------------------------- FOOTER ------------------------ -->
  <footer>
    <p>footer</p>
  </footer>
</body>

编辑所以我制作了一个 gif,准确地显示了我想要完成的任务。但我通过截屏我的网站并将整个内容制作成图像来做到这一点。目标是在我的代码中使用 div 中的多个图像来执行此操作。这是我想要完成的:

这个问题:Vertically center image on page and maintain aspect ratio on resize 是一个图像的解决方案。当我在我的代码中尝试它时,图像会缩放但它们会分开。将每个图像设置为max-height: 100% 根本无法解决。

【问题讨论】:

  • @ZachSaucier 如果我在所有图像上应用max-height: 100%,例如该答案的解决方案表明它对我的布局的影响与我在此处显示的完全不同,因为 div 容器。遗憾的是,我认为该解决方案行不通。
  • 子元素相对于父元素的大小
  • 您可以使用this article中的一种方法来获得您想要的效果
  • 你能用弹性盒子吗? &lt;header&gt; 和 .img-contr 是否设置了高度?例如,它们分别设置了 100% 和 33% 的宽度。

标签: html css resize viewport responsive


【解决方案1】:

好的。因此,在vmin 单位和aspect ratio media queries 的帮助下,我能够整理出一些最适合我的方法来解决这个问题:

/* ///////////////////////////////// IMPORTS /////////////////////////////// */
@import url( "https://necolas.github.io/normalize.css/latest/normalize.css" );

/* ///////////////////////////////// INITIAL /////////////////////////////// */
* {
  box-sizing: border-box;
  overflow: hidden;
}
html {
  height: 100%;
  background-color: #fff;
}
html, body {
  padding: 0.5%;
}
body {
  max-width: 1000px;
  background-color: #eee;
}
body, .top-contr {
  margin: auto;
}
p {
  margin: 0;
  padding: 0;
  text-align: center;
  color: #aaa;
}

/* ///////////////////////////////// STRUCTURE ///////////////////////////// */

/* / / / / / / / / / / / / / / / / / / HEADER / / / / / / / / / / / / / / / /*/
header, footer, main .top-contr {
  padding: 1%;
}
header .top-contr {
  background-color: #ddd;
  padding: 15%;
}
.top-contr, .v-cntr, .v-cntr-a {
  position: relative;
}
.cnt {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

/* / / / / / / / / / / / / / / / / / / MAIN / / / / / / / / / / / / / / / / /*/
main .top-contr {
  width: 33.333%;
  float: left;
}
main .mid-contr {
  background-color: #ccc;
  padding: 45%;
}
main .cnt {
  padding: 4%;
}

/* / / / / / / / / / / / / / / / / / / FOOTER / / / / / / / / / / / / / / / /*/
footer .top-contr {
  background-color: #bbb;
  padding: 5%;
}

/* //////////////////////////////// CORE CLASSES /////////////////////////// */
.clr-fix::after {
  content: "";
  display: block;
  clear: both;
}
.v-cntr {
  top: 50%;
  transform: translateY( -50% );
}

/* /////////////////////////////// MEDIA QUERIES /////////////////////////// */
@media ( min-aspect-ratio: 3/2 ) {
  body {
    width: 135vmin;
    height: 100vmin;
  }
}
@media ( min-aspect-ratio: 3/2 ) and ( min-width: 1000px ) {
  .v-cntr-a {
    top: 50%;
    transform: translateY( -50% );
  }
}
<body class="v-cntr">                  <!-- << v-cntr = vertically center -->
  <div class="wrapper v-cntr-a">
  
    <!-- ----------------------------- HEADER --------------------------- -->
    <header>
      <div class="top-contr">                   <!-- << contr = container -->
        <div class="cnt">                           <!-- << cnt = content -->
          <p class="v-cntr">header</p>
        </div>
      </div>
    </header>

    <!-- ------------------------------ MAIN ---------------------------- -->
    <main>
      <div class="top-contr">
        <div class="mid-contr">
          <div class="cnt">
            <p class="v-cntr">rectangle 1</p>
          </div>
        </div>
      </div>
      <div class="top-contr">
        <div class="mid-contr">
          <div class="cnt">
            <p class="v-cntr">rectangle 2</p>
          </div>
        </div>
      </div>
      <div class="top-contr">
        <div class="mid-contr">
          <div class="cnt">
            <p class="v-cntr">rectangle 3</p>
          </div>
        </div>
      </div>
    </main>

    <!-- ----------------------------- FOOTER --------------------------- -->
    <footer>
      <div class="top-contr">
        <div class="cnt">
          <p class="v-cntr">footer</p>
        </div>
      </div>
    </footer>
  </div>
</body>

还可以在此处查看接受的答案:https://stackoverflow.com/questions/42757168/how-to-keep-aspect-ratio-on-a-vertically-scaling-div-not-image?noredirect=1&lq=1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 2021-11-23
    • 1970-01-01
    相关资源
    最近更新 更多