Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a div overlap other divs?

Tags:

html

css

The contents below the search bar are meant to be shown after the users enter some text. Currently the style is down to what I'm aiming for.

However when I display the search results, it pushes the container following the search bar, as illustrated by my picture:

enter image description here

What can I do that the search results display and just overlap everything below it without pushing other elements downwards?

Here is my 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>

And my CSS (written with 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;
                            }
                        }
                    }
                }                   
            }
        }
    }
like image 388
Only Bolivian Here Avatar asked Apr 10 '12 13:04

Only Bolivian Here


People also ask

Can two divs overlap?

Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design.

Why div is overlap with another div?

It is actually to do with the way you have managed your float attributes. Your div representantes floats left, but the footer does not. You can test this by turning float:left off from the representantes div. This is a common cause of divs overlapping.

How do I put 3 divs next to each other?

Three or more different div can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side.


2 Answers

Use CSS's Absolute Positioning. Unlike Relative Positioning, Absolute Positioning removes the item from the flow of the document (ie keeping it from pushing other things down.)

Just remember, something that's absolutely positioned is positioned relative to it's nearest positioned parent - so whatever container the absolute positioned items are in (in your case) should be set to position:relative;

Info on all kinds of positioning: http://www.w3schools.com/css/css_positioning.asp

like image 144
Dave Avatar answered Oct 09 '22 09:10

Dave


Give position:absolute to your .item DIV. Write like this:

 .item {
 position:absolute;
}
like image 22
sandeep Avatar answered Oct 09 '22 09:10

sandeep