18

Hey I just have a simple Card Bootstrap 4 component.

<div class="card">
    <div class="card-header">This is my header</div>
    <div class="card-block">This is my block</div>
    <div class="card-footer">This is my footer</div>
</div>

What I wanted to accomplish was to have the header and footer with a opacity of 1 but the block with a opacity of .4. I tried to use rbga in the background-color style with no luck

.card-block { background-color: rgba(245, 245, 245, 0.4); }
Luke Flournoy
  • 3,393
  • 2
  • 16
  • 22
  • have you tried using [`opacity`](https://developer.mozilla.org/en-US/docs/Web/CSS/opacity) – happymacarts Feb 24 '17 at 05:19
  • Would it solve your problem to go for "bg-transparent" as mentioned on the link above; https://getbootstrap.com/docs/4.1/utilities/colors/ – akinov Aug 13 '18 at 22:23

8 Answers8

24

have you tried using opacity

.special-card {
/* create a custom class so you 
   do not run into specificity issues 
   against bootstraps styles
   which tends to work better than using !important 
   (future you will thank you later)*/

  background-color: rgba(245, 245, 245, 1);
  opacity: .4;
}
<div class="card">
  <div class="card-header">This is my header</div>
  <div class="card-block special-card">This is my block</div>
  <div class="card-footer">This is my footer</div>
</div>
happymacarts
  • 2,547
  • 1
  • 25
  • 33
15

please, try this:

<div class="card-transparent">
  <div class="card-header">This is my header</div>
  <div class="card-block special-card" style="background-color: rgba(245, 245, 245, 0.4); ">This is my block</div>
  <div class="card-footer">This is my footer</div>
</div>
Jorge Opazo
  • 151
  • 1
  • 2
7

Turns out the bootstrap class .card was overriding the background opacity css style I was trying to set on .card-block regardless of whether I put !important keyword or not.

I was able to fix this by adding the background style to the card and just changing the individual opacities of the .card-header and .card-footer to 1.

.card { background-color: rgba(245, 245, 245, 0.4); }
.card-header, .card-footer { opacity: 1}
Luke Flournoy
  • 3,393
  • 2
  • 16
  • 22
2

Try this approach

HTML

<div class="card text-white bg-success mb-3 mt-4" style= "">
    <div class="card-header">Пользователь</div>
    <div class="card-body special-card" style="">
        <h5 class="card-title">Primary card title</h5>
        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
    </div>
</div>
<div class="card text-white bg-primary mb-3" style=" ">
    <div class="card-header">Привязанный профиль персонала</div>
    <div class="card-body special-card">
        <h5 class="card-title">Secondary card title</h5>
        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
    </div>
</div>
<div class="card text-white bg-danger mb-3" style=" ">
    <div class="card-header">Права доступа профиля персонала</div>
    <div class="card-body special-card">
        <h5 class="card-title">Success card title</h5>
        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
    </div>
</div>

CSS

 .special-card {
            background-color: rgba(245, 245, 245, 0.4) !important;
        }

enter image description here

NoWar
  • 36,338
  • 80
  • 323
  • 498
1

Your css looks ok. I think the issue is your bootstrap file is overriding your code. Try this to override the code although I wont suggest using !important

.card-block { background-color: rgba(245, 245, 245, 0.4) !important; }

Refer to this link for the overriding. Its called specificity

Malcolm Vaz
  • 131
  • 5
0

opacity is different to the background-color opacity will set the translucency of the element and background-color will set it only for th colour of the background.

This may sound similar but they are very different.

The big diff is that opacity will make the text adn images and child elements also transplucent but the background-color will only affect the col...oh, you get it ;)

jayenne
  • 11
  • 1
  • 2
-1

Here's what has worked with me (using custom sass variables I created) to modify background colors, etc within the accordion:

::ng-deep div.card-header:hover {
background-color: var(--subtle-gray);
}

::ng-deep div.card-header a {
background-color: var(--accent);
text-decoration: none !important;
}

::ng-deep div.card-body {
background-color: var(--subtle-gray);
}
-3

In Bootstrap 4 you can type "opacity-50" into class thats mean opacity:0.5 in css without type any css code and "opacity-30" thats mean opacity:0.3 in css etc...

`<div class="card">
    <div class="card-header">This is my header</div>
    <div class="card-block opacity-50">This is my block</div>
    <div class="card-footer">This is my footer</div>
</div>`
  • 1
    That is not correct. It is possible since Bootstrap 5.1, see this [link](https://getbootstrap.com/docs/5.1/utilities/opacity/) – Philipp Palmtag Jul 18 '22 at 08:10