简体   繁体   中英

Add tip to Twitter Bootstrap's Dropdown Menu

The dropdown menu in Twitter Bootstrap's nav component has a "tip" at the top of the dropdown menu.

在此处输入图片说明

How can you add a tip to a normal dropdown menu?

在此处输入图片说明

<div class="dropdown">
    <a href="#" data-toggle="dropdown">
        Click me
    </a>
    <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
        <li><a tabindex="-1" href="#">Action</a></li>
    </ul>
</div>

Add this CSS:

.dropdown-menu:before {
  position: absolute;
  top: -5px;
  left: 9px;
  display: inline-block;
  border-right: 7px solid transparent;
  border-bottom: 7px solid #ccc;
  border-left: 7px solid transparent;
  border-bottom-color: rgba(0, 0, 0, 0.2);
  content: '';
}

.dropdown-menu:after {
  position: absolute;
  top: -3px;
  left: 10px;
  display: inline-block;
  border-right: 6px solid transparent;
  border-bottom: 6px solid #ffffff;
  border-left: 6px solid transparent;
  content: '';
}

I got it from the nav dropdown styles and changed a couple of values. It should be easy to tune for your needs.

Here's an updated solution for Bootstrap 4. Just apply the following CSS and add a dropdown-menu-tip-[n|ne|nw|s|se|sw] modifier to your menu.

[class*="dropdown-menu-tip-"]::after {
  content: '';
  position: absolute;
  width: .5rem;
  height: .5rem;
  background-color: white; /* For SCSS use $dropdown-bg */
  border: solid 1px rgba(0, 0, 0, .15); /* For SCSS use $dropdown-bg */
  border-bottom: none;
  border-left: none;
}

/* North */
.dropdown-menu-tip-n::after {
  top: calc(-.25rem - 1px);
  left: calc(50% - .25rem);
  transform: rotate(-45deg);
}

/* Northeast */
.dropdown-menu-tip-ne::after {
  top: calc(-.25rem - 1px);
  right: 1rem;
  transform: rotate(-45deg);
}

/* Northwest */
.dropdown-menu-tip-nw::after {
  top: calc(-.25rem - 1px);
  left: 1rem;
  transform: rotate(-45deg);
}

/* South */
.dropdown-menu-tip-s::after {
  left: calc(50% - .25rem);
  bottom: calc(-.25rem - 1px);
  transform: rotate(135deg);
}

/* Southeast */
.dropdown-menu-tip-se::after {
  right: 1rem;
  bottom: calc(-.25rem - 1px);
  transform: rotate(135deg);
}

/* Southwest */
.dropdown-menu-tip-sw::after {
  left: 1rem;
  bottom: calc(-.25rem - 1px);
  transform: rotate(135deg);
}

Demo: https://codepen.io/claviska/pen/LzvMpx

Source: https://www.abeautifulsite.net/adding-a-checked-state-to-bootstrap-4-dropdown-menus

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM