Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace Bootstrap dropdown-toggle icon with another default icon?

Is there a pure CSS method to replace the caret icon in Bootstrap's dropdown-toggle class? I want it to look like a downward arrowhead or logical-or symbol (∨), like this:

dropdown with arrow

instead of the original downward solid triangle caret. I've found answers for replacing the size of the original icon:

.dropdown-toggle::after {
    display: inline-block; /* Default */
    width: 0; /* Default */
    height: 0; /* Default */
    margin-left: .3em; /* Default */
    vertical-align: middle; /* Default */
    content: ""; /* Default */
    border-top: .3em solid; /* caret size */
    border-right: .3em solid transparent; /* caret size */
    border-left: .3em solid transparent; /* caret size */
}

but I can't figure out a good way to make it an arrowhead. Another question referenced here requires you to manually embed the image in HTML.

like image 252
Katie Avatar asked Jan 28 '19 05:01

Katie


2 Answers

You might change the default values to the following:

.dropdown-toggle {
  font: 400 1.5rem/1.25 sans-serif;
  color: white;
  background: purple;
  padding: .5em 1em;
}

.dropdown-toggle::after {
  display: inline-block;
  width: .3em;
  height: .3em;
  margin: -.3em 0 0 .4em;
  vertical-align: middle;
  content: "";
  border: .3em solid;
  border-width: 0 .15em .15em 0;
  transform: rotateZ(45deg)
}
<span class="dropdown-toggle">CUSTOMERS</span>
like image 192
Kosh Avatar answered Oct 05 '22 16:10

Kosh


As described in this other SO thread, you can either just remove the current icon and use a <span> or <i> tag to add your own custom/Font Awesome icon like this:

.dropdown-toggle::after {
        display: none !important;
 }
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script><link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/><link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<div class="dropdown">
  <button type="button" class="btn btn-secondary dropdown-toggle"  data-toggle="dropdown" style="background-color: #7c2a8b;">
    Customer
    <i class="fa fa-angle-down"></i>
  </button>
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#">A</a>
    <a class="dropdown-item" href="#">B</a>
    <a class="dropdown-item" href="#">C</a>
  </div>
</div>

Or you can skip the css part and just remove the dropdown-toggle class name and add the icon to your html like this:

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script><link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/><link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<div class="dropdown">
  <button type="button" class="btn btn-secondary" data-toggle="dropdown" style="background-color: #7c2a8b;">
    Customer
    <i class="fa fa-angle-down"></i>
  </button>
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#">A</a>
    <a class="dropdown-item" href="#">B</a>
    <a class="dropdown-item" href="#">C</a>
  </div>
</div>
like image 26
AndrewL64 Avatar answered Oct 05 '22 15:10

AndrewL64