1

I want to modify a default style of a component of react-bootstrap.

For example, I use the Panel component and I want to have a bold title. How can I achieve this? If I use bsClass, then I loose all default styles of a "warning" panel.

What is the best way, to create a class, that inherits everything from the default panel classes and allows to override only the properties that we want?

Thanks,

Henry

1
  • Is there a way to have a class that inherits from the default bootstrap class? or another way to add extra styles? Commented Jan 5, 2017 at 12:45

1 Answer 1

1

The bootstrap docs actually include an example of a bold title here. You can use a heading tag that has font-weight bold already applied and use the "panel-title" class for sizing and alignment. Here's the example from the docs I'm talking about:

<div class="panel panel-default">
  <div class="panel-heading">
    <h3 class="panel-title">Panel Title</h3>
  </div>
  <div class="panel-body">
    Panel content
  </div>
</div>

But if your question is more generic, i.e. how do I change built in styles?, any of the normal css mechanisms should work, like using a strong tag:

<div class="panel panel-warning">
  <div class="panel-heading">
    <strong>Panel Title</strong>
  </div>
  <div class="panel-body">
    Panel content
  </div>
</div>

Or adding an inline style:

<div class="panel panel-warning">
  <div class="panel-heading" style="font-weight: bold">Panel Title</div>
  <div class="panel-body">
    Panel content
  </div>
</div>

Or (probably preferred) by another class from your own external stylesheet:

/* CSS file */
.bold {
  font-weight: bold
}

<div class="panel panel-warning">
  <div class="panel-heading bold">Panel Title</div>
  <div class="panel-body">
    Panel content
  </div>
</div>

If you're talking about modifying all panel titles to be bold, then you should either 1) copy the bootstrap definition for "panel-heading," make the changes you want to your copy, and place it in a higher priority stylesheet (i.e. lower on the page) so it takes precedence (but this adds weight to your css payload), 2) modify the existing bootstrap code (bad idea), or 3) compile your own version using less by importing the main bootstrap less files and supplementing them with your own stuff. The last is definitely the most flexible and arguably the best, but it's more work. But this approach would let you "inherit" styles (as you say) because less let's you apply other classes as part of your own class, like:

.bold-heading {
  .panel-heading; // Apply base panel-heading styles
  font-weight: bold; // And override the stuff you want to change
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.