As stated in another answer, the background is shown behind the actual image.
Solution
You could create an overlay using a CSS pseudo element (::before
or ::after
). This prevents you from having to add extraneous HTML elements.
See the following snippet:
.card {
height: 500px;
position: relative; // Allows absolute positioning of the pseudo element
}
.card::after {
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: linear-gradient(280.03deg, rgba(44, 39, 39, 0.76) 17.44%, rgba(44, 39, 39, 0) 53.16%); // Background is defined on the pseudo-element, not on the image
z-index: 1; // Place the pseudo element above the content
}
.card img {
height: 100%;
width: 100%;
}
.card .content {
position: absolute;
bottom: 32px;
color: white;
}
<div class="container">
<div class="card">
<img src="https://placekitten.com/1440/671" />
<div class="content">
<p>
Why does my linear gradient background not work?
</p>
</div>
</div>
</div>
Alternative solution
If you don't need to have an img
element in the HTML (i.e. in case the image has no actual semantic value to the page, but is purely used for styling), it's recommended to have the image as a background in CSS. You can then use multiple backgrounds via CSS to have both the image and the gradient as a background.
.card {
height: 500px;
background: linear-gradient(280.03deg, rgba(44, 39, 39, 0.76) 17.44%, rgba(44, 39, 39, 0) 53.16%), url('https://placekitten.com/1440/671'); // Multiple CSS backgrounds
}
.card .content {
position: absolute;
bottom: 32px;
color: white;
}
<div class="container">
<div class="card">
<div class="content">
<p>
Why does my linear gradient background not work?
</p>
</div>
</div>
</div>