css introduction cascading style sheets provides a great deal of control over the presentation of...

47
CSS Introduction • Cascading Style Sheets • Provides a great deal of control over the presentation of the document • HTML indicates both semantics of a document and its presentation - semantics content - presentation style how it appear on the web page

Upload: isaac-tucker

Post on 11-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

CSS Introduction

• Cascading Style Sheets

• Provides a great deal of control over the presentation of the document

• HTML indicates both semantics of a document and its presentation

- semantics content

- presentation style how it appear on the web page

Page 2: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Advantages of Style Sheets

• Saves time• Easy to change• Keep consistency• Give you more control over layout• Use styles with JavaScript => DHTML• Make it easy to create a common format for all

the Web pages

Page 3: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Media attribute in link tag

• <link rel="stylesheet" type="text/css" href="style1.css" media = “screen, tv,projection” title = Style 1" />

• <link rel="stylesheet" type="text/css" href="style1.css" media= “handheld, print” title = Style 1" />

Page 4: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Applying a single style sheet to multiple documents

Page 5: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Basic Structure of a Style

• Each definition contains:– A property– A colon– A value– A semicolon to separate two or more

values– Can include one or more values

• h1 {font-size:12pt; color:red}• h1 is the selector

Page 6: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Style Precedence

1. External style sheet

2. Embedded styles

3. Inline styles

Page 7: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Three Style Types

• Inline styles– Add styles to each tag within the HTML

file– Use it when you need to format just a

single section in a web page

• Example– <h1 style=“color:red; font-family: sans-

sarif”>IU</h1>

Page 8: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Three Style Types

• Embedded or internal styles– A style is applied to the entire HTML file– Use it when you need to modify all

instances of particular element (e.g., h1) in a web page

• Example– <style>

• h1 {color:red; font-family:sans-serif}

– </style>

Page 9: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Creating an Embedded Style

<head><title>Embedded Example</title><style> (default is “text/css”)

Style declarations</style></head>• A style declaration:

– Selector {attribute1:value1; attribute2:value2; …}

– Selector = an element in a document (e.g., a header or paragraph)

Page 10: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

An Example of an embedded style

<head>

<title>Getting Started</title>

<style type=“text/css”>

h1 {font-family: sans-serif; color: organge}

</style>

</head>

Page 11: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Three Style Types

• External style sheets– An external style sheet is a text file containing

the style definition (declaration)– Use it when you need to control the style for an

entire web site

• Example– h1, h2, h3, h4, h5, h6 {color:red; font-

family:sans-serif}– Save this in a new document using a .css

extension

Page 12: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Creating an External Style Sheet

• Open a new blank document in Notepad

• Type style declarations– h1 {color:red; font-family:sans-serif;}

• Do not include <style> tags

• Save the document as filename.css

Page 13: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Linking to Style Sheets 1

• Open an HTML file• Between <head> and </head> add

– <link href=URL rel=“relation_type” type=“link_type”>• URL is the file.css• Relation_type=“stylesheet”• Link_type=“text/css”

• Save this file and the .css file in the same web server directory

Page 14: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

An example of an external style sheet with an original html file

<head>

<title>Getting Started</title>

<link href=“scraps.css” rel=“stylesheet” type=“text/css” />

</head>

h1 {font-family: sans-serif; color: orange}

b {color: blue}

html file

Text file of css named “stylesheet”

Page 15: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Style Sheet Strategies

• Wherever possible, place your styles in external style sheets

• Take advantage of the power of CSS to have control over an entire Web site

Page 16: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Selector Strings

• Type selector - simply the name of the element type- separated by commas- eg. h1,h2,h3 {color:red; font-family:sans-serif;}

• Universal selector- denoted by asterisk *- * { font-weight:bold } for every element in the document

• ID selector

Page 17: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Using IDs and Classes

• Use an id to distinguish something, like a paragraph, from the others in a document.– For example, to identify a paragraph as

“head”, use the code:

<p id=“head”>… </p>

Page 18: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Working With Ids

• To create an ID for a specific tag, use the property:– <tag ID=id_name>

• To apply a style to a specific ID, use:– #id_name {style attributes and values}

Page 19: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Classes

• HTML and XHTML require each id be unique– therefore an id value can only be used once in a document.

• You can mark a group of elements with a common identifier using the class attribute.

<element class=“class”> … </element>

Page 20: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Applying a style to a class

Page 21: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Working With Classes

• To create a class, enter the following in the HTML tag:– <tag CLASS=class_name>– <h1 CLASS=FirstHeader>IU</h1>– class_name is a name to identify this class of

tags

• To apply a style to a class of tags, use:– tag.class_name {style attributes} or– .class_name {style attributes}

Page 22: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Working With Classes and Ids

• The difference between the Class property and the ID property is that the value of the ID property must be unique: – you can’t have more than one tag with the

same ID value– You can apply the same Class value to

multiple document tags

Page 23: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Pseudo Classes

• Pseudo classes are used to add special effects to some selectors

• Syntaxselector:pseudo-class {property:value;}

