简体   繁体   English

如何使背景图像响应

[英]How to make the background image responsive

I admit in the beginning that I am new to UI development and started learning recently. 我从一开始就承认我是UI开发的新手,最近开始学习。 I am developing a website which i similar to InuitLabs.com . 我正在开发一个类似于InuitLabs.com的网站。 When I looked at the source code using view source I am totally lost. 当我使用视图源查看源代码时,我完全迷失了。 Particularly I am interested in knowing 我特别想知道

  1. How the slider image on the homepage is responsive? 主页上的滑块图像如何响应? Is it through javascript or using pure css. 是通过javascript还是使用纯CSS。
  2. Also I want to know the text moves upwards on scrolling leaving behind the background image intact? 我还想知道文本在滚动时会向上移动,而完整保留背景图像吗? How to achieve the same effect. 如何达到同样的效果。

I know this might be the basic question but I found it hard to know through the source code as there are many javascript and css files. 我知道这可能是基本问题,但是由于存在许多JavaScript和CSS文件,因此我很难通过源代码来了解它。

Regards, Pradeep 问候,Pradeep

Take a look at the background-size property. 看一下background-size属性。 you can set background-size to any px or % value or use constants: cover will adjust the image size to fill the entire container while contain try to fit the image inside the container without cropping it, most likely leaving some parts of the container without any background. 您可以将background-size设置为任何px或%值,也可以使用常量: Cover会调整图像大小以填充整个容器,同时包含尝试使图像适合容器内部而不会裁剪的情况,很可能使容器的某些部分没有任何背景。

What you probably want is to set your background-size property to cover. 您可能想要的是将background-size属性设置为cover。

you just set the image to the percent you want in % through css for example: 您只需通过CSS将图像设置为所需的百分比,例如:

.slider img {
   width:100%;
}

edit: also you need to specify the height as auto, if you do not want to lose the image ratio. 编辑:如果您不想丢失图像比例,还需要将高度指定为自动。 if you set width and height at 100% the image ratio will be messed up. 如果将宽度和高度设置为100%,则图像比例会混乱。

if you want to set responsive height also for longer device then use width:100%; 如果您还要为更长的设备设置响应高度,则使用width:100%; and height:100%; 高度:100%; other wise you can use height:auto; 否则,您可以使用height:auto; make div and keep it background-size property for it. 使div并为其保留background-size属性。

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

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