如何使div重叠其他div?

时间:2012-04-10 13:15:30

标签: html css

搜索栏下方的内容是在用户输入一些文字后显示的。目前风格已达到我的目标。

然而,当我显示搜索结果时,它会按照搜索栏推送容器,如我的图片所示:

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(用LESS 编写):

  #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;
                            }
                        }
                    }
                }                   
            }
        }
    }

2 个答案:

答案 0 :(得分:8)

使用CSS的Absolute Positioning。与相对定位不同,绝对定位会从文档流中移除项目(即防止其他项目向下移动。)

请记住,绝对定位的东西是相对于它最近定位的父级定位的 - 所以绝对定位项目所在的容器(在您的情况下)应该设置为position:relative;

有关各种定位的信息:http://www.w3schools.com/css/css_positioning.asp

答案 1 :(得分:6)

position:absolute提供给.item DIV。写得像这样:

 .item {
 position:absolute;
}
相关问题