CSS How To... |
||||||||||||||||
When a browser reads a style sheet, it will format the HTML document according to the information in the style sheet. Three Ways to Insert CSSThere are three ways of inserting a style sheet: External Style SheetWith an external style sheet, you can change the look of an entire website by changing just one file! Each page must include a reference to the external style sheet file inside the <link> element. The <link> element goes inside the <head> section: Example<head>
An external style sheet can be written in any text editor. The file should not contain any html tags. The style sheet file must be saved with a .css extension. Here is how the "mystyle.css" looks: body {
Note: Do not add a space between the property value and the unit (such as Internal Style SheetAn internal style sheet may be used if one single page has a unique style. Internal styles are defined within the <style> element, inside the <head> section of an HTML page: Example<head>
Inline StylesAn inline style may be used to apply a unique style for a single element. To use inline styles, add the style attribute to the relevant element. The style attribute can contain any CSS property. The example below shows how to change the color and the left margin of a <h1> element: Example<h1 style="color:blue;margin-left:30px;">This is a heading</h1>
CSS BackgroundsThe CSS background properties are used to define the background effects for elements. CSS background properties:
Background ColorThe The background color of a page is set like this: Examplebody {
With CSS, a color is most often specified by:
Look at CSS Color Values for a complete list of possible color values. In the example below, the <h1>, <p>, and <div> elements have different background colors: Exampleh1 {
Background ImageThe By default, the image is repeated so it covers the entire element. The background image for a page can be set like this: Examplebody {
Below is an example of a bad combination of text and background image. The text is hardly readable: Examplebody {
Note: When using a background image, use an image that does not disturb the text. Background Image - Repeat Horizontally or VerticallyBy default, the Some images should be repeated only horizontally or vertically, or they will look strange, like this: Examplebody {
If the image above is repeated only horizontally ( Examplebody {
Tip: To repeat an image vertically, set Background Image - Set position and no-repeatShowing the background image only once is also specified by the Examplebody {
In the example above, the background image is shown in the same place as the text. We want to change the position of the image, so that it does not disturb the text too much. The position of the image is specified by the Examplebody {
Background Image - Fixed positionTo specify that the background image should be fixed (will not scroll with the rest of the page), use the Examplebody {
Background - Shorthand propertyTo shorten the code, it is also possible to specify all the background properties in one single property. This is called a shorthand property. The shorthand property for background is Examplebody {
When using the shorthand property the order of the property values is:
It does not matter if one of the property values is missing, as long as the other ones are in this order. All CSS Background Properties
|