2010-10-09 85 views
1

我想将一个div放在另一个div上。如何将div放在其他div上

CSS:

.fresh_bnr_img { 
    float:left; 
    width:950px; 
    text-align:center; 
} 

.black_strip1 { 
    position:relative; 
    top:20px; 
    width:120px; 
    height:50px; 
    background:#000; 
    opacity:0.5; 
    z-index:999; 
} 

HTML:

<div class="fresh_bnr_img"> 
    <div class="black_strip1">Home</div> 
    <img src="images/banner-2.jpg" width="946" height="295" alt="fresh-banner" /> 
</div> 

的问题是,当我放置black_strip1类在内的div,它出现在正确的地方,但一个空白空间出庭下一个分区。

回答

3

给包含div position:relative;和孩子DIV position:absolute;

孩子的div将会定位相对于父DIV。 (从父div的左上角开始)

例如,

.fresh_bnr_img{ 
position:relative; 
float:left; 
width:950px; 
text-align:center; 
} 


.black_strip1{ 
position:absolute; 
top:20px; /* 20px from the top edge of .fresh_bnr_img*/ 
left:0px; /* 0px from the left edge of .fresh_bnr_img*/ 
width:120px; 
height:50px; 
background:#000; 
opacity:0.5; 
z-index:999; 
} 
+0

非常感谢莫因... – 2010-10-11 04:10:29