【发布时间】: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 行有一个未关闭的
<?php标签 -
感谢 Dan,在我的代码中它是正确的 - 只是在我在这里发布的内容中不正确。
标签: html css flexbox responsive-design height