CSS CSS
CSS CSS
CSS CSS
What is CSS?
CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles are normally stored in Style Sheets External Style Sheets can save you a lot of work External Style Sheets are stored in CSS files Multiple style definitions will cascade into one
How to Insert a Style Sheet When a browser reads a style sheet it will sheet, format the document according to it. There are three ways of inserting a style sheet:
External Style Sheet Internal Style Sheet Multiple Style Sheets
The browser will read the style definitions from the file mystyle.css, mystyle css and format the document according to it. An external style sheet can be written in any text editor. The file should not contain any html tags. Your style sheet should be saved with a .css extension. An example of a style sheet file is shown below:
hr {color: sienna} p {margin-left: 20px} {marginbody {background-image: url("images/back40.gif")} {background-
h3 { texttext-align: right; fontfont-size: 20pt } If the page with the internal style sheet also links to the g y external style sheet the properties for h3 will be: color: red; texttext li t t-align: right; i ht fontfont-size: 20pt The color is inherited from the external style sheet and the text-alignment and the font-size is replaced by the textfontinternal style sheet.
CSS Syntax
The CSS syntax is made up of three parts:
a selector, l t a property and a value:
p {font-family: "sans serif"} {fontNote: If you wish to specify more than one property, you must separate each property with a semicolon. The example below shows how to define a center aligned paragraph, with a red text color:
p{ {text- g {text-align:center;color:red} ; }
To make the style definitions more readable, you y ,y can describe one property on each line, like this: p{textp{text-align: center;color: black;font-family: arial} black;font-
External Style Sheet - Example-1 ExampleThe HTML file be o links to a e te a sty e e e below s an external style sheet with the <link> tag:
<html> <head> <link rel="stylesheet" type text/css href="ex1.css" /> type="text/css" href ex1.css / </head> <body> <h1>This header is 36 pt</h1> <h2>This header is blue</h2> <p>This paragraph has a left margin of 50 pixels</p> </body> </html>
Example - 2
<html> <head> <link rel="stylesheet" type="text/css" li k l " l h " " / " href="ex2.css" /> </head> <body> <h1>This is a header 1</h1> <hr /> <p>You can see that the style sheet formats the text</p> <p><a href="http://www.google.com" target="_blank">This is a link</a></p> g p </body> </html>
<html> <head> <style t t l type="text/css"> type="text/css" "t t/ "> body { backgroundbackground-image: url('smiley.gif'); url('smiley.gif'); backgroundbackground-repeat: nono-repeat; backgroundbackground-attachment: fixed } </style> </head> <body> <p>The image will not scroll with the rest of the page</p> <p>The image will not scroll with the rest of the page</p> <p>The image will not scroll with the rest of the page</p> <p>The image will not scroll with the rest of th page</p> < >Th i ill t ll ith th t f the </ > </body>