【发布时间】:2018-10-10 18:24:08
【问题描述】:
我有一个带有背景图片的容器。我希望它:
- 始终填满容器的宽度
- 如果容器的高度变大,图像应该贴在底部并增加顶部的间隙
- 如果容器的高度比图片短,它应该在顶部保持 20px 的间隙,隐藏图片的底部(因为它溢出)
- 我不知道 CSS 中图像的高度/宽度
- 图像不应倾斜(应保持纵横比)
似乎我需要的是宽度上的contain,而不是高度,但是我不确定如何从顶部做最小偏移。
在这里查看我的尝试:
background-size: 100% auto;
background-position: left 20px;
/* works when the height is shorter than the image
------------------
| |
| |
|................|
|. .|
|. .|
|. .|
|. .bg .|
|. .|
|. .|
|. .|
------------------
. . <- this is clipped off, that is fine.
................
*/
/*
does not work when the height is larger than the image
------------------
| |
| |
|................|
|. .|
|. .|
|. .|
|. .bg .|
|. .|
|. .|
|. .|
|. .|
|. .|
|................| <- I want this to be stuck to the bottom
| |
| |
------------------
*/
background-size: 100% auto;
background-position: left bottom;
/* works when the height is taller than the image
------------------
| |
| |
| |
|................|
|. .|
|. .|
|. .|
|. .bg .|
|. .|
|. .|
|. .|
|. .|
|................| <- stuck to the bottom, good!
------------------
*/
/*
does not work when the height is shorter than the image
................
. .
. .
. .
. .bg .
. . <- This is clipped off
------------------
|. .|
|. .|
|. .|
|................|
------------------
*/
background-size: cover;
background-position: left 20px;
/* works when the width is wider than the image (scales it up)
------------------
| |
| |
|................|
|. .|
|. .|
|. .|
|. .bg .|
|. .|
|. .|
|. .|
------------------
. . <- this is clipped off, that is fine.
................
*/
/*
does not work when the width is narrower than the image, and the height is taller
------------------
| |
| |
|................|.....
|. | . <- I do not want the width to overflow
|. | .
|. | .
|. .bg | .
|. | .
|. | .
|. | .
|. | .
|. | .
|................|.....
------------------
*/
我想要什么:
/* if the container is shorter than the image
------------------
| |
| |
|................| <- 20px offset from the top, full width of the container
|. .|
|. .|
|. .|
|. .bg .|
|. .|
|. .|
|. .|
------------------
. . <- this is clipped off, that is fine.
................
*/
/*
if the container is larger than the image
------------------
| |
| |
| |
| |
|................| <- full width of the container
|. .|
|. .|
|. .|
|. .bg .|
|. .|
|. .|
|. .|
|. .|
|. .|
|................| <- stuck to the bottom
------------------
*/
测试片段:
/* width/heights are for illustrative purposes - actual width-heights are unknown */
div {
background-size: cover;
background-position: left 20px;
background-repeat: no-repeat;
margin-bottom: 50px;
border: 1px solid red;
width: 200px;
height: 300px;
}
.taller {
height: 500px;
}
.shorter {
height: 100px;
}
.wider {
width: 400px;
}
.narrower {
width: 200px;
}
<!-- this image size and the container size are variable depending on author input - these are included as test cases, but I do not know the sizes -->
<strong>Same Size</strong>
<div style="background-image: url('https://picsum.photos/200/300');"></div>
<strong>Taller</strong>
<div class="taller" style="background-image: url('https://picsum.photos/200/300');"></div>
<strong>Wider</strong>
<div class="wider" style="background-image: url('https://picsum.photos/200/300');"></div>
<strong>Narrower</strong>
<div class="narrower" style="background-image: url('https://picsum.photos/200/300');"></div>
<strong>Shorter</strong>
<div class="shorter" style="background-image: url('https://picsum.photos/200/300');"></div>
<strong>Taller & Wider</strong>
<div class="taller wider" style="background-image: url('https://picsum.photos/200/300');"></div>
<strong>Taller & Narrower</strong>
<div class="taller narrower" style="background-image: url('https://picsum.photos/200/300');"></div>
<strong>Shorter & Wider</strong>
<div class="shorter wider" style="background-image: url('https://picsum.photos/200/300');"></div>
<strong>Shorter & Narrower</strong>
<div class="shorter narrower" style="background-image: url('https://picsum.photos/200/300');"></div>
【问题讨论】:
-
不将背景宽度设置为 100% 并将位置设置为底部有效吗?它不会剪裁任何东西,背景将始终填充 100% 的宽度,并且始终位于底部
-
不,如果容器较短,它会从顶部剪掉 - 我想保持从顶部的偏移量并让它从底部剪掉,如我的第一个示例所示。跨度>
-
但是如果我不断降低浏览器的高度,那最终不会发生吗?另外,请添加您当前的代码以重现此问题
-
不,它会从底部剪掉——没关系。如果
top是background-position,它将始终显示图像的顶部,这就是我想要的(这是偏移量的基础)。我已经添加了这个问题的说明。 -
请添加您的代码
标签: css background-image background-position background-size