Related Links |
Tuesday, January 9. 2007Absolute Positioning? Absolutely Not!Absolute Positioning? Absolutely Not! Absolute Positioning? Absolutely not! This reaction to the process of absolutely positioning elements on a web page is understandable but not justified. Why ever not? Absolute positioning provides the designer with certain advantages and a quite a few disadvantages too. However, before we actually set out the long list of advantages and disadvantages, let us look at the concept of absolute positioning. Defining Concepts Absolute positioning defines the x and y coordinates of an element with reference to the top left corner of the browser page or the containing block and the position attribute is set to absolute. The code will read as under:
Note that the right and bottom coordinates have not been defined. So the text moves to the right and the bottom of the page. Specifying all four coordinates will give you greater control over the element. Absolute positioning removes the element from the normal flow of the document and it does not impact the other elements on the page when the page is loaded. This means that the element is placed in an arbitrary position somewhere in the viewport and its properties are set by the top, bottom, left and right attributes. A point that needs to be noted here is that if the absolutely positioned element is a child element of another element which has not been placed static, then the absolutely placed element will not be placed relative to the viewport, but relative to the parent box. In the following code three boxes have been specified. When the code is executed, box one will be placed relative to the view port. Box two will be placed absolutely and box three as the child of box two will be placed relative to box two.
The user agent or the application that processes the web page such as the browser selects the initial containing block. It also could be related to the veiwport. It is ok to assign negative values to the absolutely positioned elements. However, this may result in non-display of the element. Layers can be used to absolutely position elements on a page. Layers are created using the div tag as in the following code: <div style="position: absolute; left: 610px; top: 80px; height: 400px; width: 100px; padding: 1em;">layer stuff</div>
The advantage of using layers with div tags is that they load faster. All the style information is contained in the attributes added. The overlapping layers can be created using the z-index command. The values assigned to the z index will determine its order in the stack. The higher the index the closer the div will appear to the front of the page. The layer that holds the page’s content should be given the z index of one so that users can read the content and click on the navigation elements. Absolute positioning gives complete control over the elements of a web page to you. Once the element is pulled out of the flow it can be placed anywhere in the page and this will allow you the freedom to display your content before your navigation structures become visible. This enables you to provide the user with optimum accessibility and to optimize your content for search engines. However, absolute positioning can cause problems of overlapping if adequate care is not exercised. Layouts that use absolute positioning for columns can have problems with footer display if the column length is not correctly estimated. Comparing Concepts Most users are confused between absolute and relative positioning concepts in CSS. This is because of the coordinate system within which the block is placed. The block then depends on the positioning context and there is no universal set of coordinates to guide the designer. Whenever a new block is placed on the page it creates a positioning contexts for its descendents with reference to the upper left hand corner of the page. It another element is placed within the first element, its position will be calculated with reference to the top left corner of the containing block and not the page. The code fragment below will place an absolutely positioned element with reference to the top left corner of the page. <div class="MyTitle"  style="position:absolute; left:125px; top:75px;">  This text is being absolutely positioned with reference to the page. </div>
<div class="MyTitle" Â style="position:absolute; left:125px; top:75px;"> Â This text is being absolutely positioned with reference to the page. Â <div class="MyTitle" Â Â Â style="position:absolute; left:25px; top:30px;"> Â Â Â This is another absolutely positioned element within the first one. Â </div> </div>
If a third element is added using the relative positioning, it will be contained within the second element. Relative positioning will render the text slightly to the right of the second element within the span. This is because it calculates the position with reference to the original position on the page.
Combining Concepts Absolute positioning is relative!  In reality an absolutely positioned element is relative to the containing block. The containing block is the nearest positioned ancestor or the document’s initial containing block. It is a structurally superior element whose property is absolute or fixed or relative. The following example shows how an absolutely positioned element can be relative to the containing block. The H3 positioned as relative allows the span to be positioned relative to the containing header.
h3 span { <h3>Some header <span>!!!</span></h3>
A relative element can be nested in an absolute element. The relative element will be treated as a child of the absolute element and its position on the page will be determined in the context of the parent’s position on the page. The relative element is moved to its original position in the flow within the parent element. The code for this will read as under: .relpos { position: relative;
CSS allows overlay of elements to create a 3D effect. The elements can be absolutely positioned in such a way that they are positioned in overlapping layers. A stacking happens and the z index order value is used to position the layer in the stack and consequently the image or text. The code below is illustrative of this technique. It positions one image slightly above the other. .csslogo{ color: red;              font-size: 42pt;              font-weight: bold;              top:110px;              left: 80px;              position: absolute;              z-index:1             } .wkshop { color: black;              font-size: 26pt;              font-weight: bold;              top:140px;              left:200px;              position: absolute;              z-index:2             }
Trackbacks
Trackback specific URI for this entry
No Trackbacks
|
QuicksearchChaptersSyndicate This BlogContact AuthorBlog Administration |