How to Set the background-color of different Elements in CSS? Last Updated : 15 Oct, 2024 Comments Improve Suggest changes Like Article Like Report Background colors in CSS set the color behind an element's content and padding. Use the background-color property to assign colors to elements, specifying values in hex, RGB, or color names. This allows customization of elements' backgrounds for design and readability.Using Background Color PropertyThe background-color property of CSS is used to set the background of an element. We can set the applying background color by selecting the element by its class name of id name and then apply the background-color property on it to set the background color.Syntaxbackground-color: color_name;Example 1: Here each <div> has a unique background color, while a <p> element also applies a class and inline styles, HTML <!DOCTYPE html> <html lang="en"> <head> <title>setting the background-color of different elements in CSS</title> <style> div { margin: 50px 50px; font-size: 50px; } .Container1 { background-color: green; } .Container2 { background-color: rgb(163, 158, 158); color: black; } .Container3 { background-color: rgb(102, 119, 102); } </style> </head> <body> <div class="Container1"> geeksforgeeks in div1 </div> <div class="Container2"> geeksforgeeks in div2 </div> <p class="Container3" style="margin:50px 50px; font-size: 50px;"> geeksforgeeks in p </p> </body> </html> OutputExample 2: Here we setting different background colors for various elements using CSS classes. A <div>, <a>, and <p> element feature unique backgrounds, HTML <!DOCTYPE html> <html lang="en"> <head> <title> setting the background-color of different elements in CSS</title> <style> div { margin: 50px 50px; font-size: 50px; } .container1 { background-color: green; } .container2 { background-color: rgb(163, 158, 158); color: black; margin: 50px 50px; font-size: 30px; } .container3 { background-color: rgb(102, 119, 102); } </style> </head> <body> <div class="container1"> geeksforgeeks in div </div> <a class="container2" href="https://www.geeksforgeeks.org/"> geeksforgeeks link </a> <p class="Container3" style="margin:50px 50px; font-size: 40px;"> geeksforgeeks in p </p> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Set the background-color of different Elements in CSS? N nikhilchhipa9 Follow Improve Article Tags : Web Technologies CSS CSS-Questions Similar Reads How to set the text-color for different elements in CSS ? Colors are very important to give a good look to the website and engage the users. So, different colored text elements are added to enhance the visuals of the webpage. The CSS color property defines the text color for an HTML element. For setting text-color for different elements in CSS, add the app 2 min read How to set background-image for the body element using CSS ? In this article, we set the image into the background using CSS property. The background-image property is used to set one or more background images to an element. By default, it places the image in the top left corner. To specify two or more images, separate the URLs with a comma. Syntax: backgroun 2 min read How to set the background color of the specified elements using jQuery ? In this article, we will see how to set the background color of specific elements using jQuery.To set the background-color, we use css() method. The css() method in jQuery is used to get or set CSS properties of selected elements. It allows you to retrieve the value of a CSS property or modify it by 2 min read How to set different background properties in one declaration? We are going to use the Shorthand property in this article to set different background properties. To reduce the length of the CSS code we can declare background properties in one line through "Shorthand Property".Through this property we can define different properties of background in a single lin 2 min read How to Change Selected Text Background Color in CSS? Sometimes, we need to change the color and background color of the selected text. The ::selection pseudo-element is used to change the background color of the selected text in CSS. This pseudo-element allows you to style the portion of text that has been selected by the user. Using ::selection Pseud 2 min read Like