Krzysztof Karczewski
Krzysztof Karczewski

Reputation: 63

How to change table header color in BootstraVue b-table

I would like to change the color of b-table in BootstraVue b-table component. The simple example is:

<template>
    <div>
        <b-table :items="items" thead-class="greenColor">
        </b-table>
    </div>
</template>
<style scoped>
.greenColor, .table thead th, thead, th {
    background-color: #00FF00 !important;
}
</style>
<script>
export default {
    data() {
        return {
            items: [
                {name: "Paweł", surname: "Kowalski"},
                {name: "John", surname: "Nowak"}
            ]
        }
    }
}
</script>

As you see I tried to set thead-class (and class sets correctly but doesn't work) and change style of thead element but the table is still white. Do you the way that I can change the color of this header?

And some of my dependencies from package.json: "nuxt": "^2.0.0", "bootstrap": "^4.1.3", "bootstrap-vue": "^2.0.0"

Upvotes: 1

Views: 10879

Answers (1)

Hiws
Hiws

Reputation: 10324

The problem you're facing is because you're using a scoped style tag. If you want to target subcomponents, you'll need to use a deep selector to target them properly.

new Vue({
  el: '#app',
  data() {
    return {
      items: [
        { first: 'Mikkel', last: 'Hansen', age: 16 }
      ],
      fields: [
        /* 
          Optionally define a class per header, 
          this will overlay whatever thead-class background you choose 
        */
        { key: 'first', thClass: 'bg-white text-dark' },
        { key: 'last' },
        { key: 'age' }
      ]
    }
  }
})
<script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/bootstrap-vue.js"></script>

<link href="https://unpkg.com/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://unpkg.com/[email protected]/dist/bootstrap-vue.css" rel="stylesheet"/>

<div id="app">
  <!-- If the CSS class is globally available (like bg-dark) you can simply use it in thead-class -->
  <b-table :items="items" thead-class="green-bg bg-dark text-white"></b-table>
  
  <!-- Optinally you can use the fields property and define classes per column -->
  <b-table :items="items" :fields="fields" thead-class="green-bg bg-dark text-white"></b-table>
</div>

<!-- Disabled since it doesn't work for SO snippets.
<style scoped>
  /* Example of how to use a deep selector */
  /deep/ .green-bg {
    background-color: green;
  }
<style>
-->

Upvotes: 3

Related Questions