Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 of 6
Notice we said pasted, because that's exactly what happens. Such AP elements are indeed placed on the sides, but they are also removed from the flow of the parent box, meaning their placement is not taken into consideration by non-AP elements within the box. Depending on source order or z-index, non-AP elements may get covered up by these AP elements. If no other content is within the box besides the AP elements, then no cover-up can happen, but then the box has no "real" flowed content that can stretch it vertically. The AP elements won't do this because they are no longer in the flow (taking up space), and unlike floated elements, AP elements cannot be cleared. So in this case, it is necessary to set some height value to the container box or it will have no screen height, and the AP elements will hang down from the container like flags from a balcony.
The Details
So let's look at the code needed to have our two elements aligned on opposite sides of a box.
The HTML: <div class="container"> <div class="left-element"> Content in left item </div> <div class="right-element"> Content in right item </div> </div>
2 of 6
The CSS: .container { position: relative; height: 50px; } .left-element { position: absolute; left: 0; width: 50%; } .right-element { position: absolute; right: 0; width: 50%; text-align: right; /* depends on element width */ }
When an AP element is placed using the right property, an effect known as the 1px Rounding Error (http://www.communitymx.com/abstract.cfm?cid=A47F91175DB7863B) can occur, causing the AP element to be placed 1px to the left of its correct position. This won't matter if only text is involved, but if the AP element has borders or a background and is designed to fit precisely against the right container edge, it may be a problem.
Note: While IE5/Mac will generally position elements correctly at the right side of the viewport, the browser will show a mysterious and unneeded horizontal scrollbar. Testing by others has uncovered a "secret right margin" on absolutely positioned elements. There are several methods to defeat this problem. You can read more about it, and ways to overcome it at Philippe Wittenbergh's Mac IE 5 test page (http://www.l-c-n.com/IE5tests/right_pos/) about right positioning.
The top property can be used for exact vertical control, or it may be omitted. When this is done, the uncoded/unwritten value for top defaults to auto, placing the element directly below any previous flowed element that precedes it in the source. Also, remember that these AP elements aren't really "inside" the container, but rather are stacked over it, so if content within them causes them to become taller they may protrude below the bottom of the container. This is one good reason to avoid excessive use of absolute positioning on a page, when simply flowing the content can do the job better and more safely. If you are wondering what sort of widths on the AP elements are best, then stop wondering and just do some experimentation. Any widths will work, depending on the desired look. Sometimes pixels are fine, sometimes percentages, or ems can come in handy. The two items don't need to have the same width units either. You could have percentage on one side and em on the other. Playing with the variables is often the best way to get the layout looking good.
3 of 6
The HTML: <div class="container"> <div class="left-element"> Content in left item </div> <div class="right-element"> Content in right item </div> </div>
The CSS: .container {no required styles} .left-element { float: left; width: 49%; } .right-element { float: right; width: 49%; text-align: right; /* depends on element width */ }
4 of 6
If there is not enough room for the two floats to fit side by side, they will not just overlap, as AP elements do. Instead, floats wrap, forcing the float that comes second in the source to drop below the first float. Because of this, it is critical that the floats be prevented from coming into contact with one another. This is done by carefully controlling the float widths. Be sure to watch out for added padding and borders that increase the size of the floats. In some cases, these added properties can increase the size of the floats enough to cause wrapping. Unlike absolute positioning, the float method does not require that the container be positioned, nor is it strictly necessary to give the floats widths, since all the newer browsers "shrink-wrap" floats around their contents. Even IE/win back to IE5 does this. Unfortunately, IE5/Mac does not, so when shrink-wrapping is desired, it may be necessary to give a width only to IE5/Mac, hiding the width from all other browsers. Here's how it may be done:
.left-element {float: left;} .right-element {float: right;}
The second pair of rules supplements the first two, but the construction of the selectors prevents any browser other than IE/Mac from recognizing the selectors. The selector construction says: "Select any element with a class attribute of .leftelement when it is nested within HTML, and HTML is the direct child of any other element." It so happens that IE browsers (both Mac and Win) believe there is some kind of mystery element wrapped around the HTML element. This is strange because HTML is supposed to be the primary or root element. Thus, IE browsers obey, and all others ignore the rule. Also, since the > (child) combinator is used in the selector, and IE/Win does not support that combinator, the result is that only IE/Mac obeys the rule. Going "widthless" on the floats will let the floats shrink to the size of the content, but if that content is a lengthy text, then line breaks must be added or the float will stretch as wide as the text line can go, possibly interfering with the other float. If widths are used, they may be in pixels, percentages, or ems. If percentage is used, don't give the floats each a 50% width. If the 1px rounding error should occur and the total width is 100%, then a dropped float will result. Better to leave some wiggle room. Giving a text float a pixel width works fine, but if it is to be just one line and the text sizing is bumped up by the user, then the single line may become too wide for the assigned pixel width and will wrap to a second line. This fate may be prevented by giving the float a width in ems rather than pixels, because an em width will enlarge along with the text. Sometimes the rate of enlargement is not exactly the same, so experimentation may be necessary to achieve satisfactory results.
5 of 6
Keywords CSS, align elements, absolute positioning, float, align elements left and right, CSS task
6 of 6