Вы находитесь на странице: 1из 3

4/4/13

HTML Styles

HTML Styles - CSS


Previous
CSS (Cascading Style Sheets) is used to style HTML elements.

Next Chapter

Look! Styles and colors


M a n i p u l a t e C o l o r s , and more... T e x t B o x e s

Try it yourself

Try it Yourself - Examples


Using styles in HTML How to add style information inside the <head> section. Link that is not underlined How to make a link that is not underlined, with the style attribute. Link to an external style sheet How to use the <link> tag to link to an external style sheet.

Styling HTML with CSS


CSS was introduced together with HTML 4, to provide a better way to style HTML elements. CSS can be added to HTML in the following ways: Inline - using the style attribute in HTML elements Internal - using the <style> element in the <head> section External - using an external CSS file The preferred way to add CSS to HTML, is to put CSS syntax in separate CSS files. However, in this HTML tutorial we will introduce you to CSS using the style attribute. This is done to simplify the examples. It also makes it easier for you to edit the code and try it yourself. You can learn everything about CSS in our CSS Tutorial.

Inline Styles
An inline style can be used if a unique style is to be applied to one single occurrence of an element. To use inline styles, use the style attribute in the relevant tag. The style attribute can contain any CSS property. The example below shows how to change the text color and the left margin of a paragraph:
w3schools.com/html/html_css.asp 1/3

4/4/13

HTML Styles

< ps t y l e = " c o l o r : b l u e ; m a r g i n l e f t : 2 0 p x ; " > T h i si sap a r a g r a p h . < / p >


To learn more about style sheets, visit our CSS tutorial.

HTML Style Example - Background Color


The background-color property defines the background color for an element:

Example
< ! D O C T Y P Eh t m l > < h t m l > < b o d ys t y l e = " b a c k g r o u n d c o l o r : y e l l o w ; " > < h 2s t y l e = " b a c k g r o u n d c o l o r : r e d ; " > T h i si sah e a d i n g < / h 2 > < ps t y l e = " b a c k g r o u n d c o l o r : g r e e n ; " > T h i si sap a r a g r a p h . < / p > < / b o d y > < / h t m l >
Try it yourself The background-color property makes the "old" bgcolor attribute obsolete. Try it yourself: Background color the old way

HTML Style Example - Font, Color and Size


The font-family, color, and font-size properties defines the font, color, and size of the text in an element:

Example
< ! D O C T Y P Eh t m l > < h t m l > < b o d y > < h 1s t y l e = " f o n t f a m i l y : v e r d a n a ; " > Ah e a d i n g < / h 1 > < ps t y l e = " f o n t f a m i l y : a r i a l ; c o l o r : r e d ; f o n t s i z e : 2 0 p x ; " > Ap a r a g r a p h . < / p > < / b o d y > < / h t m l >
Try it yourself The font-family, color, and font-size properties make the old <font> tag obsolete.

HTML Style Example - Text Alignment


The text-align property specifies the horizontal alignment of text in an element:

Example
< ! D O C T Y P Eh t m l > < h t m l >
w3schools.com/html/html_css.asp 2/3

4/4/13

HTML Styles

< b o d y > < h 1s t y l e = " t e x t a l i g n : c e n t e r ; " > C e n t e r a l i g n e dh e a d i n g < / h 1 > < p > T h i si sap a r a g r a p h . < / p > < / b o d y > < / h t m l >
Try it yourself The text-align property makes the old <center> tag obsolete. Try it yourself: Centered heading the old way

Internal Style Sheet


An internal style sheet can be used if one single document has a unique style. Internal styles are defined in the <head> section of an HTML page, by using the <style> tag, like this:

< h e a d > < s t y l et y p e = " t e x t / c s s " > b o d y{ b a c k g r o u n d c o l o r : y e l l o w ; } p{ c o l o r : b l u e ; } < / s t y l e > < / h e a d >

External Style Sheet


An external style sheet is ideal when the style is applied to many pages. With an external style sheet, you can change the look of an entire Web site by changing one file. Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the <head> section:

< h e a d > < l i n kr e l = " s t y l e s h e e t "t y p e = " t e x t / c s s "h r e f = " m y s t y l e . c s s " > < / h e a d >

HTML Style Tags


Tag <style> <link> Description Defines style information for a document Defines the relationship between a document and an external resource

Deprecated Tags and Attributes


In HTML 4, several tags and attributes were used to style documents. These tags are not supported in newer versions of HTML. Avoid using the elements: <font>, <center>, and <strike>, and the attributes: color and bgcolor.

Previous
w3schools.com/html/html_css.asp

Next Chapter
3/3