How to center a dropdown with Javascript?

Asked

Viewed 86 times

-1

I need to create a drop-down menu with the following styling:

inserir a descrição da imagem aqui

As you can see, it must be aligned with the 'Arrow' pointing to the center of the parent element. I tried numerous styling combinations, but nothing worked. Either it is aligned to the left of the element, or to the right. I can not in any way align to the center. I am posting here only the most important parts of the code to facilitate understanding. Follow:

HTML made it like this:

<li class="user-actions-menu-item dropdown-trigger" id="admin-dropdown-trigger">
    <a href="#">
        <i class="icon-help-circled"></i>
    </a>

    <ul class="user-actions-dropdown">
        <li class="user-actions-dropdown-item">
            <a href="#">
                <i class="icon-user"></i>
                Conta
            </a>
        </li>

        <li class="user-actions-dropdown-item">
            <a href="#">
                <i class="icon-info-circled"></i>
                Notificações
            </a>
        </li>
    <ul>
</li>

CSS (Using SASS):

.dropdown-trigger {
    position: relative;
}

.user-actions-dropdown {
        display: block;
        position: absolute;
        width: 180px;
        height: auto;
        background-color: $light-blue;
        z-index: 12;
        list-style: none;
        padding: 0;
        font-size: 1em;
        margin: auto;
        @include bordered(1px, solid, $light-gray);
        margin-top: 15px;
        left: 0;
        right: 0%;
}

I searched for a few hours on the internet for a solution, and everything indicates only with Javascript I can achieve this result. Would anyone know how to do this with or without Javascript?

  • Create an example on Jsfiddle that produces the problem. It is complicated to have to process SASS to see how the result will look and then answer.

1 answer

1


You can set a fixed width on ul.user-actions-dropdown and then position via CSS with position, left and margin-left.

Something like this: http://codepen.io/anon/pen/pvVxNd

There is also another way, which is by using the Transform property.

Ex: -Webkit-Transform: translateX(-50%);

  • Interesting your solution... It seems half gambiarra... but it worked very well!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.