Changing the Background in CSS
<div style="background: url('https://img.youtube.com/vi/8PAeN-hw33A/maxresdefault.jpg') center no-repeat; height: 400px; background-size: cover; margin-bottom: 20px;"> </div>
<p><span class="yt-core-attributed-string--link-inherit-color" dir="auto">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. </span></p>
<p><span class="yt-core-attributed-string--link-inherit-color" dir="auto">⌚Timestamps🏹 </span></p>
<ul>
<li><span class="yt-core-attributed-string--link-inherit-color" dir="auto"><a class="yt-core-attributed-stringlink yt-core-attributed-stringlink--call-to-action-color" tabindex="0" href="https://www.youtube.com/watch?v=8PAeN-hw33A&t=15s" target="">0:15</a></span><span class="yt-core-attributed-string--link-inherit-color" dir="auto"> <a href="https://www.youtube.com/watch?v=8PAeN-hw33A&t=15s">CSS Background Color </a></span></li>
<li><span class="yt-core-attributed-string--link-inherit-color" dir="auto"><a class="yt-core-attributed-stringlink yt-core-attributed-stringlink--call-to-action-color" tabindex="0" href="https://www.youtube.com/watch?v=8PAeN-hw33A&t=39s" target="">0:39</a></span><span class="yt-core-attributed-string--link-inherit-color" dir="auto"> <a href="https://www.youtube.com/watch?v=8PAeN-hw33A&t=39s">CSS Background Opacity </a></span></li>
<li><span class="yt-core-attributed-string--link-inherit-color" dir="auto"><a class="yt-core-attributed-stringlink yt-core-attributed-stringlink--call-to-action-color" tabindex="0" href="https://www.youtube.com/watch?v=8PAeN-hw33A&t=65s" target="">1:05</a></span><span class="yt-core-attributed-string--link-inherit-color" dir="auto"> <a href="https://www.youtube.com/watch?v=8PAeN-hw33A&t=65s">CSS Image Background </a></span></li>
<li><span class="yt-core-attributed-string--link-inherit-color" dir="auto"><a class="yt-core-attributed-stringlink yt-core-attributed-stringlink--call-to-action-color" tabindex="0" href="https://www.youtube.com/watch?v=8PAeN-hw33A&t=103s" target="">1:43</a></span><span class="yt-core-attributed-string--link-inherit-color" dir="auto"> <a href="https://www.youtube.com/watch?v=8PAeN-hw33A&t=103s">CSS Background Size </a></span></li>
<li><span class="yt-core-attributed-string--link-inherit-color" dir="auto"><a class="yt-core-attributed-stringlink yt-core-attributed-stringlink--call-to-action-color" tabindex="0" href="https://www.youtube.com/watch?v=8PAeN-hw33A&t=140s" target="">2:20</a></span><span class="yt-core-attributed-string--link-inherit-color" dir="auto"> <a href="https://www.youtube.com/watch?v=8PAeN-hw33A&t=140s">CSS Background Contain</a></span></li>
</ul>
<p><span class="yt-core-attributed-string--link-inherit-color" dir="auto"><iframe src="https://www.youtube.com/embed/8PAeN-hw33A" width="560" height="314" allowfullscreen="allowfullscreen"></iframe></span></p>
<div id="model-response-message-contentrd6f01278905add60" class="markdown markdown-main-panel stronger enable-updated-hr-color" dir="ltr">
<p>Using the basic class bg-test for the height and width of the div element.</p>
<p><code>.bg-test {</code><br /><code> height:300px;</code><br /><code> width:100%;</code><br /><code> margin-bottom:10px;</code><br /><code>}</code></p>
<p>then adding on some classes to this test div to demonstrate each background color. </p>
<p>so to add a <a href="https://kruxor.com/view/cssreference/ruvO9/background/">background </a>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. </p>
<p><code>.bg-color {</code><br /><code> background:rgba(245,234,566);</code><br /><code>}</code></p>
<p>now we will add some opacity to the background using the class <code>bg-opacity</code> but also still using the bg-test class as this is used for the height and width of the element. </p>
<p><code>.bg-opacity {</code><br /><code> / this sets the opacity to half of the solid color above as we are using rgba /</code><br /><code> background:rgba(245,234,566, 0.5);</code><br /><code>}</code></p>
<p>Then we can add a background image with the following class bg-image and code. </p>
<div><code>/ adding an image as a background and set it to cover the size of the div /</code></div>
<div><code>.bg-image {</code></div>
<div><code>background: url("https://i.imgur.com/GCZCt3q.png") center;</code></div>
<div><code> background-size: cover;</code></div>
<div><code>}</code></div>
<p>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. </p>
<div><code>.bg-image-resize {</code></div>
<div><code>background: url("https://i.imgur.com/GCZCt3q.png") center;</code></div>
<div><code> background-size: cover;</code></div>
<div><code> resize: both;</code></div>
<div><code> overflow: auto; </code></div>
<div><code>}</code></div>
<p> </p>
<p>Here are the CSS code examples for background properties demonstrated in the video:</p>
<h3>Background Color</h3>
<ul>
<li>
<p><code>background-color</code> [<a class="ng-star-inserted" href="http://www.youtube.com/watch?v=8PAeN-hw33A&t=18" target="blank" rel="noopener">00:18</a>]</p>
</li>
<li>
<p><code>background</code> (as a shorthand for color) [<a class="ng-star-inserted" href="http://www.youtube.com/watch?v=8PAeN-hw33A&t=31" target="blank" rel="noopener">00:31</a>]</p>
</li>
</ul>
<p> </p>
<h3>Background Opacity</h3>
<ul>
<li>
<p>Using <code>RGBA</code> values in the <code>background</code> property to set opacity. For example: <code>background: RGBA(R, G, B, A)</code> where 'A' is the opacity level (e.g., 0.8) [<a class="ng-star-inserted" href="http://www.youtube.com/watch?v=8PAeN-hw33A&t=49" target="blank" rel="noopener">00:49</a>].</p>
</li>
</ul>
<hr />
<p> </p>
<h3>Background Image</h3>
<ul>
<ul>
<li>
<p><code>background: URL('your-image-url')</code> [<a class="ng-star-inserted" href="http://www.youtube.com/watch?v=8PAeN-hw33A&t=75" target="blank" rel="noopener">01:15</a>]</p>
</li>
<li>
<p><code>background-position: center</code> [<a class="ng-star-inserted" href="http://www.youtube.com/watch?v=8PAeN-hw33A&t=83" target="blank" rel="noopener">01:23</a>]</p>
</li>
<li>
<p><code>background-repeat: no-repeat</code> [<a class="ng-star-inserted" href="http://www.youtube.com/watch?v=8PAeN-hw33A&t=87" target="blank" rel="noopener">01:27</a>]</p>
</li>
<li>
<p><code>background-size: cover</code> [<a class="ng-star-inserted" href="http://www.youtube.com/watch?v=8PAeN-hw33A&t=102" target="blank" rel="noopener">01:42</a>]</p>
</li>
<li>
<p><code>background-size: contain</code> [<a class="ng-star-inserted" href="http://www.youtube.com/watch?v=8PAeN-hw33A&t=122" target="blank" rel="noopener">02:02</a>]</p>
</li>
</ul>
</ul>
<div class="attachment-container youtube"> </div>
</div>
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