internet programming 1 - wordpress.com12/01/2013 1 internet programming 1 itg 212 / a lecture 10:...

32
12/01/2013 1 Internet Programming 1 ITG 212 / A Lecture 10: Cascading Style Sheets – Page Layout – Part 2 1 Lecture10

Upload: others

Post on 22-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

  • 12/01/2013

    1

    Internet Programming 1ITG 212 / A

    Lecture 10: Cascading Style Sheets –Page Layout – Part 2

    1 Lecture10

  • 12/01/2013

    2

    The CSS Box Model

    Lecture102

    bottom border

    Content

    bottom padding

    bottom margin

    leftmargin

    rightmargin

    top margin

    top border

    leftborder

    rightborder

    top padding

    leftpadding

    rightpadding

  • 12/01/2013

    3

    An Aside – CSS Shorthand Notation

    Lecture103

    It can get quite tedious writing a separate stylefor each of the four sides of an element, whetheryou are specifying margins, padding, or borders.CSS provides several different shorthand ways tospecify these properties within a singledeclaration.

    In any shorthand notation the order is always top,right, bottom, left.

    So instead of writing:

    {margin-top:5px; margin-right:10px; margin-bottom:8px; margin-left:4px}

    You can write: {margin: 5px 10px 8px 4px;}

  • 12/01/2013

    4

    An Aside – CSS Shorthand Notation

    Lecture104

    Note that there is just a space between each of thevalues, no delimiters such as a comma are used.

    You also do not need to specify all four values whenusing the shorthand notation. If you do not provide allfour values, the opposite side’s value is used as themissing value.

    For example: {margin: 12px 10px 6px;} will set themissing value for the left margin as 10px which is thevalue that is specified for the right margin.

    Similarly: {margin: 12px 10px;} will set the missingbottom margin to 12px and the missing left margin to10px.

  • 12/01/2013

    5

    An Aside – CSS Shorthand Notation

    Lecture105

    If only a single value is specified, such as{margin: 12px;} then all four sides are set to thissingle value.

    Note when using the shorthand notation, it is notpossible to set only the bottom and left marginswithout providing some values for the top andright, even if those values are both zero. In casessuch as this, you can write 0 without supplying avalue type, such as:

    {margin: 0 0 4px 8px;}

    The same shorthand rules apply to padding andborder, as well as margin.

  • 12/01/2013

    6

    The CSS Box Model

    Lecture106

    You can adjust three different aspects of the boxwith CSS: margin, border, and padding.

    For the margin you can set the distance betweenthis box and adjacent elements in the markup.

    For the border you can set the thickness, style,and color.

    For the padding you can set the distance of thebox’s content is to be separated from its border.

  • 12/01/2013

    7

    The CSS Box Model

    Lecture107

    A simple way to think about these properties isthat margins push outward from the border andpadding pushes inward from the border.

    Since every box has four sides, propertiesassociated with margins, border, and paddingeach have four settings: top, right, bottom, andleft.

    We’ll now look more closely at the propertiesassociated with a box’s margin, border, andpadding and give lots of examples illustrating howto manipulate these properties to achieve theresults you want.

  • 12/01/2013

    8

    The Box Border

    Lecture108

    The box border has three associatedproperties:

    Width. This includes thin, medium, thick, or anyunit (ems, px, %, …).

    Style. This includes none, hidden, dotted, dashed,solid, double, groove, ridge, inset, and outset.

    Color. Any color value can be used.

    The shorthand notation for styling all fourborders the same, width, style, and color is:

    .borderclass {border: 4px solid green; }

  • 12/01/2013

    9

    Lecture109

  • 12/01/2013

    10

    Lecture1010

  • 12/01/2013

    11

    The Box Padding

    Lecture1011

    Padding adds space between the box’s contentand the border of the box.

    As part of the inside of the box, it takes on thecolor of the box’s background.

    The markup on the next page illustrates twoparagraphs, one with and one without padding.

  • 12/01/2013

    12

    Lecture1012

  • 12/01/2013

    13

    Lecture1013

  • 12/01/2013

    14

    The Box Margins

    Lecture1014

    Margins are slightly more complex that borders andpadding.

    Most block level elements (paragraphs, headings, lists,etc.) have default margins.

    Consider the example shown on the next page whichillustrates a heading and two paragraphs.

    The first case shows the default case.

    The second case shows the borders and backgroundsturned “on” to illustrate how the margins create spacebetween the elements.

    The third case illustrates what happens if the marginsare set to zero. The elements then touch one another.

  • 12/01/2013

    15

    Lecture1015

  • 12/01/2013

    16

    The Box Margins

    Lecture1016

    It is typically a good practice in your CSS to place thefollowing declaration at the top of a style sheet:

    * {margin:0; padding:0; }

    This will set the default margins and padding of allelements to 0 and that way you won’t get confused asto which margins and padding are being set by thebrowser and which you are setting.

    Now you can add them back to just the elements thatyou want to have margins as you style the page.

    As we’ll see later, different browsers apply defaultpadding and margins differently to element sets suchas forms and lists, and by “neutralizing” the defaultsettings in this manner and then adding your own, willresult in a more consistent cross-browser effect.

  • 12/01/2013

    17

    The Box Margins

    Lecture1017

    Often you will mix units when you set margins fortext elements such as paragraphs.

    For example, the left and right margins of aparagraph might be set in pixels so that the textremains a fixed distance from a navigationsidebar, but you might want to set the top andbottom margins in ems so that the verticalspacing between paragraphs is relative to the sizeof the paragraphs’ text. So you might dosomething like this:

    p {font-size:1em; margin:0.75em 30px; }

  • 12/01/2013

    18

    The Box Margins

    Lecture1018

    p {font-size:1em; margin:0.75em 30px; }

    In this case, the space between paragraphs isalways three-quarters of the height of the text: ifyou increase the overall type size in the bodyelement, not only will paragraphs’ text get bigger,but the space between the paragraphs alsoincreased proportionally. The left and rightmargins, set in pixels will remain unchanged bythe redefinition of the body element type size.

    This will become more important when we beginlooking at overall page layouts.

  • 12/01/2013

    19

    The Box Margins

    Lecture1019

    Vertical margins collapse. Horizontal margins donot.

    Consider the following scenario: we have threeparagraphs of text, one after the other in normalflow, and each is styled using the following CSSrule:

    p {width:400px; height:50px; border:1px solid black;

    margin-top:50px; margin-bottom:30px;}

    How would you expect these paragraphs toappear? How much space (in terms of pixels) willappear between the paragraphs? Will it be 30px,50px or 80px?

  • 12/01/2013

    20

    You might reasonably assume that there would be 80 pixels (50 + 30) between the paragraphs, but you would be wrong as the actual gap is only 50 pixels.

    When top and bottom margins meet, they overlap until one of the margins touches the border of the other element.

    In this case, the larger top margin of the lower paragraph touches first (i.e., it touches the bottom border of the upper paragraph), so it determines how far apart the elements will be set.

    This effect is known as collapsing.

  • 12/01/2013

    21

    The Box Margins

    Lecture1021

    This collapsing margin effect ensures that whenan element is first or last in a group of headings,paragraphs, or lists, for example, the element canbe kept away from the top or bottom of the pageor containing element.

    When the same elements appear between otherelements, both margins are not needed, so theysimply collapse into each other, and the largerone sets the distance.

  • 12/01/2013

    22

    How Big Is A Box?

    Lecture1022

    The way the box model works is the root of mostproblems for beginning (and some expert) CSSdevelopers.

    For now we’ll focus only on block level elementssuch as headings, paragraphs, and lists. Inlineelements will behave differently.

    Let’s go back over the box model step-by-step ina little more depth, focusing first on the width ofa box, since managing element width is critical tocreating multi-column layouts (but the same basiclogic will apply to the height of boxes as well).

  • 12/01/2013

    23

    How Big Is A Box?

    Lecture1023

    The width of a box is set using the width property.

    Without any padding the content is also the width of the box, and it touches the sides of the box.

  • 12/01/2013

    24

    Lecture1024

  • 12/01/2013

    25

    How Big Is A Box?

    Lecture1025

    In the previous example, since we did not includeany padding in the box, the content extended tothe very edge of the box.

    What happens when we add some padding to thebox, in this case 20 pixels to the right and leftsides of the box?

    What will be the total size of the box? In otherwords, we set its width to be 400 pixels, will thecontent now be squished into 360 pixels of width,or will the box expand to a total of 440 pixels inwidth?

  • 12/01/2013

    26

    Lecture1026

    The box expanded to a total width of 440 pixels (20 pixels of padding on the right and left of the content).

  • 12/01/2013

    27

    How Big Is A Box?

    Lecture1027

    What would happen to the overall size of the box ifwe now add a 6 pixel border to the right and leftsides of the box?

  • 12/01/2013

    28

    Lecture1028

    How Big Is A Box? The box will expand by another 12 pixels in width.6px left border + 20px left padding + 400px box width + 20px right padding + 6px right border(6 + 20 + 400 + 20 + 6) = 452 pixels

  • 12/01/2013

    29

    How Big Is A Box?

    Lecture1029

    Now let’s add left and right margins to create spacearound the sides of the element. Now how much spacewill the element occupy?

  • 12/01/2013

    30

    Lecture1030

    How Big Is A Box?

    The margins add space around the element but do not expand the size of the element itself, since the margins are outside of the box.

  • 12/01/2013

    31

    The Box Model

    Lecture1031

    The main thing to remember is that with all modern browsers when you set the width of an element, you are really setting the width of the content within it, and any padding, borders, and margins you set increase the overall space the element occupies over and above the specified width value.

  • 12/01/2013

    32

    Lecture10

    Questions?

    Next Lecture will start 10:30

    Second Midterm Exam

    Tuesday 22/01/2013

    From 10:00 am to 12:00 pm

    32