css - 图像和内容之间的差距随着背景大小的增加而不断增加

标签 css

.header-img {
    background-image: url("https://cdn.pixabay.com/photo/2016/04/15/04/02/water-1330252__340.jpg");
    background-size: contain;
    background-repeat: no-repeat;
    height: 300px;
}

随着我减小浏览器窗口的大小,图像会缩放并且图像和内容之间的差距会不断增加。 我需要解决这个问题,我需要保持背景大小。

<div class="header-img">
</div>

<div class="content">
sddsfsdfsdfdsfdsf
</div>

https://jsfiddle.net/dz47c1qn/

最佳答案

如果您希望图像保持原始高度,请使用background-size: cover

.header-img {
	background-image: url("https://cdn.pixabay.com/photo/2016/04/15/04/02/water-1330252__340.jpg");
	background-size: cover;
	background-repeat: no-repeat;
    height: 300px;
}
<div class="header-img">
</div>

<div class="content">
sddsfsdfsdfdsfdsf
</div>

关于css - 图像和内容之间的差距随着背景大小的增加而不断增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55540378/

相关文章:

javascript - 当用户上下滚动屏幕时旋转图像

html - 使用绝对定位的主要元素时,将页脚放在底部

html - 将 <li> 与 &lt;input&gt; 与文本对齐

css - 一行中的两个元素,但不居中

css - 如何在没有很多类的情况下让页脚停留在每页的底部?

html - 将纸图标按钮对齐到脚手架的右上角

html - DIV float :left with another div with display: inline-block wraps

c# - 如何对 <HEAD> 部分中的文件进行版本控制?

javascript - 如何改变div的滚动速度

css - flexbox CSS 可以根据宽高比调整大小吗?