2012-04-10 118 views
5

搜索欄下方的內容旨在在用戶輸入一些文本後顯示。目前,這種風格取決於我的目標。如何使div與其他divs重疊?

然而,當我將搜索結果顯示,它推動以下搜索欄的容器,通過我的圖片所示:

enter image description here

我能做些什麼,搜索結果顯示,只是重疊下的所有內容它沒有向下推其他元素?

這裏是我的HTML:

<div id="search-bar" class="box"> 
    <h1 class="horizontal-header">SEARCH THE DATABASE</h1> 
    <div id="search-wrapper"> 
     <input name="query" id="name" class="big-search" placeholder="champion, item, spells..." /> 
     <div id="search-results"> 
      <a href="#"> 
       <div class="item"> 
        <img src="http://images4.wikia.nocookie.net/__cb20091218194710/leagueoflegends/images/0/0f/JaxSquare.png" alt="" /> 
        <div class="info"> 
         <p class="name">Jax</p> 
         <p class="description">Champion</p> 
        </div> 
       </div> 
      </a> 
      <a href="#"> 
       <div class="item"> 
        <img src="http://images4.wikia.nocookie.net/__cb20091218194710/leagueoflegends/images/0/0f/JaxSquare.png" alt="" /> 
        <div class="info"> 
         <p class="name">Jax</p> 
         <p class="description">Champion</p> 
        </div> 
       </div> 
      </a> 
      <a href="#"> 
       <div class="item"> 
        <img src="http://images4.wikia.nocookie.net/__cb20091218194710/leagueoflegends/images/0/0f/JaxSquare.png" alt="" /> 
        <div class="info"> 
         <p class="name">Jax</p> 
         <p class="description">Champion</p> 
        </div> 
       </div> 
      </a> 
     </div> 
    </div> 
</div> 

我的CSS(用更少的書面):

#search-bar { 
     width: 636px; 
     height: 35px; 

     #search-wrapper { 
      float:left; 
      margin-left: 13px; 

      #search-results { 
       z-index:999; 
       position:relative; 


       a { 
        display:block; 

        .item:hover { 
         background-color:#282828; 
        } 

        .item { 
         overflow: hidden; 
         background-color: #171717; 
         padding: 2px; 
         cursor:pointer; 
         margin-bottom:1px; 

         img { 
          float: left; 
          width: 35px; 
         } 

         .info { 
          float: left; 
          margin-left: 8px; 

          .name { 
           color: white; 
           margin: 0; 
          } 

          .description { 
           color: white; 
           margin: 0; 
          } 
         } 
        } 
       }     
      } 
     } 
    } 

回答

8

使用CSS的Absolute Positioning。與「相對定位」不同,「絕對定位」將文檔中的項目從文檔流中移除(即防止其將文檔推向其他東西)。

請記住,絕對定位的東西相對於它最近的定位父 - 絕對定位的項目是(你的情況)應設置爲position:relative;

信息對各種定位:http://www.w3schools.com/css/css_positioning.asp

6

position:absolute.item DIV。這樣寫:

.item { 
position:absolute; 
} 
+0

這工作,謝謝! :) – 2012-04-10 13:22:27