240 likes | 462 Views
Cascading Style Sheets (Advanced topics). Two Layout Techniques. Table Layout This works, but is considered a misuse of tables. Table layout is gradually phased out. CSS Layout CSS positioning is used to place elements. Standard compliant
E N D
Two Layout Techniques • Table Layout • This works, but is considered a misuse of tables. • Table layout is gradually phased out. • CSS Layout • CSS positioning is used to place elements. • Standard compliant • Very old browsers and mobile devices may not render CSS correctly.
The Concept of Normal Flow • Normal flow basically means how browsers normally lay out components without any special settings in CSS. • In the normal flow, text elements are laid out from top to bottom and from left to right (for ltr languages). • You can float any element with CSS (paragraphs, lists, divs, etc.) • Floats are used to create multicolumn layouts, navigation, etc.
Normal Flow vs. Window Sizes 2 1 1 2 3 1 2 3 4 4 5 5 3 4 1 2 1 2 3 4 5 3 4 5 See Experiment 1 in css_float1.htm for more information.
CSS float property • Values: left | right | none | inherit • img {float: right; margin: 20px} • See Figure 21-1 on p. 370 for screenshot. • Floated elements using this CSS property are removed from the normal flow. Surrounding content is reflowed to stay out of their way.
CSS Clear Property • Values: left | right | both | none | inherit • img {float: left; margin-right: 10px} • h2{clear: left; top-margin: 2em} • See Figure 21-7 on p. 375 for screenshot. • Here is how to interpret the values of the Clear property • The left value starts the element below any elements that have been floated to the left edge of the containing block. • See Flotutorial’s Tutorial 3 for more info.
CSS Position Property • Values: static | relative | absolute | fixed • Static: normal positioning • Relative: moves the element box, but its original space in the document flow is preserved • Absolute: positions an element at a specific location. The element is removed from the document flow. • Fixed: similar to absolute position, but places elements relative to the view port (browser window)
CSS Positioning Properties • Left • Right • Top • Bottom • Example • div.a { position: absolute; height: 120px; width: 300px; background-color: yellow} • div.b{position; absolute; top: 20px; left: 10px}
Handling Overflow • When an element is set to a size too small to contain all its contents, it is possible to specify how its content is to be displayed. • CSS Overflow property • Values: visible | hidden | scroll …
CSS z-index Property • The z-Index property allows you to define the stacking order of elements. • The higher the z-index is, the higher in the layer stack an element will be. • Examples on p. 384
Absolute Positioning • Absolute positioning is used to position an element at a certain location. • Three properties have to be met. • It is declared using { position: absolute } • It is positioned relative to the edges of its containing block using offset properties (e.g., top, left, bottom, and right) • It is completed removed from the document normal flow.
Relative Positioning (1) • The main difference between relative positioning and absolute positioning is that the space where an element would have appeared in the normal flow is preserved in relative positioning and continues to influence the elements that surround it.
Relative Positioning (2) • Four Properties of Relative Positioning • It is declared using { position: relative } • It is positioned relative to its initial position in the normal flow using offset properties (e.g., top, left, bottom and right) • Its original space in the document flow is preserved. • It can potential overlap other elements.
Centering a Page • div.mypage { width: 500px;margin-left: auto;margin-right: auto;} • See other techniques on p. 419 – 421.
Two-Column Layout (with Header and footer) Header Main Side bar Footer
Two-Column Layout (Floats) <div class=“header”> … </div> <div class=“main”> … </div> <div class=“sidebar”> … </div> <div class=“footer”> … </div> .header { background-color: yellow; padding: 3px} .main {float:left; width:80%; margin-right: 3%;} .sidebar { width: 20%; float:left;} .footer {clear: left; padding:15px}
Two-Column Layout (Absolute) <div class=“header”> … </div> <div class=“main”> … </div> <div class=“sidebar”> … </div> <div class=“footer”> … </div> .header { width: 780px; height: 70px;} .main {width:580px;} .sidebar { position: absolute; top: 70px; left: 580px; width: 200px} .footer {clear: left; padding:15px}
Three-Column Layout • Similar to the two-column layout, you can do three-column layout using float or absolute positioning. • See exercises in class.
Faux Columns (p. 432) Faux Columns can be applied to both table layout and CSS layout.
CSS Floating and Other Techniques • Floatutorial – css.maxdesign.com.au [MR] • Understanding CSS z-Index – Mozilla [MR] Items marked as [MR] are must read items.