this content was originally posted here.

darken background image using css

darken a background image using css only useful if you have overlay text or just want to add a nicer effect to an image, i usually find that adding this to an image with the text over the top makes it much easier to read and also you can make a bunch of images on the same page fit into the same theme, as you can add rgba color color tint here.

Demo

code

you will need to include this base css class for the demos to work the same as below

.demo-darken-shared {
	height:300px;
	width:300px;
	display: flex;
	align-items: center;
	justify-content: center;
	color:#FFF;		
	font-size:20px;
	font-weight:bold;
	text-shadow:0px 1px 2px rgba(0,0,0,0.8);
	text-align: center;
}

demo image original

some text overlay
harder to read

demo image original - code

.demo-darken-img {
	background:url(images/coffee-2608864_1280.jpg) center no-repeat;
	background-size:cover;
}

demo image with filter

some text overlay
easier to read

demo image with filter - code

.demo-darken-img-faded {
	background:linear-gradient( rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0.6) ),url(images/coffee-2608864_1280.jpg) center no-repeat;
	background-size:cover;
}

demo image with a gradient

some text overlay
easier to read
with a fade

demo image with a gradient - code

.demo-darken-img-dlfaded {
	background:linear-gradient( rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.8) ),url(images/coffee-2608864_1280.jpg) center no-repeat;
	background-size:cover;
}

demo image with a color tint

some text overlay
easier to read
with a colour

demo image with a color tint - code

.demo-darken-img-colour {
	background:linear-gradient( rgba(50, 115, 220, 0.6), rgba(50, 115, 220, 0.6) ),url(images/coffee-2608864_1280.jpg) center no-repeat;
	background-size:cover;
}