如何使一个 div 覆盖其他的 div?

5
搜索栏下方的内容是为了在用户输入文本后显示。目前的样式符合我的要求。但是,当我显示搜索结果时,它会推动跟随搜索栏的容器,如下图所示:
[图片链接]
我该怎么做才能使搜索结果显示,并将其与下面的所有内容重叠,而不会向下推其他元素?
这是我的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>

我使用LESS编写的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;
                            }
                        }
                    }
                }                   
            }
        }
    }
2个回答

8

使用CSS的绝对定位(Absolute Positioning)。与相对定位(Relative Positioning)不同,绝对定位将元素从文档流中移除(即避免了它推动其他元素下移的影响)。

请记住,绝对定位的元素是相对于其最近的已定位父元素定位的 - 因此,包含绝对定位项的容器(在您的情况下)应设置为position:relative;

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


6

给你的 .item DIV 添加 position:absolute 样式。写成这样:

 .item {
 position:absolute;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接