在Bootstrap-Vue中渲染自定义样式

san*_*ina 2 html javascript css vue.js bootstrap-vue

我正在尝试向使用Bootstrap-Vue的下拉菜单组件添加一些自定义样式。我在这里使用文档。

这是我的模板:

<div class="container">
  <b-dropdown id="dropdownMenuButton" text="Dropdown Button" no-caret class="custom-dropdown">
    <b-dropdown-item>First Action</b-dropdown-item>
    <b-dropdown-item>Second Action</b-dropdown-item>
    <b-dropdown-item>Third Action</b-dropdown-item>
    <b-dropdown-divider></b-dropdown-divider>
    <b-dropdown-item>Something else here...</b-dropdown-item>
    <b-dropdown-item disabled>Disabled action</b-dropdown-item>
  </b-dropdown>
</div>
Run Code Online (Sandbox Code Playgroud)

我发现我可以设置#dropdownMenuButton的样式,但是当下拉列表在浏览器中呈现时,它会在#dropdownMenuButton内创建一个元素,而我无法设置样式。我尝试这样做:

<style scoped>
  #dropdownMenuButton > button {
    width: 100%;
  }

  #dropdownMenuButton__BV_toggle_ {
    width: 100%;
  }
</style>
Run Code Online (Sandbox Code Playgroud)

但是没有运气。仅供参考,要创建的按钮的ID为dropdownMenuButton__BV_toggle_

Ran*_*ies 11

/deep/如果您不想弄乱全局样式,也可以使用关键字来影响子组件:

<style scoped>
  /deep/ #dropdownMenuButton > button {
    width: 100%;
  }

  /deep/ #dropdownMenuButton__BV_toggle_ {
    width: 100%;
  }
</style>
Run Code Online (Sandbox Code Playgroud)


Maa*_*tje 7

这是因为您要使样式范围限定于组件,并且由于bootstrap vue下拉列表是另一个组件,因此您将无法使用范围样式来做到这一点。

https://vue-loader.vuejs.org/guide/scoped-css.html

尝试像这样删除范围属性。

<style>
  #dropdownMenuButton > button {
    width: 100%;
  }

  #dropdownMenuButton__BV_toggle_ {
    width: 100%;
  }
</style>
Run Code Online (Sandbox Code Playgroud)