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.
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
- <!DOCTYPE html>
- <html>
-
- <head>
- <style>
- h1 {
- color: blue;
- }
-
- p {
- color: red;
- }
- </style>
- </head>
-
- <body>
- <h1>c# corner</h1>
- <p>c-sharpcorner.com</p>
- </body>
-
- </html>
Output
In CSS, we can set the background, like the following-
Set Background image of heading element
We can use the heading selector to set the background image of <h1>.
Example
- <!DOCTYPE html>
- <html>
-
- <head>
- <style>
- h1 {
- background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
- }
- </style>
- </head>
-
- <body>
- <h1>c# corner</h1>
- <p>c-sharpcorner.com</p>
- </body>
-
- </html>
To repeat the background of a body or an individual element, background-repeat is used.
{
background-image:url("url_image");
background-repeat;
}
Example
- <!DOCTYPE html>
- <html>
-
- <head>
- <style>
- body {
- background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
- backgound-repeat;
- }
- </style>
- </head>
-
- <body>
- <h1>c# corner</h1>
- <p>c-sharpcorner.com</p>
- </body>
-
- </html>
background-repeat in x-direction
Repeat image in x (horizontal), using background-repeat:repeat-x property
{
background-image: url("image_url");
background-repeat: repeat-x;
}
Example
- <!DOCTYPE html>
- <html>
-
- <head>
- <style>
- body {
- background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
- background-repeat: repeat-x;
- }
- </style>
- </head>
-
- <body>
- <h1>c# corner</h1>
- <p>c-sharpcorner.com</p>
- </body>
-
- </html>
background-repeat in y-direction
Repeat image in x (vertically), using background-repeat:repeat-y property.
{
background-image: url("image_url");
background-repeat: repeat-y;
}
Example
- <!DOCTYPE html>
- <html>
-
- <head>
- <style>
- body {
- background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
- background-repeat: repeat-y;
- }
- </style>
- </head>
-
- <body>
- <h1>c# corner</h1>
- <p>c-sharpcorner.com</p>
- </body>
-
- </html>
Background image- no-repeat
In CSS background-repeat:no-repeat
is used to off the repeat of an image.
{
background-image: url("image_url");
background-repeat: no-repeat;
}
Example
- <!DOCTYPE html>
- <html>
-
- <head>
- <style>
- body {
- background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
- background-repeat: no-repeat;
- }
- </style>
- </head>
-
- <body>
- <h1>c# corner</h1>
- <p>c-sharpcorner.com</p>
- </body>
-
- </html>
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
- <!DOCTYPE html>
- <html>
-
- <head>
- <style>
- body {
- background-image: url("http://csharpcorner.mindcrackerinc.netdna-cdn.com/App_Themes/CSharp/Images/SiteLogo.png");
- background-repeat: no-repeat;
- background-position: right top;
- }
- </style>
- </head>
-
- <body>
- <h1>c# corner</h1>
- <p>c-sharpcorner.com</p>
- </body>
-
- </html>
Output
In this article, we studied CSS - Background, And Colors.