Core CSS:: by Molly E. Holzschlag
Core CSS:: by Molly E. Holzschlag
Core CSS:: by Molly E. Holzschlag
•
The Box Model
•
The Power and Problem with Floats
•
CSS Positioning
Core CSS Wrap-Up
By Molly E. Holzschlag
•
•
Hot Tips and More...
Designed for
•
• developers
• Written by top experts
• Latest tools &
• technologies
• Hot tips & examples
Bonus content online
Subscribe New issue every 1-2
Refcardz.com
weeks
III
Figure 2. 1’ve taken the original text and p lace d it within a Now for FREE!
p arag rap h e le me nt. Then, d up licate d the e le me nt below, giving us Inc.
DZone, | w ww. d z o n e . c om
two b lo ck boxes each containing inline boxes.
2
tech facts at your fingertips
Core CSS: Part 111
shifted to the left and therefore the text flows to the right.
<img s r c = ” b - c a c t u s . j p g ” a l t = ” p h o t o o f bloomi ng b a r r e l
cactus” />
Figure 6. As we co ntinue to make the browser window smaller, the <p>The colors are beautiful, but the barrel cactus
paragraph reflows to fit the available width, while the text flows to h a s an i n n e r s e c r e t : i f you a r e i n n e e d o f h y d r a t i o n
the left in a downward manner, elongating the page .
in the desert, this cactus has a lot of fluid inside
and h a s of
location s athe
ved t h e land
image i v e stheo ftext
many.
flow, in the first case, the
Hot A great way to gain an appreciation of normal flow
The CSS values will of be different in terms of manipulating the
Tip is to simply create an HTML page with multiple will simply flow around the floated element once a float
paragraphs of text, and then size and resize the has
image is floated right, and in the second, left. Other elements
browser, watching how the flow behaves.
DZone, Inc. | w ww. d z o n e . c om
been applied.
3
tech facts at your fingertips
Core CSS: Part 111
The first thing to know is that floats are not in the normal flow. referred to as “tolerance” can be remedied by modifying
This means that other text and elements will continue to flow
widths slightly to accommodate IE.
around the floated elements. 1n Figure 9, we see what
happens
if two elements are floated to the right and left. 1f not contained elements,
Floats can beand on the by
contained floated boxes
setting themselves.
explicit widths onThis doesn’t
containing
by another element that has an explicit width, the floated
elements will continue to spread apart as the browser size of how tolerance
eradicate to mathematically
problems,arrange youryou
but it gives elements
a more(Figure 12).
finite idea
increases, and come together as the browser size decreases.
normal flow
Figure 13. Clearing floats is the process of returning the next
• A box with position: relative; creates a new instance of
consecutive element to the normal flow.
normal flow within it
1n terms of floats for layout, if you want to be able to have •
Relative positioning is most useful for offsetting boxes
content below floated elements, such as a traditional “footer”
or creating a positioning context (see Relatively Absolute
where site information will go, clearing is necessary. This allows later this reference)
the subsequent elements to return to the normal flow (Figure 14).
1magine three paragraphs of text in the normal flow (Figure 17).
Using CSS 1’ve hidden the visibility of the text in the second
paragraph to make the visualization of relative positioning clearer.
Figure 14. Once the float is properly cleared, we return to the normal
flow, which is how we can create a footer beneath the floated
columns. Figure 17. Three paragraphs in the normal flow.
There are numerous ways to clear floats. A very popular one is Here’s some CSS that offsets the paragraph using relative positioning:
the creation of a clear class and the use of the property
p { w i d t h : 4 0 0 p x ; b o r d e r : 1px s o l i d b l u e ; }
“clear” p#rel {position: relative; top: 50px; left: 50px;}
and a value of both:
The offset can be seen in Figure 18.
.clear {clear: both;}
People will then use a div or a break element to clear the float:
<div class=”clear”></div> or
<br class=”clear” />
My preference of the two is the break element, as an empty
div is a non-empty element with nothing inside it. While still
slightly presentational, at least “break” has meaning in this
case Figure 18. The second paragraph, positioned relatively.
(Figures 15 and 16).
At this point you’re probably wondering, what is that paragraph
being positioned to? This is why 1 say the term relative in this
instance is so confusing. The answer is not what you’d expect.
A relatively positioned element is positioned in relation to its
location in the normal flow. 1n other words, the browser still
“sees” the positioned element in the flow but on the design
surface, it’s visually moved from where it would have been. This is
Figure 15. The float is properly cleared. Notice how the column to the
left doesn’t fill the entire available space—this is normal behavior. The
why the big space is left behind, almost a ghost of the paragraph.
important issue is that the bottom-most element is cleared and does not The browser interprets the element as being in the normal flow.
try to “creep up” into the available space. Absolute
Absolute positioning is a little clearer in its terminology, and perhaps
a bit more logical in its behavior. An absolutely positioned element:
• 1s removed from normal flow
• 1s positioned in relation to
–a positioned parent element
–the root element of HTML
• Subsequent content flows into the now available space
Figure 16. Even when a tolerance problem forces the far right column
off the line, notice that it does not encroach upon the space above it. Inc.
DZone, | w ww. d z o n e . c om
This is because it’s not in the normal flow. Also, note that despite the
5
tech facts at your fingertips
Core CSS: Part 111
Fixed
Fixed positioning is actually a subset of absolute positioning
and can be very useful in creating static elements on the design
surface while other elements flow behind.
h1 {width: 100%; position: fixed; top: 0; left: 0;
b o r d e r : 1px s o l i d # 9 9 9 ; b a c k g r o u n d - c o l o r : # c c c ; }
Elements that are fixed are done so in relation to the
browser keep like items together within normal flow.
chrome (Figure 21).
Figure 23. Relatively absolute: Using a combination of positioning to
Creating a Positioning Context: “Relatively Absolute” 1 can reverse the stack order using the z-index:
One technique that is very helpful to designers who are using a
50px; background-color: blue; z-index: 3;}
combination of floats and positioning to accomplish their layouts
has been coined ”Relatively Absolute”. DZone, Inc. | wdww.
i v #dzs oanme p
. clom
e2 {position:
absolute; left: 65px; padding:
50px; background-color: red; z-index: 2;}
6
tech facts at your fingertips
Core CSS: Part 111
Stacking Order: z-index, continued Figure 26 shows the results, with the red box coming to the foreground.
tutorials, cheatsheets, blogs, feature articles, source code and Sponsorship Opportunities 9 781934 238233
more. “DZone is a developer’s dream,” says PC Magazine. [email protected]
Copyright © 2008 DZone, 1nc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, Version 1.0
mechanical,
photocopying, or otherwise, without prior written permission of the publisher. Reference: The Zen of CSS Design, Molly E. Holzschlag and Dave Shea, Peachpit Press, February 2005.