How to apply colors and background colors to your websites with CSS ?
You can use one or more of this property :
color
background-color
background-image
background-repeat
background-attachment
background-position
Color ?
Example, we want all headlines in a website to be dark red. The headlines are all marked with the HTML element <h1>. Simply do this :
h1 {
color: #ff0000;
}
Colors can be entered as hexadecimal (#ff0000), or the names of the colors (”red”) or rgb-values (rgb(255,0,0)).
background-color ?
background-color property describes the background color of elements.
You can also apply background colors to other elements including headlines and text.
h1 {
color: #990000;
background-color: #FC9804;
}
Notice that we applied two properties to <h1> by dividing them by a semicolon (;).
background image ?
background-image is used to insert a background image.
insert the image of the butterfly as a background image for a web page, simply apply the background-image property to <body> and specify the location of the image.
body {
background-color: #FFCC66;
background-image: url(”background.png”);
}
NB: Notice how we specified the location of the image as url(”background.png”). This means that the image is located in the same folder as the style sheet. You can also refer to images in other folders using url(”./images/background.png”).
background image : repeat ?
You can use different values for background-repeat below :
background-repeat: repeat-x : The image is repeated horizontally
background-repeat: repeat-y : The image is repeated vertically
background-repeat: repeat : The image is repeated both horizontally and vertically
background-repeat: no-repeat : The image is not repeated
For example, to avoid repetition of a background image the code should look like this:
body {
background-color: #FFCC66;
background-image: url(”background.png”);
background-repeat: no-repeat;
}
background image : attachment?
The property background-attachment specifies whether a background picture is fixed or scrolls along with the containing element.
A fixed background image will not move with the text when a reader is scrolling the page, whereas an unlocked background image will scroll along with the text of the web page.
Background-attachment: scroll : The image scrolls with the page - unlocked
Background-attachment: fixed : The image is locked
For example, the code below will fix the background image.
body {
background-color: #FFCC66;
background-image: url(”background.png”);
background-repeat: no-repeat;
background-attachment: fixed;
}
background image : position?
By default, a background image will be positioned in the top left corner of the screen. The property background-position allows you to change this default and position the background image anywhere you like on the screen. For example, the value ‘50px 70px’ positions the background image 50px from the left side and 70px from the top of the browser window.
The coordinates can be indicated as percentages of the browser window, fixed units (pixels, centimetres, etc.) or you can use the words top, bottom, center, left and right.
Example :
background-position: 2cm 2cm = The image is positioned 2 cm from the left and 2 cm down the page
background-position: 50% 25% = The image is centrally positioned and one fourth down the page
background-position: top right = The image is positioned in the top-right corner of the page
Example code = the background image in the bottom right corner:
body {
background-color: #FFCC66;
background-image: url(”background.png”);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: right bottom;
}
background : Compressing method
You can compress several properties and thereby write your style sheet in a shorter way, the list of order is describe below :
[background-color] | [background-image] | [background-repeat] | [background-attachment] | [background-position]
So the same result can be achieved in just one line of code:
background: #FFCC66 url(”background.png”) no-repeat fixed right bottom;
