61
1 Divs and Spans

Divs and Spans

  • Upload
    nitara

  • View
    88

  • Download
    2

Embed Size (px)

DESCRIPTION

Divs and Spans. Two new XHTML Elements and Provides supporting structures Structures are able to be styled Shortcuts available to make specifying properties easier. Lets you divide your webpage into logical sections or groupings - PowerPoint PPT Presentation

Citation preview

Page 1: Divs and Spans

1

Divs and Spans

Page 2: Divs and Spans

2

Two new XHTML Elements <div> and <span>

• Provides supporting structures

• Structures are able to be styled

• Shortcuts available to make specifying properties easier

Page 3: Divs and Spans

3

<div>

• Lets you divide your webpage into logical sections or groupings

• Adds structure to a webpage by dividing into logical sections

• Logical sections are a group of related elements on the page

• Logical sections could represent the main content areas

• Logical sections will be enclosed within a <div> element

Page 4: Divs and Spans

4

<div>

• Adding <div> opening and closing tags around the elements belonging to a logical section

• Nest elements in each grouping into a <div> element

• Nesting elements inside a <div> indicates that all those elements belong to the same group

Page 5: Divs and Spans

5

Adding id to <div>

• Adding an id to <div> enables you to label what the grouping means

• Using the id attribute provides a unique label for the <div>

• Give the cats <div> and id of cats and the dogs <div> an id of dogs

<div id=“cats”> </div><div id=“dogs”> </div>

Page 6: Divs and Spans

6

Adding style to div

<div id=“cats”> </div><div id=“dogs”> </div>

Creating two rules, one for each <div>

#cats {background-image: url(cats.jpg);

#dogs {background-image: url(dogs.jpg);

Page 7: Divs and Spans

7

<div>

• Each <div> is selected by an id selector

• Setting a background on the <div> will show through on the elements within that <div>

• Elements in the <div> will also inherit properties from the <div> just as any child element

Page 8: Divs and Spans

8

Benefits to adding more structure to pages

• Allows you to further expose the underlying logical structure of your pages

– helps viewers to better understand them– Helps in maintaining them

• Structure gives you a way to apply style to a section

Page 9: Divs and Spans

9

Adding a header and footer using div

<div id=“header”> </div>

<div id=“cats”> </div>

<div id=“dogs”> </div>

<div id=“footer”> </div>

Page 10: Divs and Spans

10

Adding structure onto structure

<div id=“header”> </div>

<div id=“pets”>

<div id=“cats”> </div>

<div id=“dogs”> </div>

</div>

<div id=“footer”> </div>

Page 11: Divs and Spans

11

Adding structure onto structure

• <div> acts like a container that you can put elements into to keep them all together

• Use <div> to add structure where it has real purpose

• Always keep structure as simple as possible

• Adding to much structure can overly complicate with no real benefit

Page 12: Divs and Spans

12

Adding id to <div>

• Adding an id to <div> enables you to label what the grouping means

• Using the id attribute provides a unique label for the <div>

• Give the cats <div> and id of cats and the dogs <div> an id of dogs

• Add a pets <div> around the cats and dogs <div> and give the pets <div> an id of pets

Page 13: Divs and Spans

13

Adding structure onto structure <body>

<div id=“header”> <h1>Cats and Dogs header info</h1></div><div id=“pets”>

<div id=“cats”><h2>Cats section </h2><p>Cats details</p> </div><div id=“dogs”><h2>Dogs section </h2><p>Dogs details</p> </div>

</div><div id=“footer”> <p>Footer info</p></div>

</body>

Page 14: Divs and Spans

14

Another example using elixirs…

<div id="elixirs"> <h2>Weekly Elixir Specials</h2> <p> <img src="images/yellow.gif" alt="Lemon Breeze Elixir" /> </p> <h3>Lemon Breeze</h3> <p> The ultimate healthy drink, combines herbal botanicals... </p> <p> <img src="images/chai.gif" alt="Chai Chiller Elixir" /> </p> <h3>Chai Chiller</h3> <p> Not your traditional chai... </p></div>

Page 15: Divs and Spans

15

Adding a border around the elixirs <div> element

• First, modify your CSS file selecting the elixirs <div> element using its id

• Add a thin solid border with a aquamarine hex color

#elixirs {

border-width: thin;

border-style: solid;

border-color: #007e7e;

}

Page 16: Divs and Spans

16

Adding a border around the elixirs <div> element

• Adds a border around the <div> element with the id “elixirs”

• Border goes around all the elements inside the <div> element

• Border goes around the content all the elements within the <div>

Page 17: Divs and Spans

17

Adding more style

We want to add the following to our elixer <div>

• Make the width narrower• Add padding• Add a background image• Adjust text alignment• Add text line heights• Add heading colors

Page 18: Divs and Spans

18

width: property

Specifing the width of an element

#elixirs {

border-width: thin;

border-style: solid;

border-color: #007e7e;

width: 200px;

}

Page 19: Divs and Spans

