Search code examples
cssdrop-down-menualignment

CSS drop down alignment


I am trying to create a dropdown menu that when you click it will open up a ul. The thing is I want the "View More" button to always be exactly where it is and the dropdown to open up centered below it.

if you go to the link below you will see what i mean. it opens up exactly like I want it to, but if you close the dropdown by clicking "View More" you will see the header move to the left when i want it to stay exactly where it is

I am building this to be able to be used in many many different locations so "hardcoding" sizes is not an option.

Please help! :)

HTML

<div id="testcontainer">

        <a href="javascript:;" class="dropdown-activator" dropdownContent="#dropdown-content-340">
            <span>View More!</span>
        </a>

        <ul class="dropdown-content" id="dropdown-content-340">
            <li><a href="http://www.google.com" class="googleIcon">Google</a></li>
            <li><a href="http://www.yahoo.com" class="yahooIcon">Yahoo</a></li>
            <li><a href="http://www.bing.com" class="bingIcon">Bing</a></li>
        </ul>


    </div>

    <div id="testcontainer2">

        <a href="javascript:;" class="dropdown-activator" dropdownContent="#dropdown-content-350">
            <span>View More!</span>
        </a>

        <ul class="dropdown-content" id="dropdown-content-350">
            <li><a href="http://www.google.com" class="googleIcon">Google</a></li>
            <li><a href="http://www.yahoo.com" class="yahooIcon">THIS IS A TEST FOR WIDER SHIT</a></li>
            <li><a href="http://www.bing.com" class="bingIcon">Bing</a></li>
        </ul>

    </div>

    <div id="testcontainer3">

        <a href="javascript:;" class="dropdown-activator" dropdownContent="#dropdown-content-400">
            <span>View More!</span>
        </a>

        <ul class="dropdown-content" id="dropdown-container-400">
            <li><a href="http://www.google.com" class="googleIcon">GOOOOOOOOOOOOOOOOOOOOOGLEASDASDSASD</a></li>
            <li><a href="http://www.yahoo.com" class="yahooIcon">Yahoo</a></li>
            <li><a href="http://www.bing.com" class="bingIcon">Bing</a></li>
        </ul>

    </div>

CSS

#testcontainer {
position: absolute;
top: 20px;
left: 100px;
text-align: center;
}

#testcontainer2 {
position: absolute;
top: 20px;
left: 150px;
text-align: center;
}

#testcontainer3 {
position: absolute;
top: 200px;
left: 500px;
text-align: center;
}

.dropdown-activator-active {
background-color: #000;
color: #fff;
}

.dropdown a {
display: inline-block;
}

.dropdown-activator {
display: inline-block;
border: 1px solid black;
padding: 3px;
}


.dropdown-content {
visibility: hidden;
height: 0;
opacity: 0;
position: relative;
text-align: left;
margin: 0 auto;
border: 1px solid black;
}

.dropdown-content-active {
visibility: visible;
height: auto;
opacity: 1;
}

.dropdown-content ul li {
list-style: none;
}

JQuery

$(function(){

$(".dropdown-activator").click(function() {
    $this = $(this);
    var current = $this.attr('dropdownContent');
    if (!$(current).hasClass('dropdown-content-active')) {
        $this.addClass("dropdown-activator-active", 100);
        $(current).addClass('dropdown-content-active', 100, function() {
            $('html').unbind('click');
            $('html').not($this).one('click', function() {
                $(current).prev().removeClass("dropdown-activator-active", 100);
                $(current).removeClass('dropdown-content-active', 100);
            });
        });
    } else {
        $this.removeClass("dropdown-activator-active", 100);
        $(current).removeClass('dropdown-content-active', 100)
    }
});
});

you can see an example of it here www.chrisworrell.com (temporary)


Solution

  • display: none; removes your element from the document flow, which is why your parent div resizes. On the other hand, visibility: hidden; hides your element while keeping it in the document flow.

    What you could do is instead of manipulating the display property of your <ul> element, set visibility: hidden; and height: 0; That way the unexpanded link will continue to use the width of the UL element it is bundled with.

    It's kind of hacky, but should get the job done.

    In order to manipulate visibility instead of display none with jQuery UI, use .animate({opacity: 1}) for fadeIn() and .animate({opacity: 0}) for fadeOut()