CSS:如何将背景图像带到前面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20716793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS: How to bring background image to the front?
提问by orschiro
I was already looking at CSS: How to make background-image above background-color in a listand tried the !important
attribute but without success.
我已经在看CSS: How to make background-image above background-color in a list并尝试了该!important
属性但没有成功。
I have thisexample page where I would like to bring the yellow book cover which I use as a background image in front of the navigation bar below.
我有这个示例页面,我想将黄书封面用作下面导航栏前面的背景图像。
The code I am using is the following:
我使用的代码如下:
.home-link {
background: url(http://www.mensch-jinn-dajjal.net/wp-content/uploads/2013/12/Buchtitel.gif) no-repeat !important;
background-size: 180px 200px !important;
background-position: 400px 80px !important;
z-index: 99999 !important;
}
Any idea why it is not coming to the front?
知道为什么它没有出现在前面吗?
I also tried to assign the navbar a negative z-index without success.
我还尝试为导航栏分配负 z-index,但没有成功。
采纳答案by Manish Mishra
z-index
won't work in this case; z-index is meant for those elements which are stacked one over another i.e. they have overlapping top and left, which is not in your case.
Html is working the way it should i.e. flow layout,
z-index
在这种情况下不起作用;z-index 适用于那些堆叠在一起的元素,即它们的顶部和左侧重叠,这不是你的情况。Html 以它应该的方式工作,即流布局,
this is your markup outhere on your site :
这是您网站上的标记:
<header id="masthead" class="site-header" role="banner">
<a class="home-link" href="...</a> <-- Link containing the image-->
<div id="navbar" class="navbar"> <!-- navigation bar -->
</div><!-- #navbar -->
</header>
clearly, your anchor and navigation bar are placed one after another, they are rendered exactly like that.
显然,您的锚点和导航栏是一个接一个放置的,它们的呈现方式完全一样。
So, if you want to overlap your image, you need to make your image position:absolute
. I would recommend, create a independent img
tag and make it position absolute and place it exactly where you want, with relative top and left, so that it adjusts itself according to the resolution.
所以,如果你想重叠你的图像,你需要制作你的图像 position:absolute
。我会建议,创建一个独立的img
标签并使其绝对定位并将其放置在您想要的位置,相对顶部和左侧,以便它根据分辨率进行自我调整。
see this simple illustration of z-index :jfiddle
请参阅 z-index 的这个简单说明:jfiddle
in this, we have four float:left, position:absolute
divisions, by-default, when no z-index is given to any of them, the one that was present in the last in the flow, was the visible color, but as you give higher z-index to anyone of them(first one in example), it becomes the visible color
在这里,我们有四个float:left, position:absolute
分区,默认情况下,当没有给任何一个 z-index 时,流中最后一个出现的那个是可见的颜色,但是当你给更高的 z-index 时他们中的任何一个(示例中的第一个),它成为可见颜色
回答by Kirk
Edited the old post
编辑了旧帖子
Just change these properties then your good to go. since you provided very small piece of code. it was hard to figure and explain. but do these following changes. this will get you what you want. and you have to do some alignments re-touch again
只需更改这些属性即可。因为您提供了非常小的代码。很难想象和解释。但做这些以下更改。这会让你得到你想要的。你必须做一些对齐重新触摸
.site-header {
background: url(http://www.mensch-jinn-dajjal.net/wp- content/uploads/2013/12/header-without-book-1600x230.jpg) no-repeat scroll top;
background-size: 1600px auto;
z-index: 1; //updated here
}
.navbar {
background-color: #DD2A0F;
margin-bottom: 10px;
padding-right: 80px;
position: absolute; //updated here
top: 200px; //updated here
z-index: -2; //updated here
}
.site-header .home-link {
color: #141412;
display: block;
margin: 0 auto;
max-width: 1080px;
min-height: 280px; //updated here
padding: 0 20px;
text-decoration: none;
width: 100%;
}
The Snapshot is here.
快照在这里。
回答by Lorenz Meyer
The problem lies not where you think. In fact a.home-link
is really on the front, but it does not overlap the navbar. And you really dont want it to, because you could no longer use the navbar
to navigate when the menu buttons are situated under the link a.home-link
问题不在于你的想法。其实a.home-link
真的是在前面,只是没有和导航栏重叠。而且您真的不希望它,因为navbar
当菜单按钮位于链接下方时,您无法再使用进行导航a.home-link
In order to achieve what you liked to, you need to separate your a.home-link
in two:
为了实现您喜欢的目标,您需要将您的目标一a.home-link
分为二:
- Have one
a
element represent the link with the text - Have a second
a
element represent the book cover
- 让一个
a
元素代表与文本的链接 - 有第二个
a
元素代表书籍封面
Just general remarks about your css-coding style:
只是关于你的 css 编码风格的一般评论:
- Use
!important
only in exceptional cases.!important
should not be a way to invalidate basic css inheritance principles. Conceive your document structure with styling in mind, and you'll (almost) never need!important
any more. Don't use!ipmortant
often, because (1) you cannot override it, (2) it leads to unmaintanable css code. - Use well chosen z-index values: why use
99999
if2
is already bigger than1
? And if you want later something before99999
, will you use999999
?
- 使用
!important
只有在特殊情况下。!important
不应该是使基本 css 继承原则无效的方法。构思您的文档结构时要考虑到样式,您(几乎)将不再需要!important
。不要!ipmortant
经常使用,因为(1)你不能覆盖它,(2)它会导致无法维护的 css 代码。 - 使用精心挑选的 z-index 值:为什么使用
99999
if2
已经大于1
?如果你想要以后的东西99999
,你会使用999999
吗?
回答by Trevan Hetzel
Stacking order of elements in CSS is controlled by the position
property and z-index
property. From looking at your code, you do not have a position
property declared either on the .home-link
div or the navbar below it.
CSS中元素的堆叠顺序由position
属性和z-index
属性控制。从查看您的代码来看,您没有position
在.home-link
div 或其下方的导航栏上声明属性。
I would try adding position: relative
to .home-link
for starters. Then you might have to play around with the positioning and z-index
of the navbar.
我会尝试添加position: relative
到.home-link
初学者。然后您可能需要调整定位和z-index
导航栏。