19

width: property

• Specifies the width of the element’s content area only• Setting width on the elixirs <div>• Content in the elixirs <div> will be 200 pixels wide• Any the elements nested in the <div> will fit inside this

width

#elixirs {

border-width: thin;

border-style: solid;

border-color: #007e7e;

width: 200px;

}

Page 20: Divs and Spans

20

Determining the entire width

• The width of the entire element would have to include content area + padding + border + margin

• All added together represents the width of the entire element

• If you have no padding, border, or margin but have defined the width of the <div> as 200, then the width of the entire box would also be 200 pixels

Page 21: Divs and Spans

21

Remember the box model

Content

Padding Content area text goes

Border

Margin

Page 22: Divs and Spans

22

Adding padding, margin, align, backgroundSpecifying additional elixir attributes

#elixirs {border-width: thin;border-style: solid;border-color: #007e7e;width: 200px;

padding-right: 20px;padding-bottom: 20px;padding-left: 20px;

margin-left: 20px;

text-align: center;text-align: center;

background-image: url(images/drink.gif);back-ground-repeat; repeat-x;}

Page 23: Divs and Spans

23

Adding padding, margin, align, background

• Adding a padding left, right, bottom 20 pixels

• Adding a left margin of 20 pixels

• Aligning all in the center

• Adding a background image, repeats only horizontally

Page 24: Divs and Spans

24

text-align: center;

• Text-align property will also affect the alignment of the images

• Doesn’t only align text• Aligns all inline content in a block element• By setting the property on the <div> block element all

inline content is nicely centered• Text-align works on any kind of inline element• Should be set on block elements only• Has no effect if used directly used on inline elements like

<img>

Page 25: Divs and Spans

25

text-align: center;

• All the text inside the <div> element is in nested block elements

• Block elements inherit the text-align property from the <div>

• Headings and paragraphs since they are block elements inherit the text-align value of “center” and then align their own content to center

• Not all properties are necessarily inherited

Page 26: Divs and Spans

26

Modifying headings only within the elixir <div>

Decendant selectors

Specifying a selector for example

“select an <h2> element, but only if it’s inside an elixirs <div> ”

Page 27: Divs and Spans

27

A way to select descendants

Selecting elements that descend from certain elements

Specifying a selector…

div h2 {

color: black;

}

…select an <h2> element, but only if it’s inside an elixirs <div>

Page 28: Divs and Spans

28

A way to select descendants

Select elements that descend from certain elements• Div parent element• h2 descendant name• Rule says to select any <h2> that is a descendant of a

<div>• Rule selects the h2 within the <div> elixir

div h2 {

color: black;

}

Page 29: Divs and Spans

29

A way to select descendants

Problems with specifying by <div>

Applies styling to all <div> with a h2 element

div h2 {

color: black;

}

Page 30: Divs and Spans

30

A way to select descendants

Adding an id to the <div>

#elixirs h2 {

color: black;

}

Page 31: Divs and Spans

31

Ways to select descendants specifically

Adding an id to the <div>

• Selects any <h2> that is a descendant of an element with the id “elixirs”

• Parent element is the element with the id elixirs

• Any descendant of elixirs that is <h2 >

#elixirs h2 {color: black;

}

Page 32: Divs and Spans

32

More ways to select descendants

To specify an <h2> that is the child of a <blockquote> within elixirs

Selects and <h2> element that descend from <blockqoute> that descend from an element with an id of “elixirs”

#elixirs blockqoute h2 {

color: black;

}

Page 33: Divs and Spans

33

Example

• Setting the <h2> heading to black within the “elixirs”• Setting the <h3> headings to red within the “elixirs”• Using the descendant selectors to target just the <h2>

and <h3> elements in the elixirs <div>

#elixirs h2 {color: black;

}

#elixirs h3 {color: red;

}

Page 34: Divs and Spans

34

Example

• Adds black and red headings in the elixirs section• Doesn’t effect color being used for <h2> headings in

the rest of the main page

#elixirs h2 {

color: black;

}

#elixirs h3 {

color: red;

}

Page 35: Divs and Spans

35

Setting the line height

• Adding the line-height property on the entire <div> will allow it be inherited by all elements

• Line height is 1 times its inherited font size

#elixirs {Line-height: 1em;

}

• If you want to modify line-height more specifically than use a number

#elixirs {Line-height: 1;

}

Page 36: Divs and Spans

36

Setting the line height

• Using a number• Adding a line height of 1 to the elixirs <div> changes the

line height of each element in it• Each element within the elixirs <div> to have a line-height

of 1 times its own font-size• Rather than the font-size of the elixirs <div>

#elixirs {

line-height: 1;

}

Page 37: Divs and Spans

37

Shortcuts for setting the different property values on the background of an element

Padding old way

padding-top: 0px;

padding-right: 20px;

padding-bottom: 30px;

padding-left: 10px;

Page 38: Divs and Spans

38

Shortcuts for setting the different property values on the background of an element

