Search code examples
htmltwitter-bootstrapbootstrap-4dropdown

Dropdown-toggle should be a link as well


I have set up a dropdown toggle with BS4. The dropdown slides down nicely on hover. But now the product owner wants to have the toggle button as a clickable link (in my sample: href="https://www.yahoo.com). But I don't know how to achieve this. Is there a solution for this? Or is it easier to re-build the toggle menu to something else, to have a link seperated from a sliding div (or whatever) afterwards?

Here is my toggle, what I have so far:

html {
  font-family: Arial;
  font-size: 1.4rem;
  line-height: 1.4rem;
}

.dropdown {
  display: inline-block;
  padding: 0 7px;
}
.dropdown.show {
  background-color: rgba(255, 255, 255, 0.9);
}
.dropdown a::before {
  content: "";
}
.dropdown a.dropdown-toggle {
  cursor: pointer;
  padding: 3px 7px;
}
.dropdown a.dropdown-toggle::after {
  font-size: 1.4rem;
  content: ">";
  color: Orange;
  line-height: 1.4rem;
  border-top: 0;
  border-right: 0;
  border-bottom: 0;
  border-left: 0;
  vertical-align: top;
  transition: 0.5s;
}
.dropdown a.dropdown-toggle:active {
  pointer-events: none;
}
.dropdown a.dropdown-toggle:hover:after {
  transform: rotate(90deg);
}
.dropdown .dropdown-menu {
  min-width: initial;
  padding: 0.5rem;
  margin: 0;
  margin-left: 7px;
  font-size: initial;
  border-radius: 0;
  transition: all 0.5s;
  overflow: hidden;
  transform-origin: top center;
  transform: scale(1, 0);
  display: block;
  border: none;
  box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16);
}
.dropdown .dropdown-menu a.dropdown-item {
  font-size: 1.4rem;
  line-height: 1.6rem;
  text-decoration: none;
}
.dropdown .dropdown-menu a.dropdown-item:hover {
  color: Orange;
}
.dropdown:hover .dropdown-toggle {
  background-color: rgba(255, 255, 255, 0.9);
}
.dropdown:hover .dropdown-menu {
  transform: scale(1);
}

.dropdown-menu:hover + a:after {
  transform: rotate(90deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>


<div class="dropdown">
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#">Sub1</a>
    <a class="dropdown-item" href="#">Sub2</a>
    <a class="dropdown-item" href="#">Sub3</a>
  </div>
  <a class="dropdown-toggle" href="http://www.yahoo.com" data-toggle="dropdown">
    MAIN
  </a>
</div>


Solution

  • Update

    The use of bootstrap's attribute and class was blocking this. i.e, data-toggle="dropdown" and .dropdown-toggle since they're tied to the framework.

    In addition to pointer-events: none.

    html {
      font-family: Arial;
      font-size: 1.4rem;
      line-height: 1.4rem;
    }
    
    .dropdown {
      display: inline-block;
      padding: 0 7px;
    }
    .dropdown.show {
      background-color: rgba(255, 255, 255, 0.9);
    }
    .dropdown a::before {
      content: "";
    }
    .dropdown a.cc-dropdown-toggle {
      cursor: pointer;
      padding: 3px 7px;
      text-decoration: none;
     
    }
    .dropdown a.cc-dropdown-toggle::after {
      font-size: 1.4rem;
      content: ">";
      color: Orange;
      line-height: 1.4rem;
      border-top: 0;
      border-right: 0;
      border-bottom: 0;
      border-left: 0;
      vertical-align: top;
      transition: 0.5s;
      display:inline-block;
    }
    .dropdown a.cc-dropdown-toggle:hover:after {
      transform: rotate(90deg);
    }
    .dropdown .dropdown-menu {
      min-width: initial;
      padding: 0.5rem;
      margin: 0;
      margin-left: 7px;
      font-size: initial;
      border-radius: 0;
      transition: all 0.5s;
      overflow: hidden;
      transform-origin: top center;
      transform: scale(1, 0);
      display: block;
      border: none;
      box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16);
    }
    .dropdown .dropdown-menu a.dropdown-item {
      font-size: 1.4rem;
      line-height: 1.6rem;
      text-decoration: none;
    }
    .dropdown .dropdown-menu a.dropdown-item:hover {
      color: Orange;
    }
    .dropdown:hover .cc-dropdown-toggle {
      background-color: rgba(255, 255, 255, 0.9);
    }
    .dropdown:hover .dropdown-menu {
      transform: scale(1);
    }
    
    .dropdown-menu:hover + a:after {
      transform: rotate(90deg);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
    
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
    
    
    <html>
    
    <body>
      <div class="dropdown">
        <div class="dropdown-menu">
          <a class="dropdown-item" href="#">Sub1</a>
          <a class="dropdown-item" href="#">Sub2</a>
          <a class="dropdown-item" href="#">Sub3</a>
        </div>
        <a class="cc-dropdown-toggle" href="http://www.yahoo.com">
          MAIN
        </a>
      </div>
    </body>
    
    </html>