How to remove the arrow in dropdown in Bootstrap 4

2019-02-02 19:16发布

I am using Bootstrap 4. I tried to remove the arrow in dropdown.

The answers I found for Bootstrap 3 do not work any more.

The jsfiddle is here.

<div class="dropdown open">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenu1">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
  </div>
</div>

10条回答
趁早两清
2楼-- · 2019-02-02 19:38

Simply remove "dropdown-toggle" class from the element. The dropdown will still work if you have the data-toggle attribute as follows

<button role="button" type="button" class="btn" data-toggle="dropdown"> 
    Dropdown Without Arrow
</button>

overriding .dropdown-toggle class styles affects all dropdowns and you may want to keep the arrow in other buttons, that's why this looks me the simplest solution.

查看更多
淡お忘
3楼-- · 2019-02-02 19:42

I don't recommend any of the existing answers because:

  • .dropdown-toggle has more styling than just the caret. Removing the class from the element causes styling issues.

  • Overriding .dropdown-toggle doesn't make sense. Just because you don't need a caret on some particular element, doesn't mean you won't need one later.

  • ::after doesn't cover dropdown variants (some use ::before).

Use a custom .caret-off class instead:

.caret-off::before {
    display: none;
}
.caret-off::after {
    display: none;
}
查看更多
可以哭但决不认输i
4楼-- · 2019-02-02 19:45

remove the "dropdown-toggle" class

查看更多
Ridiculous、
5楼-- · 2019-02-02 19:49

Boostrap generates this using the CSS border:

.dropdown-toggle:after {
  border: none;
}
查看更多
Ridiculous、
6楼-- · 2019-02-02 19:49

This works on bootsrap4 and ng-bootstrap.

.dropdown-toggle:after {
    display: none;
}
查看更多
Luminary・发光体
7楼-- · 2019-02-02 19:58

I was using the accepted answer for quite a while in my project but just now stumbled across a variable used by bootstrap:

$enable-caret: true !default;

If you set this to false then the caret will be removed without having to do any custom code.

My project was Ruby/Rails so I was using the bootstrap-rubygem. I changed the variable by importing a custom-variables.scss with the above variable set to false in my application.scss BEFORE the bootstrap.scss file/files.

查看更多
登录 后发表回答