2014-10-01 72 views
0

我有這樣的jsfiddle:Click here如何對齊div中的2個div?

<div id="header-parent"> 
    <div id=header>  
      <div id=header-title><img src="${pageContext.request.contextPath}/icon/pizza2.png"><span>this is a title</span></div> 
      <div id=header-cart><div id="cart-title"> 
            <div><span>0</span></div> 
            <div><span>pizza cart</span></div> 
           </div> 
           <div id="cart-icon"><img src="${pageContext.request.contextPath}/icon/shopping-cart.PNG"></div> 
      </div> 
    </div> 
</div> 

如果你去到該頁面你會看到右邊的div實際上是在頭本身下對齊。

我怎樣才能設置正確的div在標題內?

回答

2

A float總是漂浮在下一個元素上方。因此,在cart-title之前放置header-cart

Updated JsFiddle

觀測值:我沒有固定的小提琴,還有你必須做出一些重大調整。你沒有引用的ID,除其他事項外...

+1

不要忘記清除你的花車:) – 2014-10-01 22:57:56

0

http://jsfiddle.net/usnmLuy5/3/

使用顯示:塊直列保持塊使用100%的寬度。使用購物車上的浮動物,並設置標題的高度,以便購物車適合。拼寫檢查高度,CSS中有一個高度被忽略,不需要。

#header-parent { 
    padding-top:5px; 
    padding-bottom:5px; 
    border-bottom: 1px solid White; 
    background: #45484d; /* Old browsers */ 
    background: -moz-linear-gradient(top, #45484d 43%, #000000 100%); /* FF3.6+ */ 
    background: -webkit-gradient(linear, left top, left bottom, color-stop(43%,#45484d), color-stop(100%,#000000)); /* Chrome,Safari4+ */ 
    background: -webkit-linear-gradient(top, #45484d 43%,#000000 100%); /* Chrome10+,Safari5.1+ */ 
    background: -o-linear-gradient(top, #45484d 43%,#000000 100%); /* Opera 11.10+ */ 
    background: -ms-linear-gradient(top, #45484d 43%,#000000 100%); /* IE10+ */ 
    background: linear-gradient(to bottom, #45484d 43%,#000000 100%); /* W3C */ 
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#45484d', endColorstr='#000000',GradientType=0); /* IE6-9 */ 
} 

#header { 
    position: relative; 
    margin-left: auto; 
    margin-right: auto; 
    cursor: pointer; 
    height: 36px; /* same as cart */ 
} 

#header-child{ 
    padding:5px 0 5px 0; 
    display:inline-block; 
} 
#header-title { 
    display:inline-block; 
} 
#header-title img{ 
    width: 25px; 
    heigth: 25px; 
    margin-right:5px; 
    vertical-align: middle; 
} 

#header-cart{ 
    display: inline-block; 
    text-align:center; 
    background-color:red; 
    float:right; 
    color:white; 
    font-size: 15px; 
} 

#header-cart img{ 
    width: 30px; 
    heigth: 30px; 
} 

#header-cart #cart-icon{ 
    float:left; 
} 
#header-cart #cart-title{ 
    float:left; 
} 

#header-cart #header-title{ 
    display: inline-block; 
    background-color:blue; 
} 

#header-title span{ 

    color: white; 
    font-family: Georgia, Times, "Times New Roman", serif; 
    font-size:20px; 
    margin-top: 0; 
    margin-bottom: 0; 
    vertical-align: middle; 
}