skip to Main Content

I have drop-down menu, I do not want to change it width min-width: 200px;. But I want to add padding from the right.

what i want to do

Also I can’t understand how to make it’s opening not to right, but to left, but padding now main problem.

<nav>
    <ul class="nav-menu">

        <li class="dropdown">
            <a href="#" data-toggle="dropdown">About</a>
            <ul class="dropdown-menu">
              <li><a href="/foo.html">Foo</a></li>
              <li><a href="/bar.html">Bar</a></li>
            </ul>
       </li>

    </ul>
</nav>
 nav {
   width: 200px;  
   position: relative;
 }
   

.nav-menu {
   background-color: orange;
   
   display: flex;
   flex-direction: row;    

 }
 
 .dropdown {
  position: relative;
  }
   
  
  a[data-toggle="dropdown"] {
    display: flex;
    align-items: center;
    background: transparent;
    color: blue;
    padding: 10px 15px;
    border-radius: 4px;
    transition: background 0.3s ease;
    white-space: nowrap;
  }
  
  a[data-toggle="dropdown"]:hover {
    color: black;
  }

.nav-menu {
    gap: 10px;
    list-style-type: none;
    padding: 0;
    margin: 0;
    align-items: center;
    width: 100%;
}

.dropdown-menu {
    list-style: none;
    padding: 10px 15px;
    margin: 0;
    position: absolute;
    top: 100%;
    left: 0;
    /* min-width: 200px; */
    width: auto;
    box-sizing: border-box;
    background: #ffffff;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
    border-radius: 6px;
    overflow: hidden;
    transform: scaleY(0);
    transform-origin: top;
    opacity: 0;
    transition: transform 0.3s ease, opacity 0.3s ease;
    z-index: 1000;
  }
  
.dropdown-menu.show {
    transform: scaleY(1);
    opacity: 1;
  }
  
.dropdown-menu:before {
    display: block;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    clip-path: inset(0 0 0 0);
  }
document.querySelectorAll('.dropdown').forEach((dropdown) => {
  const button = dropdown.querySelector('a[data-toggle="dropdown"]');
  const menu = dropdown.querySelector('.dropdown-menu');

  button.addEventListener('click', (event) => {
    event.preventDefault();
    document.querySelectorAll('.dropdown-menu.show').forEach((openMenu) => {
      if (openMenu !== menu) {
        openMenu.classList.remove('show');
      }
    });
    const buttonWidth = button.offsetWidth;
    menu.style.width = `${buttonWidth}px`;
    menu.classList.toggle('show');
  });
});

// to close the dropdown when clicked anywhere else on the page
document.addEventListener('click', (event) => {
  if (!event.target.closest('.dropdown')) {
    document.querySelectorAll('.dropdown-menu.show').forEach((menu) => {
      menu.classList.remove('show');
    });
  }
});

https://jsfiddle.net/4y0vq17m/1/

I tried to add padding-right to .dropdown-menu but it do not works as I expect.

2

Answers


  1. You can do it by updating this 2 transform in CSS. You can also add transform-origin if needed

    .dropdown-menu {
        transform: scaleY(0) translateX(-25%);
      }
      
    .dropdown-menu.show {
        transform: scaleY(1) translateX(-25%);
      }
    
    Login or Signup to reply.
  2. Try setting value: ‘left: -30px;’ for .dropdown-menu class:

    .dropdown-menu {
        list-style: none;
        padding: 10px 15px;
        margin: 0;
        position: absolute;
        top: 100%;
        left: -30px;
        min-width: 200px;
        background: #f6f6f6 ;
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
        border-radius: 6px;
        overflow: hidden;
        transform: scaleY(0);
        transform-origin: top;
        opacity: 1;
        transition: transform 0.3s ease, opacity 0.3s ease;
        z-index: 1000;
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search