简体   繁体   English

响应式CSS背景图片问题

[英]Responsive CSS Background Image Issue

I used foundation 5 framework for designing a page. 我使用Foundation 5框架设计页面。 It has a logo placed on the top of the background image. 它的徽标位于背景图片的顶部。 Link: Demo Page 链接: 演示页

The styles for those two images are: 这两个图像的样式为:

header {
    position: relative;
}
.logo {
    background: url("../img/text2save.png");
    background-position: left top;
    background-repeat: no-repeat;
    background-size: contain;
    position: absolute;
    z-index: 2;
    width: 20%;
    height: 80px;
}
header div {
    background: url("../img/banner.jpg");
    background-repeat: no-repeat;
    height: 300px;
    background-size: 100%;

}

Problem: When the browser is resized down the background image leaves blank space below it ie, the other element moves down. 问题:缩小浏览器大小时,背景图像在其下方留有空白,即其他元素向下移动。 I couldn't figure out the solution. 我不知道解决方案。

The Site: http://text2save.tushark.com.np 站点: http//text2save.tushark.com.np

The reason why there is extra space is because you have statically set the header height to 300px. 之所以会有额外的空间,是因为您已将标题高度静态设置为300px。 The background image, meanwhile, is scaling when you resize the page, since it's size is 100% width of the page, which changes when you make the window smaller/larger. 同时,背景图像在调整页面大小时正在缩放,因为它的大小是页面宽度的100%,当您缩小/放大窗口时,背景图像会改变。

Either make the height relative to the background image size or make the background-image size static. 设置相对于背景图像尺寸的高度或将背景图像尺寸设为静态。

the problem is because you are using static height on your div 问题是因为您在div上使用了静态高度

the easy way to not show the blank space (bad): 不显示空格(坏)的简单方法:

background-size: auto 100%;

Using media query (best): 使用媒体查询(最佳):

@media (max-width:500px) {
    header div{
    height : 150px;
  }
}

And a better solution : 更好的解决方案:

Dont use text on your image , best write on the html, ever will show it. 请勿在图片上使用文字,最好在html上书写,以免显示文字。

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

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