简体   繁体   English

没有显示背景图片的图片

[英]Image with background image not showing

Look at the following JSFiddle 看下面的JSFiddle

 .container { width: 100%; background-color: yellow; } .classic { display: block; position: relative; margin-left: auto; margin-right: auto; background-position: center center; background-image: url("http://www.pulsarwallpapers.com/data/media/3/Alien%20Ink%202560X1600%20Abstract%20Background.jpg"); background-size: contain; background-repeat: no-repeat; } .classic-img { display: block; position: absolute; overflow: hidden; width: 100%; height: 100%; top: 0%; left: 0%; } .classic-img img { position: absolute; width: 100%; height: 100%; top: 0; left: 0; } .top-menu { width: 100%; height: 50px; position: relative; background-color: red; } .top-menu-buttons { position: absolute; margin-left: auto; margin-right: auto; bottom: 0; left: 0; right: 0; width: 50%; text-align: center; } .top-menu-buttons .button { display: inline-block; vertical-align: middle; font-size: 25px; color: white; } .top-menu-buttons span { display: inline-block; vertical-align: middle; font-size: 25px; color: white; } .bottom-menu { width: 100%; height: 50px; position: relative; background-color: green; } .bottom-menu-buttons { position: absolute; margin-left: auto; margin-right: auto; bottom: 0; left: 0; right: 0; width: 80%; text-align: center; } .bottom-menu-buttons .button { display: inline-block; vertical-align: middle; font-size: 35px; color: white; padding-left: 10px; padding-right: 10px; } 
 <div class="list"> <a class="item"> <div id="container" class="container"> <div class="top-menu"> <div class="top-menu-buttons"> <button>-</button> <span>20</span> <button>+</button> </div> </div> <div id="classic" class="classic"> <div id="classic-img" class="classic-img"> <img src="http://bc03.rp-online.de/polopoly_fs/1.4643662.1415087612!httpImage/2100575733.JPG_gen/derivatives/d540x303/2100575733.JPG" /> </div> </div> <div class="bottom-menu"> <div class="bottom-menu-buttons"> <button class="button button-icon ion-eye">1</button> <button class="button button-icon ion-refresh">2</button> <button class="button button-icon ion-crop">3</button> <button class="button button-icon ion-android-options">4</button> <button class="button button-icon ion-social-tumblr">5</button> </div> </div> </a> </div> 

CSS: CSS:

What i want to achieve is something like the following: 我要实现的目标如下:

在此处输入图片说明

The red area should be a top menu. 红色区域应该是顶部菜单。 It should not be a fixed top position it should just always be on top of the image. 它不应位于固定的顶部位置,而应始终位于图像的顶部。

As you can see the image has a white background and a black forground. 如您所见,图像具有白色背景和黑色前景。 It should look like a polaroid. 它看起来应该像宝丽来。

The green area should be a menu at the bottom but also not fixed to the bottom it should just always be underneath the image. 绿色区域应该是底部的菜单,但也不应固定在底部,它应该始终位于图像下方。 If there is not enough space it should simply scroll not clinch or esize any of the divs. 如果没有足够的空间,则应简单滚动而不压紧或调整任何div。 I guess the main problem is the div in the middle where the image with the background image is. 我猜主要的问题是图像与背景图像位于中间的div。

I try for ages now to get the correct css but unfortunately im very unexperienced and all i can do at the moment is try and error but i cant get it working. 我现在尝试了很长时间才能获得正确的CSS,但是不幸的是,我非常没有经验,我现在只能做的是尝试和出错,但是我无法使它正常工作。

remove position: absolute from .classic-img and .classic-img img 删除position: absolute .classic-img.classic-img img position: absolute

add margin: 100px auto; 增加margin: 100px auto; adjust 100px as per your need, also you have set background-size: contain and the aspect ratio of bg image is almost equal to the image, therefore you would see only small portion of bg image here in the fiddle - jsfiddle.net/18vg13dt/3 根据需要调整100px ,还设置了background-size:contain,并且bg图像的长宽比几乎等于该图像,因此在小提琴中您只会看到bg图像的一小部分-jsfiddle.net/18vg13dt / 3

additionally if you want gap from left and right also the use - margin: 100px 50px; 另外,如果您还希望左右之间留有空隙,请使用margin: 100px 50px; similarly according to your needs. 同样根据您的需求。

jsfiddle 的jsfiddle

I guess this is what you want? 我想就是你想要的?

.classic {
    background: url("http://www.pulsarwallpapers.com/data/media/3/Alien%20Ink%202560X1600%20Abstract%20Background.jpg")no-repeat center center;
    background-repeat: no-repeat;
    background-size: cover;
}

.classic-img {
    padding: 10px;
    box-sizing: border-box;
}

.classic-img img {
    display:block;
    width:100%;
    height:100%;
}

You need to set the height for the class .classic-img 您需要设置类.classic-img的高度

Try this : 尝试这个 :

.classic-img {
display: block;
position: relative; /*---absolute to relative*/
overflow: hidden;
width: 100%;
height: 100px; /*----Adjust the height---*/
top: 0%;
left: 0%;
}

If you still face the issue, do come back and let us know. 如果您仍然遇到问题,请回来告诉我们。

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

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