简体   繁体   English

获取背景图像或文本以填充宽度,不重复,但不垂直拉伸

[英]get background image or text to fill width, not repeat, but not stretch vertically

Just as the title says. 正如标题所说。 How can I get a background image, or even text to fill width wise, not stretch/squash vertically, and just show up once? 如何获得背景图像,甚至是文本来沿宽度方向填充宽度,而不是垂直拉伸/挤压,仅显示一次? I can't seem to figure this out. 我似乎无法弄清楚。 Thank you. 谢谢。

Edit: I also want the height to scale with the width of the picture proportionally, so it will not be skewed but scaled with the width. 编辑:我也希望高度与图片的宽度成比例地缩放,因此它不会偏斜,而是与宽度成比例。

If you only want the background img to fill the width without taking into account the height you can do: 如果仅希望背景img填充宽度而不考虑高度,则可以执行以下操作:

.element-with-back-img{
    background-repeat: no-repeat;
    background-size: 100%;
}

but with this the img doesn't fill the height of element-with-back-img because the height will set to "auto" 但与此同时,img不会填充element-with-back-img的高度,因为该高度将设置为“自动”

By doing this: 通过做这个:

.element-with-back-img{
    background-repeat: no-repeat;
    background-size: 100% 100%;
}

the img fills the height but if element-with-back-img has different proportions than those from the img, this one will change its proportions to fill the element-with-back-img img填充高度,但如果element-with-back-img与img的比例不同,则此比例将更改其比例以填充element-with-back-img

I recommend u to: 我建议您:

.element-with-back-img{
    background-repeat: no-repeat;
    background-size: cover;
}

this scale the background image to be as large as possible so that the background area is completely covered by the background image. 这样会将背景图像缩放到尽可能大的程度,以使背景区域完全被背景图像覆盖。 Some parts of the background image may not be in view within the background positioning area 在背景定位区域内可能看不到背景图像的某些部分

I hope this could help u 我希望这可以帮助你

 .test { background-image: url(http://placekitten.com/1000/750); background-size: 100%; background-repeat: no-repeat; background-color: lightblue; height: 150px; display: inline-block; } #test1 { width: 200px; } #test2 { width: 100px; } #test3 { width: 300px; } .test:hover { background-position: center center; } 
 <div class="test" id="test1"></div> <div class="test" id="test2"></div> <div class="test" id="test3"></div> 

Use css for the body: 使用CSS作为主体:

body {background-repeat: no-repeat;
      background-size: 100%;}

To fill width-wise, use 'background-size'. 要沿宽度方向填充,请使用“背景尺寸”。 That allows you to enter two parameters - width and height, in that order. 这样您就可以按此顺序输入两个参数-宽度和高度。 Use '100%' to fill the width, then a fixed value for the height (assuming you know what the height of your image is). 使用“ 100%”填充宽度,然后使用固定的高度值(假设您知道图像的高度是多少)。

Eg if your background image is 500px tall, then: 例如,如果您的背景图片高为500px,则:

{
    background-size:100% 500px;
    background-repeat: no-repeat;
}

You can experiment for yourself here . 您可以在这里为自己做实验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM