1 07-2-css floats-and_positioning

1,485 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,485
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
38
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • We've shown you how boxes have layers that define how they are rendered and how they hold their contents.In there we also show how the properties of CSS that relate to the box model can define rules for how elements play together in the document.From there, we need to know how elements with those properties can use flow and positioning to work in page layouts and with mixed content.
  • We've covered it only lightly, but here we find out that elements are made up of two different kinds of box model sets. Block and Inline.
  • Block elements will expand across the available width (unless constraints are placed on them) and clear any following content below them down the page. <div> and <p> elements are examples of these.
  • Inline elements will sit within the flow of text or elements horisontally as well as vertically. They will by default, only take up width and height of their contents. <span> and <a> elements are examples of these.
  • Since we introduced the box model last lesson it's important to know that our example was based around a block kind of element. Things change for inline elements, as they don't support the same kind of box model properties due to their nature:
  • The Width property wont do anything on inlines since they are defined by their content and run inline with other content.Margins are only possible on the left and right, not top and bottom....and other things left to your experimentation :-)
  • In the CSS layout model, text elements are laid out from top to bottom in the order in which they appear in the source, and from left to right (in left-to-right reading languages).Block elements stack up on top of one another and fill the available width of the browser window or other containing element.Inline elements and text characters line up next to one another to fill the block elements.
  • When the window or containing element is resized, the block elements expand or contract to the new width.
  • When the window or containing element is resized, the inline content reflows to fit.
  • Objects in the normal flow affect the layout of the objects around them. This is what we've been showing you with the box model examples and what you know from seeing websites in your own experience - elements don’t overlap or bunch up, they make room for one another.Now we start paying attention to whether elements are in the flow or removed from the flow. Floating and positioning changes the relationship of elements to the normal flow in different ways.
  • The float property moves an element as far as possible to the left or right, allowing the following content to wrap around it. It's not a positioning scheme, but a unique feature built into CSS. 
  • The float property moves an element as far as possible to the left or right, allowing the following content to wrap around it. It's not a positioning scheme, but a unique feature built into CSS.Floats are one of the primary tools of modern CSS-based web design, used to create multi-column layouts, navigation toolbars from list elements, and table-like alignment without tables.
  • Floated elements continue to influence the surrounding content.Eg. If some paragraph text is near a floated element it reflows to make room for it.One popular analogy compares floats to islands in a stream - they are not in the flow, but the stream has to flow around them.
  • It is also important to note that the floated element is placed within the content area (the inner edges) of the element that contains it.It does not extend into the padding area of the paragraph.
  • The margin is held on all sides of the floated image. The entire element box, from outer edge to outer edge, is floated.
  • It is necessary to specify a width for floated text elements because without one, the content area of the box expands to its widest possible width (or, on some browsers, it may collapse to its narrowest possible width).Images have an inherent width, so we don't need to specify a width (although we can).Basically, If you do not provide a width value, inside the browser the width of the floated block will be set to auto, which fills the available width of the browser window or other containing element.There’s not much sense in having a full-width floated box since the idea is to wrap text or elements next to the float, not start below it!
  • The margin is held on all four sides of floated elements, even though top and bottom margins are usually not rendered on inline elements.This is because all floated elements behave like block elements.Once you float an inline element, it follows the display rules for block-level elements, and margins are rendered on all four sides.Margins on floated elements do not collapse (overlap) as normal though! So be aware!
  • A floated block will float to the left or right relative to where it occurs in the XHTML source code allowing the following elements in the flow to wrap around it.It will stay below any block elements that come before it in the flow (in effect, it is “blocked” by them).That means you can’t float an element up to the top corner of a page, even if its nearest ancestor is the body element.If you want a floated element to start at the top of the page, it must appear first in the document's source code.
  • It’s perfectly fine to float multiple elements on a page or even within a single element.When you float multiple elements, there is a complex system of behind-the-scenes rendering rules that ensure floated elements do not overlap.Floated elements will be placed as far left or right (as specified) and as high up as space allows.When a series of sequential paragraphs are floated to the same side (left), the first floats start stacking up from the left edge, but when there isn't enough room for the last one, it moves down and to the left when it bumps into something; e.g. the edge of the browser window.
  • A floating image obscures borders of block boxes it overlaps.
  • Both paragraphs have set 'clear: left', which causes the second paragraph to be "pushed down" to a position below the float — "clearance" is added above its top margin to accomplish this (see the 'clear' property).
  • The last thing you need to know about floated elements is how to turn the text wrapping off and get back to layout as usual.This is done by clearing the element that you want to start below the float.
  • Applying the clear property to an element prevents it from appearing next to a floated element, and forces it to start against the next available “clear” space below the float.Keep in mind that you apply the clear property to the element you want to start below the floated element, not the floated element itself.
  • The left value starts the element below any elements that have been floated to the left
  • Similarly, the right value makes the element clear all floats on the right edge of the containing block.
  • Similarly, the right value makes the element clear all floats on the right edge of the containing block.
  • 1 07-2-css floats-and_positioning

    1. 1. CSS Floats & Positioning<br />Colin Gourlay & Kevin Vanderbeken<br />
    2. 2. Today:<br />Floating Elements<br />Clearing Floated Elements<br />
    3. 3. introduction<br />
    4. 4. different boxes<br />
    5. 5. block elements<br /><div>, <p>, etc.<br />I’m text inside a block element<br />So am I.<br />I’m in a block element too!<br />
    6. 6. inline elements<br /><span>, <a>, etc.<br />I’m text inside an inline element<br />So am I.<br />I’m in<br />an inline element too!<br />
    7. 7. block and inline elements support the box model in different ways<br />
    8. 8. <ul><li>The Width property wont do anything on inlines since they are defined by their content and run inline with other content.
    9. 9. Margins are only possible on the left and right, not top and bottom.
    10. 10. ...and other things left to your experimentation :-) </li></li></ul><li>In the CSS layout model, text elements are laid out from top to bottom in the order in which they appear in the source, and from left to right.<br />
    11. 11. When the window or containing element is resized, block elements expand or contract to the new width.<br />block element<br />block element<br />block element<br />block element<br />block element<br />block element<br />
    12. 12. When the window or containing element is resized, inline elements reflow to fit. <br />inline element<br />inline<br />inline element<br />element<br />inline element<br />inline element<br />inline element<br />
    13. 13. elements don’t overlap or bunch up, they make room for one another<br />
    14. 14. floated elements<br />a.k.a<br />‘floats’<br />
    15. 15. floats can be used to create:<br />multi-column layouts<br />navigation toolbars<br />table-like alignment<br />(without tables)<br />and much, much moreTM<br />
    16. 16. float: left;<br />float: right;<br />float: none;<br />
    17. 17. things you should know about floated elements<br />
    18. 18. a floated element is like an island in a stream<br />I’m a block element that is beside an image that has been floated to the<br />right. My contents will wrap around the image.<br />
    19. 19. floats stay in the content area of the containing element<br />this isn’t possible<br />
    20. 20. margins are maintained<br />img{ float: right; margin-left: 5px; margin-right: 5px; }<br />I’m a block element that is beside an image that has been floated to the<br />right. My contents will wrap around the image.<br />
    21. 21. always provide a width for floated elements<br />p.floatRight{ float: right; margin: 5px; width: 120px; }<br />I’m a block element that is beside an paragraph that has been floated to the right. My contents <br />I’m a paragraph element, floated to the right, with a defined width<br />will wrap around the paragraph.<br />
    22. 22. floated inline elements behave as block elements<br />
    23. 23. elements do not float higher than their reference in the source<br />
    24. 24. you can float multiple elements<br />Because I am beside two floated<br />elements (one left, one right), I wrap around both of them. <br />
    25. 25. there is one noticeable side effect of using floats...<br />
    26. 26.
    27. 27. what we actually want to see is this...<br />
    28. 28.
    29. 29. to achieve this effect, we need to make use of clearing<br />
    30. 30. clearing floated elements<br />
    31. 31. clear: left;<br />clear: right;<br />clear: both;<br />
    32. 32. clear: left;<br />The left value starts the element below any elements that have been floated to the left<br />
    33. 33. clear: right;<br />Similarly, the right value makes the element clear all floats on the right edge of the containing block<br />
    34. 34. clear: both;<br /> If there are multiple floated elements, and you want to be sure an element starts below all of them, use the both value to clear floats on both sides<br />
    35. 35. My position is governed by the floated elements to the left. I have not been cleared in any way.<br />I have been cleared, so I start below the floated elements.<br />
    36. 36. next week...<br />
    37. 37. CSS Positioning<br />&<br />CSS Grid Systems<br />

    ×