【问题标题】:CSS Flexbox - how to make a parent div the height of a nested child imgCSS Flexbox - 如何使父 div 成为嵌套子 img 的高度
【发布时间】:2020-11-25 20:32:59
【问题描述】:

这可能已经得到解答,但我很难找到它。我有一个带有幻灯片包装的容器,其中包含带有英雄 img 和页脚的幻灯片。我希望英雄 img 填充视口的整个宽度(它是一个长横幅),但我想保持图像的比例,并且如果视口的宽度发生变化,则容器的其余部分的高度也会发生变化。我必须拥有幻灯片功能的所有嵌套。

除了高度,一切正常。

我尝试过使用 display:flex 来做到这一点,但我必须在容器级别设置一个固定高度,否则什么都不会显示。

有没有办法在 css 中做到这一点,还是我必须使用 js?

    <div class="container">
      <div class="slide-wrap">
        <?php 
          
         //php to dynamically create the slides

            <!-- Individual slides -->
            <div class="slide">
              <img class="hero-img" src="<?php echo get_the_post_thumbnail_url(); ?>" alt="">

              <!-- Hero showcase footer -->
              <div id="hero-footer">
                <a class="btn-secondary" href="<?php the_permalink(); ?>">Press</a>
              </div>

            </div>

          <?php } wp_reset_postdata(); ?>
      </div>
      <div class="arrows">
        <div><i id="arrow-left" class="fas fa-arrow-left fa-2x" href="#"></i></div>
        <div><i id="arrow-right" class="fas fa-arrow-right fa-2x" href="#"></i></div>
      </div>
      
    </div>

和css:

.container {
  position: relative;
  overflow: hidden;
  padding: 0;
  background: black;
  display: inline-block;
  height: 750px;
}

.slide {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 0.4s ease-in-out;
}

.slide.current {
  opacity: 1;
  z-index: 2;
}

.hero-img {
  width: 100%;
}

#hero-footer {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: $text-color;
}

#hero-footer a {
  margin: 1rem;
  font-size: 1.5rem;
  width: 200px;
}

.arrows {
  display: flex;
  justify-content: space-between;
  padding: 0 1rem;
}

#arrow-left {
  left: 0;
  margin-left: 30px;
  z-index: 10;
}

#arrow-right {
  right: 0;
  margin-right: 30px;
  z-index: 11;
}

【问题讨论】:

  • 您是否尝试过在 .slide 类中使用 bottom:0 和 right:0 来提供完整的高度和完整的宽度?
  • 我刚刚试过了,容器还是折叠的。换句话说,什么都没有出现。
  • 您能否提供 PHP 输出的代码示例?
  • 我刚刚注意到您的示例第 3 行有一个未关闭的 &lt;?php 标签
  • 感谢 Dan,在我的代码中它是正确的 - 只是在我在这里发布的内容中不正确。

标签: html css flexbox responsive-design height


【解决方案1】:

我假设您正在寻找一种保持纵横比的 CSS 解决方案。

这是一个在 SCSS 中创建 16x9 容器的解决方案:

[data-ar] {
    position: relative;
    &:before {content: ''; display: block;}
    > * {top: 0; left: 0; width: 100%; height: 100%; display: block; position: absolute;}
}
[data-ar='16:9']:before {padding-top: 9 / 16 * 100%;}

然后将该类作为包装器应用到您的图像:

<div data-ar="16:9">
    <img class="hero-img" src="<?php echo get_the_post_thumbnail_url(); ?>" alt="">
</div>

只需更改变量即可制作任何其他任意纵横比:

[data-ar='16:9']:before {padding-top: 9 / 16 * 100%;}
[data-ar='4:3']:before {padding-top: 3 / 4 * 100%;}
[data-ar='176:43']:before {padding-top: 43 / 176 * 100%;}

【讨论】:

  • 我刚试过这个,我遇到的持续问题是父容器仍然折叠。 .container、.slide-wrap、.slide 都是嵌套的,因此 img 高度是隐藏的。在检查器中,我可以看到图像有一个高度,但它的父母都没有,所以什么都没有出现。
  • 您可以尝试将.slide-wrap 包装在纵横比div 中吗? &lt;div data-ar="16:9"&gt;&lt;div class="slide-wrap"&gt;...&lt;/div&gt;&lt;/div&gt; 或者可能是 &lt;div class="slide-wrap" data-ar="16:9"&gt;
  • 我确实试过这个,它也没有效果。最后我选择了一个 javascript 选项。
猜你喜欢
  • 2018-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
相关资源
最近更新 更多