Changing the Background in CSS
Some of the properties of using a CSS background on a div, how to set the background color, opacity and also using an image as a background with CSS.
⌚Timestamps🏹
- 0:15 CSS Background Color
- 0:39 CSS Background Opacity
- 1:05 CSS Image Background
- 1:43 CSS Background Size
- 2:20 CSS Background Contain
Using the basic class bg-test for the height and width of the div element.
.bg-test {
height:300px;
width:100%;
margin-bottom:10px;
}
then adding on some classes to this test div to demonstrate each background color.
so to add a background color, i added the class bg-color and then to this using the background as a shorthand for the background-color as you can just pass it a color value.
.bg-color {
background:rgba(245,234,566);
}
now we will add some opacity to the background using the class bg-opacity
but also still using the bg-test class as this is used for the height and width of the element.
.bg-opacity {
/* this sets the opacity to half of the solid color above as we are using rgba */
background:rgba(245,234,566, 0.5);
}
Then we can add a background image with the following class bg-image and code.
/* adding an image as a background and set it to cover the size of the div */
.bg-image {
background: url("https://i.imgur.com/GCZCt3q.png") center;
background-size: cover;
}
If you want to see how the image resizes you can also set the div value to resize which you can see in the last example.
.bg-image-resize {
background: url("https://i.imgur.com/GCZCt3q.png") center;
background-size: cover;
resize: both;
overflow: auto;
}
Here are the CSS code examples for background properties demonstrated in the video:
Background Color
Background Opacity
-
Using
RGBA
values in thebackground
property to set opacity. For example:background: RGBA(R, G, B, A)
where 'A' is the opacity level (e.g., 0.8) [00:49].
Background Image
HTML
<h3>Background Color Example</h3>
<div class="bg-test bg-color"></div>
<h3>Background Opacity Example</h3>
<div class="bg-test bg-opacity"></div>
<h3>Background Image Example</h3>
<div class="bg-test bg-image"></div>
<h3>Background Image Resize Example</h3>
<p>In this example you can drag the div size around and the image will stay covering the div.</p>
<div class="bg-test bg-image-resize"></div>
CSS
.bg-test {
height:300px;
width:100%;
margin-bottom:10px;
}
.bg-color {
background:rgba(245,234,566);
}
.bg-opacity {
/* this sets the opacity to half of the solid color above as we are using rgba */
background:rgba(245,234,566, 0.5);
}
/* adding an image as a background and set it to cover the size of the div */
.bg-image {
background: url("https://i.imgur.com/GCZCt3q.png") center;
background-size: cover;
}
.bg-image-resize {
background: url("https://i.imgur.com/GCZCt3q.png") center;
background-size: cover;
border: 2px solid;
width: 300px;
resize: both;
overflow: auto;
}
Background Color Example
Background Opacity Example
Background Image Example
Background Image Resize Example
In this example you can drag the div size around and the image will stay covering the div.
External Link for Changing the Background in CSS