首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >全屏显示背景图像

问全屏显示背景图像
EN

Stack Overflow用户
提问于 2019-06-16 21:13:00
回答 2查看 460关注 0票数 0

我正在设置一个网站,并希望创建一个响应式的背景图像,但我只能得到单线与背景图像。我应该使用什么属性来使整个背景图像适合?

我创建了两个文件,index.html:

代码语言:javascript
复制
<div id="logo">Test</div>

style.css:

代码语言:javascript
复制
#logo {
background-image: url("bg.png");
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}

我尝试了很多次,但只有一行得到了背景图像。我希望背景图像完全显示在屏幕上,而不使用高度属性,我认为这使网站的响应性较差。

EN

回答 2

Stack Overflow用户

发布于 2019-06-16 21:22:31

如果您希望图像具有与屏幕相同的高度,则可以使用height:100vh。但是如果你只想要全尺寸的图片,你可以这样做:

​

代码语言:javascript
复制
html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}

#image{
max-height: 100%;
    max-width: 100%;
}

#text{
  position: absolute;
  color: white;
  top: 0
}
代码语言:javascript
复制
<img id="image" src="https://images.unsplash.com/photo-1528920304568-7aa06b3dda8b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80">
<div id="text">test</div>

​

票数 0
EN

Stack Overflow用户

发布于 2019-06-16 21:27:13

您应该更改徽标div的高度,并通过添加高度将其设置为全高:主体和徽标div的高度为100%,您的代码应如下所示

代码语言:javascript
复制
html, body
{
   height: 100%;
}
#logo {
   background-image: url("bg.png");
   background-position: center center;
   background-repeat: no-repeat;
   background-attachment: fixed;
   background-size: cover;
   height:100%;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56619160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档