1 / 25

CSS III Positioning

CSS III Positioning. Webdesign og Webkommunikation 09.10.2008. Goals of the lecture. Floating Positioning. The normal flow. Block elements laid from top to bottom Text elements laid from left to right, flowing in the block element Objects in the normal flow influence surrounding objects

shadi
Télécharger la présentation

CSS III Positioning

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSS III Positioning Webdesign og Webkommunikation 09.10.2008

  2. Goals of the lecture • Floating • Positioning

  3. The normal flow • Block elements laid from top to bottom • Text elements laid from left to right, flowing in the block element • Objects in the normal flow influence surrounding objects • Floating and positioning change the normal flow

  4. The normal flow <p><img src="logo_en.gif" alt="" /> "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam accumsan, leo vel gravida placerat, est nulla sollicitudin mi, ut dignissim eros urna sit amet sem. Phasellus posuere malesuada tortor. Mauris id pede.</p>

  5. Floating image img.right {float:right; margin: 10px;} <p><img class="right" src="logo_en.gif" alt="" /> "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam accumsan, leo vel gravida placerat, est nulla sollicitudin mi, ut dignissim eros urna sit amet sem. Phasellus posuere malesuada tortor. Mauris id pede.</p>

  6. Floating text span.sidenote {float: right; margin: 10px; border: 2px solid; width: 200px;} <p><span class=“sidenote”>This is a sidenote on the right explaining some latin…</span>Lorem <em>ipsum dolor</em> sit amet...</p>

  7. Floating multiple elements <p>P1</p> <p class=“float”>P2</p> <p class=“float”>P3</p> <p class=“float”>P4</p> <p class=“float”>P5</p> <p>P6</p> p#float { float: left; width: 200px; } p {border: 1px solid red;}

  8. Clearing img {float: left;} img {float: left;} h2 {clear: left;}

  9. Positioning • 4 types: • static: normal way, objects are rendered following the normal flow • relative: moves the element box preserving the original space • absolute: object is removed from the flow and positioned relative to their containing block • fixed: similar to absolute, but object is positioned relative to the viewport (i.e. the browser window)

  10. Specifying position • offset property (top, right, bottom, left) • Value: • Absolute measure (pixels, ems) • Relative (%) • Move image 50 pixels down • Can take negative values -> move in the opposite direction img {position: absolute; top: 50px; }

  11. Relative positioning • Relative to the object’s initial position in the normal flow • The original space in the document flow is preserved • There might be overlap em { position: relative; top: 30px; left: 30px; background-color: pink; }

  12. Absolute positioning • Relative to the edges of the containing block using offset properties • Object is removed from the document flow em { position: absolute; top: 30px; left: 30px; background-color: pink; }

  13. Absolute positioning II • How to determine the containing block of an element: • positioned element not contained within another positioned element -> position relative to the root containing block (<html> element) • positioned element contained within an element that is positioned using relative, absolute or fixed -> position relative to that containing element • An element that is positioned becomes the containing block for all elements it contains

  14. Absolute positioning III #one {position: absolute; top: 10px; left: 10px;} #two {position: absolute; top: 30px; left: 10px;} #three {position: absolute; top: 10px; left: 10px;} <p id=“one”>p1</p> <div id=“two”> <p id=“three”>p3</p> </div> 10px 10px p1 30px 10px 10px p3 10px Viewport

  15. Z-index p {position: absolute; padding: 5px; color: black;} #p1 {z-index: 19; background-color: red;} #p2 {z-index: 1; background-color: blue;} span.b {z-index: 72; background-color: green;} <p id=“p1”>Paragraph 1, z-index=19</p> <p id=“p2”>Paragraph 2, z-index=1. <span class=“b”>This is a span with z-index 72</span> And now some more text…

  16. Fixed positioning • Relative to the edges of the viewport (browser window) • Object is removed from the document flow • Elements remain fixed with respect to the browser window • Not supported by (you guessed it) IE 6 and earlier.

  17. Fixed positioning II #menu { position: fixed; width: 200px; background-color: pink; } #content { position: absolute; left: 250px; border: 1px solid #CCCCCC; }

  18. Page layout • We don’t know the size of the browser window • Users might change the size of the text • 3 main techniques for page layout: • Liquid • Fixed • Elastic

  19. Liquid layout • The page fills the available space in the window • http://www.w3.org/ • Pros: • No need to design for a specific resolution • The whole window is filled: no blank spaces • Cons: • Line lengths can get very long in large monitors • Layout less predictable

  20. Liquid layout II • Use percentages to specify width #left { float:left; width: 25%; margin-right: 5%; background-color:pink; } #content { float:left; width: 70%; }

  21. Fixed layout • The width of the page is fixed: blank space either on one or on both sides • http://www.dr.dk/ • Pros: • Predictable layout • Good control over line length • Cons: • If page width bigger than browser window, the content on the right is not shown • Layout changes if text is resized

  22. Elastic layout • Expands or contracts with the text size • http://www.htmldog.com/articles/elasticdesign/demo/ • Pros: • Consistent layout • Flexibility in text size • Good control over line lengths • Cons: • Images don’t resize with text • Not really necessary with the “new” zooming function in newer browsers

  23. Centering a page div#container { position: relative; width: 600px; margin-left: auto; margin-right: auto; } Might not work in IE6 div#container { position: absolute; width: 600px; left: 50%; margin-left: -300px; } Works always

  24. Final advise • Try your website in different browsers • And if you can’t: • http://browsershots.org/

  25. Questions?

More Related