Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu September 25, 2022 . #css . #Element

How to change the background color of an element in CSS

To change the background color of an HTML element is easy using the css background-color property.

Suppose, you have an <div> element.

<div>I am a div element.</div>

Then, we can change the background color of that <div> using the following method.

Example 1 :

Using the inline styles...

<div style="background-color:red">I am a div element.</div>

Example 2 :

Using the Internal Styles...

<style>
 div{
  background-color:red;
 }
</style>
<div style="background-color:red">
 I am a div element.
</div>

Example 3 :

Using the id attribute. First, we need to include the id attribute inside the div element.

<style>
#my-id{
 color:red ;
 background-color:black;}
</style>
<div id="my-id">This is a div element.</div>
save
listen
AI Answer
Write Your Answer
loading
back
View All