css netcentric. what is css o css stands for cascading style sheets o styles define how to display...

53
CSS Netcentric

Upload: daisy-mcdowell

Post on 29-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSSNetcentric

Page 2: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

What is CSSO CSS stands for Cascading Style SheetsO Styles define how to display HTML

elementsO Styles were added to HTML 4.0 to

solve a problemO External Style Sheets can save a lot

of workO External Style Sheets are stored in

CSS files

Page 3: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Styles Solved a Big Problem

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

O HTML was intended to define the content of a document, like:O <h1>This is a heading</h1>O <p>This is a paragraph.</p>

O When tags like <font>, and color attributes were added to the HTML 3.2 specification, it started a nightmare for web developers.

Page 4: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Styles Solved a Big Problem

O Development of large web sites, where fonts and color information were added to every single page, became a long and expensive process.

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

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

O All browsers support CSS today.

Page 5: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS SyntaxO A CSS rule has two main parts: a

selector, and one or more declarations:

Page 6: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Syntax

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

O Each declaration consists of a property and a value.

O The property is the style attribute you want to change. Each property has a value.

Page 7: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS SyntaxO A CSS declaration always ends with a

semicolon, and declaration groups are surrounded by curly brackets:

p {color:red;text-align:center;}

O To make the CSS more readable, you can put one declaration on each line, like this:

p{color:red;text-align:center;}

Page 8: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Id and ClassO In addition to setting a style for a

HTML element, CSS allows you to specify your own selectors called "id" and "class"

Page 9: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Id SelectorO The id selector is used to specify a style

for a single, unique element.O The id selector uses the id attribute of

the HTML element, and is defined with a "#".

O The style rule below will be applied to the element with id="para1":

#para1{text-align:center;color:red;}

Page 10: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Class SelectorO 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.

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

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

O In the example below, all HTML elements with class="center" will be center-aligned:

.center {text-align:center;}

Page 11: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Class SelectorO You can also specify that only

specific HTML elements should be affected by a class.

O In the example below, all p elements with class="center" will be center-aligned:

p.center {text-align:center;}

Page 12: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Grouping SelectorO In style sheets there are often elements

with the same style.h1{color:green;}h2{color:green;}p{color:green;}

Page 13: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Grouping SelectorO To minimize the code, you can group

selectors.O Separate each selector with a comma.O In the example below we have

grouped the selectors from the code above:

h1,h2,p{color:green;}

Page 14: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Nesting SelectorO It is possible to apply a style for a

selector within a selector.O In the example below, one style is

specified for all p elements, one style is specified for all elements with class="marked", and a third style is specified only for p elements within elements with class="marked":

Page 15: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Nesting Selectorp{color:blue;text-align:center;}.marked{background-color:red;}.marked p{color:white;}

Page 16: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Inserting CSSO 3 ways

O External style sheetO Internal style sheetO Inline style

Page 17: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

External Style SheetO An external style sheet is ideal when

the style is applied to many pages. O With an external style sheet, you can

change the look of an entire Web site by changing one file.

O Each page must link to the style sheet using the <link> tag.

O The <link> tag goes inside the head section:

Page 18: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

External Style Sheet

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

Page 19: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

External Style SheetO An external style sheet can be written in any text

editor. O The file should not contain any html tags. O Your style sheet should be saved with a .css extension. O An example of a style sheet file is shown below:

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

!!! Do not add a space between the property value and the unit (such as margin-left:20 px). The correct way is: margin-left:20px

Page 20: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Internal Style SheetO An internal style sheet should be used when a

single document has a unique style. O 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/background.gif");}</style></head>

Page 21: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Inline Style SheetO An inline style loses many of the advantages of style

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

O To use inline styles you use the style attribute in the relevant tag.

O The style attribute can contain any CSS property. O 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 22: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Multiple Style SheetsO If some properties have been set for the same selector

in different style sheets, the values will be inherited from the more specific style sheet. 

