2017-02-21 59 views
0

因此,可以说,我有一些文件CSS力格与绝对位置落后格与静态位置

 #a{width: 10px; height: 10px; background: red; z-index: 10;} 
 
     #b{width: 100%; height: 100%; background: black; z-index: 5; position: absolute;}
<body> 
 
     <div id="a">foo</div> 
 
     <div id="b">bar</div> 
 
    <body>

#b div覆盖#a,因为#babsolute位置。

我怎样才能#b#a而不改变#a位置?

回答

3

您可以将position: relative添加到#a元素。

+0

是的,我在发布问题后发现它,我的坏)谢谢 – Coffee

+1

#a {width:10px; height:10px;背景:红色; z-index:10;位置:相对} –

2

你必须设置比static一个其他position你的第一个div申请喜欢z-index

#a { 
 
    width: 100px; 
 
    height: 100px; 
 
    background: red; 
 
    z-index: 10; 
 
    position: relative; 
 
} 
 

 
#b { 
 
    width: 100%; 
 
    height: 100%; 
 
    background: black; 
 
    z-index: 5; 
 
    position: absolute; 
 
    left:0; 
 
    top:0; 
 
}
<div id="a">foo</div> 
 
<div id="b">bar</div>

2

样式您应该添加一个相对位置,你的第一个div:

<body> 
    <div id="a">foo</div> 
    <div id="b">bar</div> 
<body> 
<style> 
    #a{width: 10px; height: 10px; background: red; z-index: 10; position:relative} 
    #b{width: 100%; height: 100%; background: black; z-index: 5; position: absolute;} 
</style>