将部分拉伸到背景图像的高度

时间:2020-01-21 16:56:38

标签: css background-image

尽管示例未考虑引导程序,但我正在使用引导程序。

我需要的是section延伸到背景图像的高度。

我已经建立了一个最小的示例,但是它似乎并未加载背景图片:

https://codepen.io/lib0/pen/zYxyoLd

1 个答案:

答案 0 :(得分:1)

因此,这与您采取的方法不同,我遵循了此示例CSS: How to set container size equal to background image size。但是,它不使用background-image属性。我不确定这是否对您有必要。

HTML

<div class="wrap">
    <img src="https://escolaeducacao.com.br/wp-content/uploads/2015/07/dna.jpg" />

    <section class="content">
    to be expanded to the size of the background image.
    </section>

</div>

CSS

.wrap {
    display: inline-block;
    position: relative;
}

.wrap img + .content {
    position: absolute;
    top: 0;
    left: 0;
}

将自动换行设置为position: relative会导致设置为position: absolute的子元素相对于该父元素定位自身