O For example, if both external style and internal style sheets has properties for h3 selector, then internal style sheet will be used.

Page 23: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Multiple Style Sheets – Cascading Order

O Styles can be specified inside an HTML element, inside the head section of an HTML page and in an external CSS file

O Even multiple external style sheets can be referenced inside a single HTML document.

O Which one override others?O Browser defaultO External style sheetO Internal style sheet (in the head section)O Inline style (inside an HTML element)

O Number 4 has the highest priorityO So, an inline style (inside an HTML element) has the

highest priority, which means that it will override a style defined inside the <head> tag, or in an external style sheet, or in a browser (a default value).

Page 24: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS in ActionO CSS BackgroundO CSS TextO CSS FontsO CSS LinksO CSS ListsO CSS Tables

Page 25: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Box ModelO All HTML elements can be considered as

boxes. O In CSS, the term "box model" is used

when talking about design and layout.O The CSS box model is essentially a box

that wraps around HTML elements, and it consists of: margins, borders, padding, and the actual content.

O The box model allows us to place a border around elements and space elements in relation to other elements.

Page 26: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Box Model

Page 27: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Box ModelO Margin - Clears an area around the border.

The margin does not have a background color, it is completely transparent

O Border - A border that goes around the padding and content. The border is inherited from the color property of the box

O Padding - Clears an area around the content. The padding is affected by the background color of the box

O Content - The content of the box, where text and images appear

O In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.

Page 28: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Width and Height of an Element

O Important: When you set the width and height properties of an element with CSS, you just set the width and height of the content area.

O To calculate the full size of an element, you must also add the padding, borders and margins.

Page 29: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Width and Height of an Element

O The total width of the element in the example below is 300px:O width:250px;

padding:10px;border:5px solid gray;margin:10px;

O Let's do the math:250px (width)+ 20px (left + right padding)+ 10px (left + right border)+ 20px (left + right margin)= 300px

Page 30: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Width and Height of an Element

O Assume that you had only 250px of space. Let's make an element with a total width of 250px:

O width:220px;padding:10px;border:5px solid gray;margin:0px;

Page 31: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

Width and Height of an Element

O The total width of an element should be calculated like this:

O Total element width = width + left padding + right padding + left border + right border + left margin + right margin

O The total height of an element should be calculated like this:

O Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin

Page 32: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS BorderO The CSS border properties allow you to specify the

style and color of an element's border

O None of the border properties will have ANY effect unless the border-style property is set!O e.g. none, dotted, dashed, solid, double, etc.

O The border-width property is used to set the width of the border.

O The width is set in pixels, or by using one of the three pre-defined values: thin, medium, or thick.O Note: The "border-width" property does not work if it is

used alone. Use the "border-style" property to set the borders first.

Page 33: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Borderp.one{border-style:solid;border-width:5px;}p.two{border-style:solid;border-width:medium;}

Page 34: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS BorderO Try yourself Border Color and Border

Individual sidesO Look also CSS Outline, Margin and

Padding

Page 35: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Display (visibility and display)

O The display property specifies if/how an element is displayed, and the visibility property specifies if an element should be visible or hidden.

O Hiding an element can be done by setting the display property to "none" or the visibility property to "hidden". However, notice that these two methods produce different results:

Page 36: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Display (visibility and display)

O visibility:hidden hides an element, but it will still take up the same space as before. O The element will be hidden, but still

affect the layout.

O display:none hides an element, and it will not take up any space. O The element will be hidden, and the page

will be displayed as if the element is not there:

Page 37: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Display (visibility and display)

O visibility:hidden hides an element, but it will still take up the same space as before. O The element will be hidden, but still

affect the layout.

O display:none hides an element, and it will not take up any space. O The element will be hidden, and the page

will be displayed as if the element is not there:

Page 38: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Display (Block and Inline Elements)

O A block element is an element that takes up the full width available, and has a line break before and after it.