• Example• a:link {color:#FF0000;}      /* unvisited link */

a:visited {color:#00FF00;}  /* visited link */a:hover {color:#FF00FF;}  /* mouse over link */a:active {color:#0000FF;}  /* selected link */

• Eg Pseudoclass.html

Page 24: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

At-rules

• This rule is used to input one style sheet into another one.

• Eg

• @import url{ “gen-rules.css”}

h1, h2 {background-color:aqua}

Page 25: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Rule cascading

• Rule cascading a multistage sorting process that selects a single declaration that will supply the property value.

• Steps:

1.Which external or embedded style sheets apply to the document ( external includes alternate as well as media type)

2.Origin and weight with every declaration of property of element

Page 26: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Rule cascading

• Origin who wrote the declaration

* Author :person who wrote the HTML doc (or)

* User Agent: A browser / other user agent who define style property values

* User: User can indicate style preferences

• Weight normal or !important

Page 27: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Rule Cascading

• Priority

1.Important with user origin

2.Important with author origin

3.Normal with author

4.Normal with user

5.Any declaration with user agent

Page 28: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Rule cascading

• Specificity Priority

1.ID selectors

2.Class and pseudo-class selectors

3.Type selectors

4.Universal selectors

Eg rule cascdng.html

Page 29: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Style Inheritance

• Inheritance properties are passed from parent to child elements

• properties have not been explicitly defined by other means. Certain properties are inherited automatically,

• a child element will take on the characteristics of its parent with regards to these properties.

Page 30: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Working With DIV

• <DIV> tag is used for blocks of text, e.g., paragraphs, block quotes, headers, or lists

• To create a container for block-level elements, use:– <DIV CLASS=class_name>

• Block-level elements

– </DIV>– Class_name is the name of the class– You can substitute the ID proper for the Class

property (with ID, the syntax for CSS style, #id_name {style attributes and values}

Page 31: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Working With <DIV>

DIV.Slogan {font-weigh:bold}

<DIV CLASS=Slogan>Maxwell Scientific’s new Slogan is:<BR>”We teach science”</DIV>

style

HTML code

Maxwell…:“We

teach…Resultingtext

Page 32: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Working With <span>

• With the <span> tag, you can use inline elements, e.g., <B>, <I>

• To create a container for inline elements, use:– <span CLASS=class_name>

• inline elements

– </span>

Page 33: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

CSS for Page Layout

• CSS manipulates the size and location of block-level elements

• Block-level elements in HTML:– Heading tags, e.g., <H1>, <H2>– <p>– <blockquote> and <address> tags– List tags, e.g., <ul>, <ol>, <dl>– <div>– <body>– <img>

Page 34: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

CSS Box Model

• Parts of the block-level elements:– Margin– Border– Padding

Page 35: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

CSS Box Layout

I appreciate the prompt delivery of the pack of star disks.

bordermarginpadding

Page 36: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Controlling the Margins

• To define the margins of an element, use:– margin:value– where value = a length value (“em” is often

used), a percentage (a margin proportional to the element’s width, or auto

Page 37: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Controlling the Margins

• To set margins on a side, use:– margin-top– margin-right– margin-bottom– margin-left

• E.g., LI {margin-top: 15px; margin-right: 45px; margin-left: 45px ; margin-bottom: 30px;} (or)Margin: 15px 45px 30px

Page 38: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Number of values

• one assign to all ( top, right, bottom & left)

• Two first value assign to top & bottom, second value to right & left

• Three first to top , second to right & left, third to bottom.

Page 39: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Setting the Padding Size

• To define padding, use:– padding: value– where value = a length value or a

percentage (a padding proportional to the element’s width)

Page 40: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Setting the Padding Size

• To set margins on a side, use:– padding-top– padding-right– padding-bottom– padding-left

Page 41: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Formatting the Border

• Border can be set in three ways:– border-width– border-style– border-color

Page 42: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Formatting the Border

• To set the border, use:– border:width_value style color

• To set borders on a side, use:– border-top– border-bottom– border-left– border-right

Page 43: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Formatting Width & Height of Block-Level Boxes

• To set the width of a block-level element, use:– width:value– height:value– where value can be a length value, a percentage, or

auto

• E.g., textarea {width:225px; height:100px}

Page 44: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Simple Inline Boxes

• Eg

• <span style= “font-size : 36pt”> BIG </span>

Page 45: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Beyond the Normal Flow

• Three alternatives1. relative positioning position is altered relative to its normal flow position2. float positioning inline element moves to one side or the other of its line box3. absolute positioning element is moved entirely from the normal and placed somewhere else.

Page 46: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Properties for positioning

• position: static indicates the normal flow• position : relative / absolute / fixed• Fixed positioned elements can overlap

other elements.• float ( static or relative pos): none/right /

left • an element can be pushed to the left or

right, allowing other elements to wrap around it.

Page 47: CSS Introduction Cascading Style Sheets Provides a great deal of control over the presentation of the document HTML indicates both semantics of a document

Using the Float Attribute

float:rightwidth:50px

float:right width:50px clear:right