Learn CSS Positioning in Ten Steps

 
<div id="text-1" class="tabbertab  " title="">
<h2>1. position:static</h2>
<p>The default positioning for all elements is <em>position:static</em>, which means the element is not positioned and occurs where it normally would in the document.</p>
<p>Normally you wouldn't specify this unless you needed to override a positioning that had been previously set.</p>
<pre>#div-1 {
 position:static;
}
</pre>
</div>
 
<div id="text-2" class="tabbertab  tabbertabhide" title="">
<h2>2. position:relative</h2>
<p>If you specify <em>position:relative</em>, then you can use <em>top</em> or <em>bottom</em>, and <em>left</em> or <em>right</em> to move the element relative to where it would normally occur in the document.</p>
<p>Let's move div-1 down 20 pixels, and to the left 40 pixels:</p>
<pre>#div-1 {
 position:relative;
 top:20px;
 left:-40px;
}
</pre>
<p>Notice the space where div-1 normally would have been if we had not
moved it: now it is an empty space. The next element (div-after) did not
 move when we moved div-1. That's because div-1 still occupies that
original space in the document, even though we have moved it.</p>
<p>It appears that <em>position:relative</em> is not very useful, but it will perform an important task later in this tutorial.</p>
</div>
 
<div id="text-3" class="tabbertab  tabbertabhide" title="">
<h2>3. position:absolute</h2>
<p>When you specify <em>position:absolute</em>, the element is removed from the document and placed exactly where you tell it to go.</p>
<p>Let's move div-1a to the top right of the page:</p>
<pre>#div-1a {
 position:absolute;
 top:0;
 right:0;
 width:200px;
}
</pre>
<p>Notice that this time, since div-1a was removed from the document,
the other elements on the page were positioned differently: div-1b,
div-1c, and div-after moved up since div-1a was no longer there.</p>
<p>Also notice that div-1a was positioned in the top right corner of the
 page. It's nice to be able to position things directly the page, but
it's of limited value.</p>
<p>What I really want is to position div-1a <em>relative</em> to div-1. And that's where relative position comes back into play.</p>
<h3>Footnotes</h3>
<ul>
<li>There is a bug in the Windows IE browser: if you specify a relative
width (like "width:50%") then the width will be based on the parent
element instead of on the positioning element.</li>
</ul>
</div>
 
<div id="text-4" class="tabbertab tabbertabhide" title="">
<h2>4. position:relative + position:absolute</h2>
<p>If we set <em>relative</em> positioning on div-1, any elements within
 div-1 will be positioned relative to div-1. Then if we set absolute
positioning on div-1a, we can move it to the top right of div-1:</p>
<pre>#div-1 {
 position:relative;
}
#div-1a {
 position:absolute;
 top:0;
 right:0;
 width:200px;
}
</pre>
</div>
 
<div id="text-5" class="tabbertab tabbertabhide" title="">
<h2>5. two column absolute</h2>
<p>Now we can make a two-column layout using relative and absolute positioning!</p>
<pre>#div-1 {
 position:relative;
}
#div-1a {
 position:absolute;
 top:0;
 right:0;
 width:200px;
}
#div-1b {
 position:absolute;
 top:0;
 left:0;
 width:200px;
}
</pre>
<p>One advantage to using absolute positioning is that we can position
the elements in any order on the page, regardless of the order they
appear in the HTML. So I put div-1b before div-1a.</p>
<p>But wait - what happened to the other elements? They are being
obscured by the absolutely positioned elements. What can we do about
that?</p>
</div>
 
<div id="text-6" class="tabbertab tabbertabhide" title="">
<h2>6. two column absolute height</h2>
<p>One solution is to set a fixed height on the elements.</p>
<p>But that is not a viable solution for most designs, because we
usually do not know how much text will be in the elements, or the exact
font sizes that will be used.</p>
<pre>#div-1 {
 position:relative;
 height:250px;
}
#div-1a {
 position:absolute;
 top:0;
 right:0;
 width:200px;
}
#div-1b {
 position:absolute;
 top:0;
 left:0;
 width:200px;
}
</pre>
</div>
 
<div id="text-7" class="tabbertab tabbertabhide" title="">
<h2>7. float</h2>
<p>For variable height columns, absolute positioning does not work, so let's come up with another solution.</p>
<p>We can "float" an element to push it as far as possible to the right
or to the left, and allow text to wrap around it. This is typically used
 for images, but we will use it for more complex layout tasks (because
it's the only tool we have).</p>
<pre>#div-1a {
 float:left;
 width:200px;
}
</pre>
</div>
 
<div id="text-8" class="tabbertab tabbertabhide" title="">
<h2>8. float columns</h2>
<p>If we float one column to the left, then also float the second column to the left, they will push up against each other.</p>
<pre>#div-1a {
 float:left;
 width:150px;
}
#div-1b {
 float:left;
 width:150px;
}
</pre>
</div>
 
<div id="text-9" class="tabbertab tabbertabhide" title="">
<h2>9. float columns with clear</h2>
<p>Then after the floating elements we can "clear" the floats to push down the rest of the content.</p>
<pre>#div-1a {
 float:left;
 width:190px;
}
#div-1b {
 float:left;
 width:190px;
}
#div-1c {
 clear:both;
}
</pre>
</div>
 
<div id="text-9" class="tabbertab tabbertabhide" title="">
<h2>10. Disclaimer &amp; Resources</h2>
<p>These examples are extremely simplified and do not trigger some of the CSS bugs in the Windows IE browser (of which there are <em>many</em>).</p>
<p>The following page was invaluable:<br>
<a href=" http://www.autisticcuckoo.net/archive.php?id=2004/12/07/relatively-absolute">Relatively Absolute</a></p>
<p>While you're here check out the following:</p>
<ul>
<li><a href=" http://www.barelyfitz.com/projects/">BarelyFitz Designs Open-Source Software Projects</a></li>
<li><a href=" http://www.barelyfitz.com/screencast/">BarelyFitz Designs Screencasts</a></li>
</ul>
</div>
</div>

你可能感兴趣的:(Learn CSS Positioning in Ten Steps)