O Examples of block elements:<h1><p><div>

O An inline element only takes up as much width as necessary, and does not force line breaks.

O Examples of inline elements:<span><a>

Page 39: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS PositioningO The CSS positioning properties allow you to

position an element. O It can also place an element behind another,

and specify what should happen when an element's content is too big.

O Elements can be positioned using the top, bottom, left, and right properties.

O However, these properties will not work unless the position property is set first.

O They also work differently depending on the positioning method.

O There are four different positioning methods.O Static, fixed, relative, absolute

Page 40: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS PositioningO Static Positioning

O HTML elements are positioned static by default.

O A static positioned element is always positioned according to the normal flow of the page.

O Static positioned elements are not affected by the top, bottom, left, and right properties.

Page 41: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS PositioningO Fixed Positioning

O An element with fixed position is positioned relative to the browser window.

O It will not move even if the window is scrolledO fixed elements are like a watermark.

Everything else on the page will then scroll past that element.

O Fixed positioned elements are removed from the normal flow.

O Fixed positioned elements can overlap other elements.

Page 42: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Positioning<style>p.pos_fixed{position:fixed;top:30px;right:5px;}</style></head><body><p class="pos_fixed">Some more text</p><p><b>Note:</b> IE7 and IE8 supports the fixed value only if a !DOCTYPE is specified.</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p></body>

Page 43: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS PositioningO Relative Positioning

O A relative positioned element is positioned relative to its normal position.

O it starts from where the element would be if it were still in the normal flow (offset based on it’s current location).

O The content of relatively positioned elements can be moved and overlap other elements

Page 44: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Positioning<style>h2.pos_left{position:relative;left:-20px;}

h2.pos_right{position:relative;left:20px;}</style>

<body><h2>This is a heading with no position</h2><h2 class="pos_left">This heading is moved left according to its normal position</h2><h2 class="pos_right">This heading is moved right according to its normal position</h2><p>Relative positioning moves an element RELATIVE to its original position.</p><p>The style "left:-20px" subtracts 20 pixels from the element's original left position.</p><p>The style "left:20px" adds 20 pixels to the element's original left position.</p></body>

Page 45: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS PositioningO Absolute Positioning

O tells the browser that whatever is going to be positioned should be removed from the normal flow of the document and will be placed in an exact location on the page.

O It is also taken out of the normal flow of the document - it won't affect how the elements before it or after it in the HTML are positioned on the Web page.

O The content of relatively positioned elements can be moved and overlap other elements

Page 46: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS PositioningO Overlapping Elements

O When elements are positioned outside the normal flow, they can overlap other elements.

O The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others).

O An element can have a positive or negative stack order:

Page 47: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS PositioningO Overlapping Elements

<style>img{position:absolute;left:0px;top:0px;z-index:-1;}</style>

Page 48: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS FloatingO With CSS float, an element can be

pushed to the left or right, allowing other elements to wrap around it.

O Float is very often used for images, but it is also useful when working with layouts.

O Elements are floated horizontally, this means that an element can only be floated left or right, not up or down.

Page 49: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS FloatingO The elements after the floating

element will flow around it.

O The elements before the floating element will not be affected.

O If an image is floated to the right, a following text flows around it, to the left:

Page 50: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Floating

img{float:right;}

Page 51: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS FloatingO Elements after the floating element

will flow around it. O To avoid this, use the clear property.O The clear property specifies which

sides of an element other floating elements are not allowed.

O Add a text line into the image gallery, using the clear property:

Page 52: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

CSS Floating

.text_line{clear:both;margin-bottom:2px;}

Page 53: CSS Netcentric. What is CSS O CSS stands for Cascading Style Sheets O Styles define how to display HTML elements O Styles were added to HTML 4.0 to solve

ExerciseO Create a navigation bar using CSS

O Home | News | Contact | About O as text

O Home | News | Contact | About O as image