Padding new way

padding: 0px 20px 30px 10px;

Page 39: Divs and Spans

39

Shortcuts for setting the different property values on the background of an element

Padding shorthand for all sides 20 px

padding: 20px;

Page 40: Divs and Spans

40

Shortcuts for setting the different property values on the background of an element

Margins old way

margin-top: 0px;

margin-right: 20px;

margin-bottom: 30px;

margin-left: 10px;

Page 41: Divs and Spans

41

Shortcuts for setting the different property values on the background of an element

Margins new way

margin: 0px 20px 30px 10px;

Page 42: Divs and Spans

42

Shortcuts for setting the different property values on the background of an element

Margins shorthand for all sides 20 px

margin: 20px;

Page 43: Divs and Spans

43

Shortcuts for setting the different property values on the background of an element

Margins new way

top and bottom 0px and left and right 20px

margin: 0px 20px;

Page 44: Divs and Spans

44

Shortcuts for setting the different property values on the background of an element

border old way

border-width: thin;

border-style: solid;

border-color: #007e7e;

Page 45: Divs and Spans

45

Shortcuts for setting the different property values on the background of an element

border new way

border: thin solid #007e7e;

Page 46: Divs and Spans

46

Additional border shorthand examples

border: solid thin;

border: solid thin #007e7e ;

border: #007e7e solid;

border: #007e7e solid thin;

border: solid;

Page 47: Divs and Spans

47

Background shorthand examples

background old way

background-color: white;

background-image: url(images/drink.gif);

back-ground-repeat; repeat-x;

Page 48: Divs and Spans

48

Background shorthand examples

background new way

background-color: white url(images/drink.gif) repeat-x;

Page 49: Divs and Spans

49

Background shorthand examples

Font old way

Font-size: small;

Font-family: verdana, helvetica, sans-serif;

Line-height: 1.6em;

Page 50: Divs and Spans

50

Background shorthand examples

Font new way

Font: small/1.6em verdana, helvetica, sans-serif;

Page 51: Divs and Spans

51

Adding <span>

Song and Artist shown below need separate styling

<ul>

<li> At Last, Etta James </li>

<li> Georgia On My Mind, Ray Charles </li>

<li> Soul Man, The Blues Brothers </li>

</ul>

Page 52: Divs and Spans

52

Adding <span>

Song and Artist now have a class

<ul><li>

<span class=“song”> At Last</span>, <span class=“artist”> Etta James </span>

</li> <li>

<span class=“song”> Georgia On My Mind </span>, <span class=“artist”> Ray Charles </span>

</li> <li>

<span class=“song”> Soul Man </span>, <span class=“artist”> The Blues Brothers </span>

</li></ul>

Page 53: Divs and Spans

53

Adding <span>

• <span> has no effect on style• Add style by adding rules to the stylesheet

.song {

font-style: italic;

}

.artist {

font-weight: bold;

}

Page 54: Divs and Spans

54

The style of an <a> element changes depending on its state

States• link (never clicked on, unvisited)• visited (visited/clicked on)• hover (mouse over but not clicked on)

Page 55: Divs and Spans

55

Adding Style to the <a> element

Add the following to the stylesheet

a:link {

color: green;

}

a:visited {

color: red;

}

a:hover {

color: yellow;

}

Page 56: Divs and Spans

56

Pseudo-class

Acts like a class but it isn’t a real class• You can style pseudo-classes not necessary to type

the classes into XHTML code• Example of a pseudo class:

a:link

a:visited

a:hover• Browser interprets each of these as a pseudo-class

Page 57: Divs and Spans

57

Pseudo-class

Acts like a class but it isn’t a real class• You can style pseudo-classes not necessary to type

the classes into XHTML code• Example of a pseudo class:

a:link

a:visited

a:hover• Browser interprets each of these as a pseudo-class

Page 58: Divs and Spans

58

Adding a descendant selector combined with a pseudo-class

Add the following to the stylesheet

#elixirs a:link {color: green;

}#elixirs a:visited {

color: red;}#elixirs a:hover {

background: bluecolor: yellow;

}

Page 59: Divs and Spans

59

Descendant selector with a pseudo-class

• First selector selects any unvisited <a> element nested in an element with the id “elixirs”, styles only links inside elixirs, sets color of text to green

• Second selector selects visited <a> element… sets color of text to red

• Third selector selects hover <a> element… sets background to blue and color of text to yellow

#elixirs a:link { color: green; }

#elixirs a:visited { color: red; }

#elixirs a:hover { background: blue

color: yellow; }

Page 60: Divs and Spans

60

The Cascade in CSS

• Refers to the way styles cascade down from multiple style sheets

• The most specific styling is applied to each element

• Basically three possible style sheet definitions

1. Styles defined by the author or creator

2. Styles defined by the person viewing or reading

3. Styles defined by the browser

Page 61: Divs and Spans

61

Cascade in CSS

• Browser determines which to display by the priority, author has the highest followed by viewer and then browser

-end