<div class="bg-image"
style="background-image: url('https://example.com/image.jpg');
height: 100vh">
</div>
The above code snippet illustrates how to add a background to a webpage. The image used is just an example.
If you want to make the background faded, here's an example using Bootstrap:
.covered {
position: relative;
padding: 30px;
}
.covered-img {
background: url('https://example.com/image.jpg');
opacity: .25;
background-size: cover;
background-position: center;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
<div class="row">
<div class="col-xs-12 covered">
<div class="covered-img"></div>
<p>Placeholder Text
<p>
</div>
</div>
According to a Stack Overflow user, this is how the code works:
How it works: The image as the first child in the parent container ensures it fills the container size due to absolute positioning. The relative position of the container means children's absolutes are relative to it. Text added after the image gets drawn on top.
Now let's apply that faded style to a background image:
.covered {
position: relative;
}
.covered-img {
background: url('https://example.com/image.jpg');
opacity: .25;
height: 100vh;
background-size: cover;
background-position: center;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
<div class="row">
<div class="col-xs-12 covered">
<div class="covered-img"></div>
<p>Placeholder Text
<p>
</div>
</div>
For more information on Bootstrap 5 Image Backgrounds, check out this article!