29
DIV, SPAN, CSS

DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

Embed Size (px)

Citation preview

Page 1: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

DIV, SPAN, CSS

Page 2: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

DIV• The <div> tag defines a division or a section in an HTML

document.• The <div> tag is used to group block-elements to format

them with CSS.• Tip: The <div> element is very often used together with

CSS, to layout a web page.• Note: By default, browsers always place a line break

before and after the <div> element. However, this can be changed with CSS.

Page 3: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

DIV• <!DOCTYPE html>• <html>• <body>

• <p>This is some text.</p>

• <div style="color:#0000FF">• <h3>This is a heading in a div element</h3>• <p>This is some text in a div element.</p>• </div>

• <p>This is some text.</p>

• </body>• </html>

Page 4: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

SPAN• The <span> tag is used to group inline-elements in a

document.• The <span> tag provides no visual change by itself.• The <span> tag provides a way to add a hook to a part of

a text or a part of a document.

Page 5: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

SPAN• A <span> element used to color a part of a text: • <p>My mother has <span style="color:blue">blue</span>

eyes.</p>

Page 6: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

CSS• CSS stands for Cascading Style Sheets• Styles define how to display HTML elements• Styles were added to HTML 4.0 to solve a problem• External Style Sheets can save a lot of work• External Style Sheets are stored in CSS files

Page 7: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

• HTML was never intended to contain tags for formatting a document.

• HTML was intended to define the content of a document, like:

• <h1>This is a heading</h1>• <p>This is a paragraph.</p>• When tags like <font>, and color attributes were added to

the HTML 3.2 specification, it started a nightmare for web developers. Development of large web sites, where fonts and color information were added to every single page, became a long and expensive process.

• To solve this problem, the World Wide Web Consortium (W3C) created CSS.

• In HTML 4.0, all formatting could be removed from the HTML document, and stored in a separate CSS file.

• All browsers support CSS today.

Page 8: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

EXAMPLE• <html>• <head>• <style>• body {background-color:yellow;}• h1 {font-size:36pt;}• h2 {color:blue;}• p {margin-left:50px;}• </style>• </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>

Page 9: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

• A CSS rule has two main parts: a selector, and one or more declarations:

• The selector is normally the HTML element you want to style.

• Each declaration consists of a property and a value.• The property is the style attribute you want to change.

Each property has a value.

Page 10: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

CSS Example• A CSS declaration always ends with a semicolon, and

declaration groups are surrounded by curly brackets:• p {color:red;text-align:center;}

Page 11: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

• <!DOCTYPE html>• <html>• <head>• <style>• p• {• color:red;• text-align:center;• } • </style>• </head>

• <body>• <p>Hello World!</p>• <p>This paragraph is styled with CSS.</p>• </body>• </html>

Page 12: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

The id Selector• The id selector is used to specify a style for a single,

unique element.• The id selector uses the id attribute of the HTML element,

and is defined with a "#".• The style rule below will be applied to the element with

id="para1":

Page 13: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

• <!DOCTYPE html>• <html>• <head>• <style>• #para1• {• text-align:center;• color:red;• } • </style>• </head>

• <body>• <p id="para1">Hello World!</p>• <p>This paragraph is not affected by the style.</p>• </body>• </html>

Page 14: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

The Class Selector• The class selector is used to specify a style for a group of

elements. Unlike the id selector, the class selector is most often used on several elements.

• This allows you to set a particular style for many HTML elements with the same class.

• The class selector uses the HTML class attribute, and is defined with a "."

Page 15: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

• <!DOCTYPE html>• <html>• <head>• <style>• .center• {• text-align:center;• }• </style>• </head>

• <body>• <h1 class="center">Center-aligned heading</h1>• <p class="center">Center-aligned paragraph.</p> • </body>• </html>

Page 16: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

Three Ways to Insert CSS• There are three ways of inserting a style sheet:• External style sheet• Internal style sheet• Inline style

Page 17: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

External • 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:

• <head><link rel="stylesheet" type="text/css" href="mystyle.css"></head>

Page 18: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

External• 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;}body {background-image:url("images/back40.gif");}

Page 19: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

Internal• An internal style sheet should be used when a single

document has a unique style. You define internal styles in the head section of an HTML page, by using the <style> tag, like this:

• <head><style>hr {color:sienna;}p {margin-left:20px;}body {background-image:url("images/back40.gif");}</style></head>

Page 20: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

Inline• An inline style loses many of the advantages of style

sheets by mixing content with presentation. Use this method sparingly!

• To use inline styles you use the style attribute in the relevant tag. The style attribute can contain any CSS property. The example shows how to change the color and the left margin of a paragraph:

• <p style="color:sienna;margin-left:20px">This is a paragraph.</p>

Page 21: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

Background• CSS background properties are used to define the

background effects of an element.• CSS properties used for background effects:• background-color• background-image• background-repeat• background-attachment• background-position

Page 22: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

Background • Background Color• The background-color property specifies the background

color of an element.• The background color of a page is defined in the body

selector:

Page 23: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

Background• <!DOCTYPE html>• <html>• <head>• <style>• body• {• background-color:#b0c4de;• }• </style>• </head>

• <body>

• <h1>My CSS web page!</h1>• <p>Hello world! This is a W3Schools.com example.</p>

• </body>• </html>

Page 24: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

• <!DOCTYPE html>• <html>• <head>• <style>• h1• {• background-color:#6495ed;• }• p• {• background-color:#e0ffff;• }• div• {• background-color:#b0c4de;• }• </style>• </head>

• <body>

• <h1>CSS background-color example!</h1>

• <div>• This is a text inside a div

element.• <p>This paragraph has its

own background color.</p>• We are still in the div

element.• </div>

• </body>• </html>

Page 25: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

Background Image

• The background-image property specifies an image to use as the background of an element.

• By default, the image is repeated so it covers the entire element.

• <!DOCTYPE html>• <html>• <head>• <style>• body {background-

image:url('paper.gif');}• </style>• </head>

• <body>• <h1>Hello World!</h1>• </body>

• </html>

Page 26: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

Background-image• http://

w3schools.com/css/tryit.asp?filename=trycss_background-image_gradient1

• Background Image - Repeat Horizontally or Vertically• By default, the background-image property repeats an

image both horizontally and vertically.• Some images should be repeated only horizontally or

vertically, or they will look strange, http://w3schools.com/css/tryit.asp?filename=trycss_background-image_gradient2

Page 27: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

Background-Image• Set position and no-repeat•  When using a background image, use an image that

does not disturb the text.• Showing the image only once is specified by the

background-repeat property:• http://

w3schools.com/css/tryit.asp?filename=trycss_background-image_norepeat

• 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 background-position property:

Page 28: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

Background-Image• 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 background-position property:

• http://w3schools.com/css/tryit.asp?filename=trycss_background-image_position

Page 29: DIV, SPAN, CSS. DIV The tag defines a division or a section in an HTML document. The tag is used to group block-elements to format them with CSS. Tip:

All CSS Background Properties

Property Description

background Sets all the background properties in one declaration

background-attachment

Sets whether a background image is fixed or scrolls with the rest of the page

background-color Sets the background color of an element

background-image Sets the background image for an element

background-position

Sets the starting position of a background image

background-repeat Sets how a background image will be repeated