Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the opacity of a card-block in bootstrap 4

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); }
like image 509
Luke Flournoy Avatar asked Feb 24 '17 04:02

Luke Flournoy


People also ask

How do I change the opacity in Bootstrap?

Bootstrap Opacity Classes – Custom CSS Bootstrap Snippets Library / Utilities Examples Currently, Bootstrap 4 does not have any opacity utility classes. You can add this to a custom stylesheet to quickly add the ability to write.opacity- (0-5) to change the opacity of an element.

How to change opacity/transparency of an element using CSS?

The opacity property specifies the opacity/transparency of an element. The opacity property can take a value from 0.0 - 1.0. The lower value, the more transparent: The opacity property is often used together with the :hover selector to change the opacity on mouse-over: The first CSS block is similar to the code in Example 1.

How to set the opacity of an element?

The opacity property sets the opacity level for an element. The opacity level describes the transparency level, where 1 is not transparent at all, .5 is 50% visible, and 0 is completely transparent. Set the opacity of an element using .opacity- {value} utilities.

How do I make a card clickable in Bootstrap 4?

Cards. A card in Bootstrap 4 is a bordered box with some padding around its content. It includes options for headers, footers, content, colors, etc. ... Add the .stretched-link class to a link inside the card, and it will make the whole card clickable and hoverable (the card will act as a link): John Doe.


Video Answer


5 Answers

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>
like image 188
happymacarts Avatar answered Oct 19 '22 12:10

happymacarts


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>
like image 34
Jorge Opazo Avatar answered Oct 19 '22 13:10

Jorge Opazo


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}
like image 25
Luke Flournoy Avatar answered Oct 19 '22 13:10

Luke Flournoy


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

like image 37
Friend Avatar answered Oct 19 '22 12:10

Friend


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

like image 1
Malcolm Vaz Avatar answered Oct 19 '22 12:10

Malcolm Vaz