Home > Enterprise >  How to read full bootstrap classes from collapsible buttons and customize it?
How to read full bootstrap classes from collapsible buttons and customize it?

Time:09-08

I am trying to implement collapsible buttons on my web-app (built through GAS). I am new to building pages in html so i am a bit stuck with a problem and didn't really find any answers on other topics.

The buttons work properly and hide/show the content i want through jquery, i pasted the snippets needed in head and body. My problem is that the bootstrap buttons are styled a certain way and it doesn't fit my web-app global aspect, so i would like to be able to customize it at will.

But to do so, I need to find and read the specifics of each bootstrap classes in order to override it correctly.

I thought it would be better not to post my whole code because it's a theorical question.

So I have my button:

 <button type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
      Button with data-target
    </button>

Which targets the pannel:

<div  id="collapseExample">
    <div >
      <table>
        <tr>
          <td colspan="3">OK</td>
          <td>OK</td>
          <td colspan="3">OK</td>
        </tr>
        <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
        <td>5</td>
        <td>6</td>
        <td>7</td>
        </tr>
      </table>
    </div>
  </div>

Specifically i would like to be able to change the background when the button is active/clicked.

I tried inspecting the page but declaring the class

.collapse show{
    background-color: red;
  }

didn't change a thing, and anyways, i am not even sure that is the correct way to do it.

For info, these are the links pasted:

<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

and in the body:

src="https://code.jquery.com/jquery-3.6.0.min.js" 
integrity="sha256-/xUj 3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-eMNCOe7tC1doHpGoWe/6oMVemdAVTMs2xqW4mwXrXsW0L84Iytr2wi5v2QjrP/xp" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cn7l7gDp0eyniUwwAZgrzD06kc/tftFf19TOAs2zVinnD/C7E91j9yyk5//jjpt/" crossorigin="anonymous"></script>

Any help is welcome! Thanks

CodePudding user response:

Styling buttons or elements in general you have two ways to do so:

  1. Using the bootstrap classes. You can find the different bootstrap button classes here: https://getbootstrap.com/docs/5.1/components/buttons/
  2. Adding your own class and styling.

Your solution wasn't totally wrong, you just forgot the dot: .collapse.show But this classes are not meant for the buttons but the collapsable element. To style the button I always create my own classes. You can also use the attributes in css like aria-expaned="true" or type="button" by adding it in []. Here you can find one example of stylings:

.custom-btn{
  color: red;
}

.custom-btn[aria-expanded="true"]{
  color: green;
}

.collapse.show{
  color: blue;
}
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>

<button type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample" >
  Button with data-target
</button>
    
<div  id="collapseExample">
  <div >
    Collapsed Text.
  </div>
</div>

I hope my anwer was helpful for your problem.

  • Related