Introduction

You can follow my previous article to learn, what is CSS and why we use CSS: click here.
In CSS, colors are generally used as-
  • Using the name of the color. Example- color-red.
  • Using the RGB value of color. Example- #ff0000.
  • Using the Hex value of the color. Example- #fffff.
Color
To set the color of an HTML element, the 'color' property is used. We can use the name, RGB and HEX value to change the color of HTML element.
Syntax:
selected_element
{
color: color_name;
}
Example
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. h1 {
  6. color: blue;
  7. }
  8. p {
  9. color: red;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <h1>c# corner</h1>
  15. <p>c-sharpcorner.com</p>
  16. </body>
  17. </html>
Output
Background
In CSS, we can set the background, like the following-
  • background-color
    We can set the background color, using background-color property and we can set the background of the body or an individual element.
    Syntax
    selected_element
    {
    background-color:color_name;
    }
    Example
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <style>
    5. body {
    6. background-color: orange;
    7. }
    8. h1 {
    9. color: white;
    10. }
    11. </style>
    12. </head>
    13. <body>
    14. <h1>c# corner</h1>
    15. <p>c-sharpcorner.com</p>
    16. </body>
    17. </html>
    Output
    For the background color, using RGB, we used Orange color and the RGB value of Orange color is #ffa500.
    Example
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <style>
    5. body {
    6. background-color: #ffa500;
    7. }
    8. h1 {
    9. color: white;
    10. }
    11. </style>
    12. </head>
    13. <body>
    14. <h1>c# corner</h1>
    15. <p>c-sharpcorner.com</p>
    16. </body>
    17. </html>
    Output
    For the background color, using HEX value, we used Orange color and the Hel value of Orange color is #FFA500.
    Example
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <style>
    5. body {
    6. background-color: #FFA500;
    7. }
    8. h1 {
    9. color: white;
    10. }
    11. </style>
    12. </head>
    13. <body>
    14. <h1>c# corner</h1>
    15. <p>c-sharpcorner.com</p>
    16. </body>
    17. </html>
    Output
  • background-image
    To set an image in the background, the background-image property is used. In CSS, by default, the image is repeated, so it covers the whole page.
    Syntax
    selected_element
    {
    background-image:url("url_image");
    }
    Example
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <style>
    5. body {
    6. background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
    7. }
    8. </style>
    9. </head>
    10. <body>
    11. <h1>c# corner</h1>
    12. <p>c-sharpcorner.com</p>
    13. </body>
    14. </html>
    Output
Set Background image of heading element
We can use the heading selector to set the background image of <h1>.
Example
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. h1 {
  6. background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <h1>c# corner</h1>
  12. <p>c-sharpcorner.com</p>
  13. </body>
  14. </html>
Output
Background repeat
To repeat the background of a body or an individual element, background-repeat is used.
Syntax
selected_element
{
background-image:url("url_image");
background-repeat;
}
Example
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
  7. backgound-repeat;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h1>c# corner</h1>
  13. <p>c-sharpcorner.com</p>
  14. </body>
  15. </html>
Output
background-repeat in x-direction
Repeat image in x (horizontal), using background-repeat:repeat-x property
Syntax
selected_element
{
background-image: url("image_url");
background-repeat: repeat-x;
}
Example
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
  7. background-repeat: repeat-x;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h1>c# corner</h1>
  13. <p>c-sharpcorner.com</p>
  14. </body>
  15. </html>
Output
background-repeat in y-direction
Repeat image in x (vertically), using background-repeat:repeat-y property.
Syntax
selected_element
{
background-image: url("image_url");
background-repeat: repeat-y;
}
Example
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
  7. background-repeat: repeat-y;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h1>c# corner</h1>
  13. <p>c-sharpcorner.com</p>
  14. </body>
  15. </html>
Output
Background image- no-repeat
In CSS background-repeat:no-repeat is used to off the repeat of an image.
Syntax
selected_element
{
background-image: url("image_url");
background-repeat: no-repeat;
}
Example
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
  7. background-repeat: no-repeat;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h1>c# corner</h1>
  13. <p>c-sharpcorner.com</p>
  14. </body>
  15. </html>
Output
background-position property
In CSS, the background-position property is used to set the position of an image.
Syntax
selected_element
{
background-image: url("image_url");
background-repeat: no-repeat;
background-position: right top;
}
Example
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
  7. background-repeat: no-repeat;
  8. background-position: right top;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <h1>c# corner</h1>
  14. <p>c-sharpcorner.com</p>
  15. </body>
  16. </html>
Output

Conclusion

In this article, we studied CSS - Background, And Colors.