<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The CSS Blog, just CSS. (Tips, Tricks, Tutorials, Resources and more!) &#187; Tutorials</title>
	<atom:link href="http://thecssblog.com/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://thecssblog.com</link>
	<description>Tips, Tricks, Tutorials, Resources and more!</description>
	<lastBuildDate>Mon, 12 Jul 2010 20:42:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Stopping the CSS positioning panic (Part 2)</title>
		<link>http://thecssblog.com/tutorials/stopping-the-css-positioning-panic-part-2/</link>
		<comments>http://thecssblog.com/tutorials/stopping-the-css-positioning-panic-part-2/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 07:00:07 +0000</pubDate>
		<dc:creator>Nacho</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://thecssblog.com/?p=372</guid>
		<description><![CDATA[
Note: This article is meant for CSS beginners.
On Part 1 we focused on static, relative, absolute and fixed positioning. In the second part we will focus on the float property.
The float property is definetly one of the most used properties in CSS. You really have to know it because is vital to create our layouts.
Let&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p><a class="external" href="http://pekguo.deviantart.com/art/Levitate-sketch-60216479"><img class="postImg-left" src="http://thecssblog.com/wp-content/uploads/2009/07/levi.jpg" alt="levi.jpg" /></a></p>
<p><strong><em>Note: This article is meant for CSS beginners.</em></strong></p>
<p>On <a href="http://thecssblog.com/tutorials/stopping-the-css-positioning-panic-part-1/">Part 1</a> we focused on static, relative, absolute and fixed positioning. In the second part we will focus on the float property.</p>
<p>The <strong>float</strong> property is definetly one of the most used properties in CSS. You really have to know it because is vital to create our layouts.</p>
<p>Let&#8217;s get started&#8230;</p>
<p><span id="more-372"></span></p>
<p><span class="clearPost-img">First, take a look at <a class="external" href="http://thecssblog.com/demos/floats/floats-none.html">this article&#8217;s guinea pig</a>. We&#8217;ll experiment on it.<br />
</span></p>
<p>By default in our HTML pages, elements have a 100% width and they place themselves one on top of each other.<br />
Floats exist in order to change this. <strong>Their main aim is to show elements parallel to each other.</strong><br />
Also when you float something, it&#8217;s <strong>width</strong> reduces just to fit it&#8217;s content (unless you specify a determined width size), and stops having the full width of the browser.</p>
<p>Everything will become clearer when we see some examples. Let&#8217;s proceed&#8230;</p>
<h4>The float options</h4>
<p>These are:</p>
<ul>
<li>float:<strong>left</strong>;</li>
<li>float:<strong>right</strong>;</li>
<li>float:<strong>none</strong>;</li>
</ul>
<p><em>We can float anything.</em> Divs, paragraphs, images, lists, any HTML tag.<br />
The way to use it is always the same, but sometimes we use floats for pieces of content and other times for blocks in order to create a layout.</p>
<h4>Let&#8217;s float</h4>
<p>To explain myself better, I will separate the examples in floats related to <strong>content</strong> and floats related to <strong>blocks</strong>, but it&#8217;s really the same thing don&#8217;t worry.</p>
<h5>Floating content</h5>
<h6>The CSS</h6>
<pre>img {<strong>float:left;</strong>}
blockquote {<strong>float:right;</strong> width:230px;}</pre>
<p>I&#8217;m defining a width for the quote to look more compact.</p>
<h6>The result</h6>
<p><a class="external" href="http://thecssblog.com/demos/floats/floats-content.html">Floating content result</a> <em>(View source or inspect with Firebug)</em>.</p>
<p>You see how the elements flow along with them instead of being one on top of the other? That&#8217;s cool. However, there is a problem there.</p>
<h6>The problem</h6>
<ul>
<li>The #<strong>content</strong> element looses the correct bottom padding.</li>
</ul>
<p>Let&#8217;s proceed to block floating and we&#8217;ll fix this problem in just a sec.</p>
<h5>Floating blocks</h5>
<h6>The CSS</h6>
<pre>#wrapper {width:800px; margin:50px auto;}
	#content {<strong>float:left;</strong> width:450px;}
	#sidebar {<strong>float:right;</strong> width:200px;}</pre>
<p><strong>Why do we define a width for our wrapper element?</strong></p>
<p>Because floats need to be contained. Otherwise, they will end up floating to the edge of the browser. Sometimes that is useful, but not generally.<br />
Two examples of what happens if we have a containing element for floats or not:</p>
<p><strong>Floats without a container element:</strong></p>
<p><a class="external" title="Open bigger in a new window" href="http://thecssblog.com/wp-content/uploads/2009/07/float-uncontained.png"><img src="http://thecssblog.com/wp-content/uploads/2009/07/float-uncontained.png" alt="Float Un-contained" width="500" height="351" /></a></p>
<p><strong>Floats with a container element</strong></p>
<p><a class="external" title="Open bigger in a new window" href="http://thecssblog.com/wp-content/uploads/2009/07/float-contained.png"><img src="http://thecssblog.com/wp-content/uploads/2009/07/float-contained.png" alt="Float Contained" width="500" height="351" /></a></p>
<h6>The result</h6>
<p><a class="external" href="http://thecssblog.com/demos/floats/floats-blocks.html">Floating blocks result</a> <em>(View source or inspect with Firebug)</em>.</p>
<p>That looks awful!</p>
<h6>The problems</h6>
<ul>
<li>The <strong>#main</strong> element is not holding two of it&#8217;s children (#content and #sidebar)</li>
<li>The <strong>#footer</strong> element is totally jammed.</li>
</ul>
<p><strong>As I told you, both examples have problems.</strong> Of course the second one is the most obvious. Don&#8217;t freak out, this was supposed to happen. Let&#8217;s solve the problems with one line.</p>
<h4>Clearing floats</h4>
<p>The sister property of floats is <strong>clear</strong>.</p>
<p>The clear property has three possibilities as well:</p>
<ul>
<li>clear:<strong>left</strong>;</li>
<li>clear:<strong>right</strong>;</li>
<li>clear:<strong>both</strong>;</li>
</ul>
<p>This property will be the one that will solve our problems. Basically the clear property is the one that says: &#8220;Hey! Stop floating guys&#8221;.<br />
It must be <strong>applied to the first element that will not float</strong> located <em>after an element currently floating</em>.</p>
<p>Huh? Don&#8217;t worry, here you have two examples:</p>
<h5>Clearing content</h5>
<h6>The CSS</h6>
<pre>#last {<strong>clear:both;</strong>}</pre>
<h6>The example</h6>
<p><a class="external" href="http://thecssblog.com/demos/floats/floats-content-fix.html">Clearing content example</a> <em>(View source or inspect with Firebug)</em>.</p>
<p>I&#8217;m clearing both, because I have elements floating left (images) and other floating right (blockquote). If I only have left floating elements, I can just clear left. The same case for elements floating right.</p>
<h5>Clearing blocks</h5>
<p>Same idea. The thing I don&#8217;t want to float is the footer. Let&#8217;s apply a clear to it:</p>
<pre>#footer {<strong>clear:both;</strong>}</pre>
<p>Now the footer is clearing the #content and #sidebar elements. This helps the container element (#main), to succesfuly wrap around it&#8217;s children once again.</p>
<p><a class="external" href="http://thecssblog.com/demos/floats/floats-blocks-fix.html">Clearing blocks example</a> <em>(View source or inspect with Firebug)</em>.</p>
<p>That&#8217;s easier than you expected right? <img src='http://thecssblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h4>What about float:none;?</h4>
<p>Float none is used to override previous floats. If nothing is floating, the float none property won&#8217;t do anything. <strong>Float none doesn&#8217;t mean clearing a float!</strong> They are two different things.</p>
<h5>Example:</h5>
<pre>#wrapper div {float:left;}</pre>
<p>I&#8217;m telling all the divs inside an element with id &#8216;wrapper&#8217; to float left.<br />
If I don&#8217;t want the last div inside #wrapper to float, I should apply an id to it and make an override.</p>
<pre>#wrapper div {float:left;}
	<strong>#wrapper #lastDiv {float:none; clear:both;}</strong></pre>
<p>So that&#8217;s the use for float none. I also used a clear to stop the floating chain inside the #wrapper element.</p>
<h4>Fixing floats when not having a clearing element</h4>
<p>Sometimes we can&#8217;t have a clearing element right next to the last floating element. For example:</p>
<h5>HTML code</h5>
<pre>&lt;ul&gt;
	&lt;li&gt;First list item&lt;/li&gt;
	&lt;li&gt;Second list item&lt;/li&gt;
	&lt;li&gt;Third list item&lt;/li&gt;
	&lt;li&gt;Fourth list item&lt;/li&gt;
	&lt;li&gt;Fifth list item&lt;/li&gt;
&lt;/ul&gt;</pre>
<h5>CSS code</h5>
<pre>li {float:left;}</pre>
<h5>So what do we do?</h5>
<p>This?</p>
<h6>HTML code</h6>
<pre>&lt;ul&gt;
	&lt;li&gt;First list item&lt;/li&gt;
	&lt;li&gt;Second list item&lt;/li&gt;
	&lt;li&gt;Third list item&lt;/li&gt;
	&lt;li&gt;Fourth list item&lt;/li&gt;
	&lt;li&gt;Fifth list item&lt;/li&gt;
	<strong>&lt;li class="last"&gt;&lt;/li&gt;</strong>
&lt;/ul&gt;</pre>
<h6>CSS code</h6>
<pre>.last {<strong>float:none; clear:both;</strong>}</pre>
<p><strong>No way!</strong> That works fine, but is really nasty.</p>
<h5>The solution:</h5>
<p>Actually, there are two.</p>
<h6>Solution 1 (floating + width)</h6>
<p>Give the parent element of the floats a float too and a width.</p>
<p><strong>CSS code</strong></p>
<pre>ul {<strong>float:left; width:100%;</strong>}</pre>
<p>Why a width? Because floated elements have an automatic width as I told you before. The UL must have a 100% width to wrap around all it&#8217;s list items.</p>
<p>This works in every browser. <strong>However</strong>, it has a problem. Sometimes giving elements a width of 100% is problematic. This happens in cases in which we have paddings for example. And on top of this, we need to give the next element (after the ul) a clearing property also.</p>
<p>To work around this, here is another solution:</p>
<h6>Solution 2 (overflow hidden)</h6>
<pre>ul {<strong>overflow:hidden;</strong>}</pre>
<p>We must give an overflow hidden (auto works too) to the container element of the floats. This works perfectly in Firefox, Opera, Safari, Chrome and IE8. Most of the times in IE6 and IE7.</p>
<p>To fix it in IE6 and IE7 browsers, take a look at <a href="http://thecssblog.com/tips-and-tricks/reduce-your-headache-when-developing-for-ie6-ie7-with-just-one-line/">one of my previous articles</a>.</p>
<p><strong>This will always work when you don&#8217;t have a fixed height.</strong> Why? Because if we have a fixed height the overflow property will either not show the content or add scrollbars, and that sucks.<br />
If you have a fixed height for the floating container use the first solution.</p>
<p>See a <a class="external" href="http://thecssblog.com/demos/floats/floats-blocks-fix-2.html">live example of solution 2</a>.</p>
<h4>Float stacking</h4>
<p>I think this is worth mentioning because some people get puzzled with this too.<br />
Floats stack horizontally. This stacking is not the same for left floated elements than right ones.</p>
<h5>Left stacking:</h5>
<p><img src="http://thecssblog.com/wp-content/uploads/2009/07/left-stack.gif" alt="Left stacking" /></p>
<p>This is simple. Now let&#8217;s look at float right.</p>
<h5>Right stacking:</h5>
<p><img src="http://thecssblog.com/wp-content/uploads/2009/07/right-stack.gif" alt="Right Stacking" /></p>
<p><a class="external" href="http://thecssblog.com/demos/floats/floats-stacks.html">Take a look at this in an HTML example</a> <em>(View source code or inspect with firebug)</em><br />
You see? <strong>The first element is actually the last one visually.</strong> Keep that in mind when you are floating right.</p>
<h4>Internet Explorer 6 (IE6) and floats</h4>
<p>Sometimes when you float elements in IE6, a known bug called: &#8216;<strong>The double margin bug</strong>&#8216; may appear.</p>
<h5>What does the bug do?</h5>
<p>As it names suggests, the browser duplicates the margin of the floated element.<br />
This issue can break our layouts; we must fix it.</p>
<h5>How can you prevent it?</h5>
<p>By never giving the floated element the same direction of it&#8217;s margin.<br />
If you float <strong>left</strong>, don&#8217;t use <strong>margin-left</strong>. If you float right, don&#8217;t use margin-right.</p>
<h5>Can&#8217;t prevent it? the solution:</h5>
<p>You can use this hack if you can&#8217;t help using the same side margin:</p>
<pre>* html div {<strong>display:inline;</strong>}</pre>
<p>It works great. Don&#8217;t really ask why. It&#8217;s IE6 :p</p>
<h4>Optional download</h4>
<p><a class="external" href="http://thecssblog.com/download/floats.zip">Download all the example files here</a></p>
<h4>Final thoughts</h4>
<p>I hope this second article helped you to understand the second part of CSS positioning: <strong>floats</strong>. This is actually one of the most complex stuff in CSS. You have to practice in order to get a hang of it. When you succeed in controlling it, it&#8217;s totally worth it.</p>
<p>Thank you all for the support in this first week of the blog and see you soon with more articles <img src='http://thecssblog.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://thecssblog.com/tutorials/stopping-the-css-positioning-panic-part-2/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Stopping the CSS positioning panic (Part 1)</title>
		<link>http://thecssblog.com/tutorials/stopping-the-css-positioning-panic-part-1/</link>
		<comments>http://thecssblog.com/tutorials/stopping-the-css-positioning-panic-part-1/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 17:24:41 +0000</pubDate>
		<dc:creator>Nacho</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://thecssblog.com/?p=212</guid>
		<description><![CDATA[
Note: This article is meant for CSS beginners.
For quite some time I&#8217;ve been noticing that when my friends begin to dig their noses on CSS they are easily freaked out and frustrated with CSS positioning.
Static, relative, absolute, fixed, float. What the hell is all this!

I visited Twitter to see if this was a general concern [...]]]></description>
			<content:encoded><![CDATA[<p><a class="external" href="http://adriana7.deviantart.com/art/Fear-me-24168272"><img class="postImg-left" src="http://thecssblog.com/wp-content/uploads/2009/07/cssbat.jpg" alt="The CSS Batman" /></a></p>
<p><strong><em>Note: This article is meant for CSS beginners.</em></strong></p>
<p>For quite some time I&#8217;ve been noticing that when my friends begin to dig their noses on CSS they are easily freaked out and frustrated with CSS positioning.</p>
<p>Static, relative, absolute, fixed, float. <strong>What the hell is all this!</strong></p>
<p><span id="more-212"></span></p>
<p><span class="clearPost-img">I visited Twitter to see if this was a general concern among CSS Beginners and I found that it actually is. Here are some tweets over the last week related to this subject:</span></p>
<blockquote><p><a class="external" href="http://twitter.com/mxcl/status/2440218872">@mxcl</a><br />
Man I hate CSS positioning. Isn&#8217;t there an alternative to tables yet?</p>
<p><a class="external" href="http://twitter.com/meredithwhitney/status/2425529881">@MeredithWhitney</a><br />
now that i know how to use ‹ div›s in css.. i just need to learn how to position them properly. getting there</p>
<p><a class="external" href="http://twitter.com/Plagiarizr/status/2354352388">@Plagiarizr</a><br />
I&#8217;ve been trying to position that fucking twitter icon for 2 hours now. God damn, my CSS skills are dead.</p>
<p><a class="external" href="http://twitter.com/imnk/status/2317945153">@iMNK</a><br />
decided that I hate CSS. hrs spent trying to position things 4 a site and no further on. mite look seriously at Ruby on Rails. i &lt;3 OOP</p>
<p><a class="external" href="http://twitter.com/SirLyric/status/2316721891">@SirLyric</a><br />
I win at CSS. On the other hand, it shouldn&#8217;t take 4 position: relatives and 4 floats to get the effect I need.</p></blockquote>
<p>CSS positioning is quite easy. I guess no-one explained it plain and simple before.<br />
I want to be crystal clear about the subject so I decided to separate the post into two parts.</p>
<p>The first part (this post) will focus on the four types of CSS positioning: <strong>static, relative, absolute and fixed</strong>.</p>
<p>The second part will focus on the <strong>float</strong> property that is obviously related to an element&#8217;s position in our site.</p>
<p>Let&#8217;s get started.</p>
<h4>Position static {position:static;}</h4>
<p>All the elements in our HTML document have a static position by default unless we specify another one. This position is how an HTML element looks like if it hasn&#8217;t any styles applied to it. This basically means a bunch of content blocks stacked one on top of the other.</p>
<p>These blocks have a 100% width unless we decide otherwise.</p>
<p>That&#8217;s why applying <strong>position:static;</strong> to an element in our CSS code is redundant, it does nothing.</p>
<p><a class="external" href="http://thecssblog.com/demos/css-positioning-part1/position-static.html">Position static example</a> <em>(View the source code or inspect with Firebug)</em>.</p>
<h5>¿A CSS position to make something default for every browser?</h5>
<p>Yes, it&#8217;s necessary because of the cascading styles. Position static in our CSS code will be always used to <strong>override</strong> a previous position type from the cascade if we need to.</p>
<p>So that&#8217;s position static. The default one. Easy right? Let&#8217;s proceed to the other ones&#8230;</p>
<h4>The other types of positioning (relative, absolute and fixed)</h4>
<p>First of all, I need to say that position relative, absolute and fixed are deeply related with other CSS properties:</p>
<ul>
<li>top</li>
<li>bottom</li>
<li>left</li>
<li>right</li>
<li>z-index</li>
</ul>
<p>Without them, these types of positioning won&#8217;t work as expected.</p>
<p>The top, bottom, left and right property values are generally defined in pixels. And they can be both positive or negative. <strong>(Example: top:1px; left:-2px;)</strong></p>
<p>The z-index property values are just numbers (Example: 1, 2, 50, 150).</p>
<h5>Top, bottom, left and right</h5>
<p>These properties work with reference points. These points are different when dealing with relative, absolute or fixed positioning.<br />
Further on the post I&#8217;ll explain the reference points for each type.</p>
<h5>Z-index, what&#8217;s that?</h5>
<p>Back to school and maths guys!</p>
<p>The <strong>X</strong> axis is related to the <em>horizontal</em> measure or movement.<br />
The <strong>Y</strong> axis is related to the <em>vertical</em> measure or movement.<br />
The <strong>Z</strong> axis is related to the <em>depth</em> measure or movement.</p>
<p>Of course web is 2D, but elements can be on top of the other using CSS positioning (relative, absolute, fixed).<br />
That&#8217;s why it is related with z axis, we can achieve some kind of &#8216;depth&#8217;.</p>
<p>An element with a <em>z-index:3;</em> will be on top of an element with a <em>z-index:1;</em><br />
The number defines the order of the elements.</p>
<p><strong>Higher number = in front, lower number = the back.</strong></p>
<h6>Simple examples:</h6>
<p><strong>As a graph:</strong></p>
<p><img src="http://thecssblog.com/wp-content/uploads/2009/07/cssdepth.gif" alt="Z-index as a Graph" /></p>
<p><strong>As photoshop layers:</strong></p>
<p><img src="http://thecssblog.com/wp-content/uploads/2009/07/zindex-ps.gif" alt="Z-index as Photoshop Layers" /></p>
<p>I&#8217;ll show an HTML example later on to finish this idea. For now just keep reading <img src='http://thecssblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h5>What do position relative, absolute and fixed share in common?</h5>
<p><strong>They separate themselves from the content&#8217;s flow.</strong> They become <strong>independent</strong> in a new layer of content.<br />
By doing this, their width is affected; it&#8217;s not 100% anymore. They will have the width needed for it&#8217;s content to fit, unless we apply a determined width size.</p>
<p>When we look at some examples next you&#8217;ll understand this better.<br />
Now let&#8217;s look at the different positions and their reference points&#8230;</p>
<h4>Position relative {position:relative;}</h4>
<p>The reference points for relatively-positioned elements are defined by their size.<br />
An example of this in an image:</p>
<p><img src="http://thecssblog.com/wp-content/uploads/2009/07/beattle2b.gif" alt="The beatle image" /></p>
<p>The images has a fixed size and it&#8217;s corners define the reference points.<br />
Let&#8217;s move the beetle using position relative:</p>
<h5>The css code</h5>
<pre>img {position:relative; top:-30px; right:-30px;}</pre>
<h5>The result</h5>
<p><img src="http://thecssblog.com/wp-content/uploads/2009/07/beattle2a.gif" alt="Beatle Movement with position relative" /></p>
<p>In relative positioning we can use one property, or a pair to define movement. The possibilities are:</p>
<ol>
<li>Top</li>
<li>Right</li>
<li>Bottom</li>
<li>Left</li>
<li>Top <strong>+</strong> Left</li>
<li>Top <strong>+</strong> Right</li>
<li>Bottom <strong>+</strong> Left</li>
<li>Bottom <strong>+</strong> Right</li>
</ol>
<p>To help you understand how the element moves when it&#8217;s property has a positive or negative value here is another image:</p>
<p><em>(Click on it for a bigger size)</em></p>
<p><a class="external" title="Click to open in a bigger size" href="http://thecssblog.com/wp-content/uploads/2009/07/beatle-positions.gif"><img src="http://thecssblog.com/wp-content/uploads/2009/07/beatle-positions.gif" border="0" alt="CSS top, bottom, right and left movements" width="552" height="322" /></a></p>
<h6>Is the same using position relative and a margin?</h6>
<p>No, definetely not. You can see why <a class="external" href="http://thecssblog.com/demos/css-positioning-part1/position-relative-and-margin.html">in this example</a> <em>(View the source code or inspect with Firebug)</em>.</p>
<p>As I told you before, position relative (and absolute and fixed) separate themselves from the content&#8217;s flow, that&#8217;s why the element places itself on top of the previous one in this case, while the gray paragraphs with a top margin continue to flow along with the content.</p>
<h4>Position absolute and fixed</h4>
<p>In absolute and fixed positioning the top, right, bottom and left properties must be used <strong>always</strong> in pair. The possibilities are:</p>
<ol>
<li>Top <strong>+</strong> Left</li>
<li>Top <strong>+</strong> Right</li>
<li>Bottom <strong>+</strong> Left</li>
<li>Bottom <strong>+</strong> Right</li>
</ol>
<p><strong>Why these pairs on relative, absolute and fixed positioning?</strong></p>
<p>Because the browser needs reference points from where to move that object.<br />
If you use a top and bottom property the browser gets puzzled, it doesn&#8217;t know in which direction to move it because the reference point is unclear. The same happens when using left and right at the same time.</p>
<p>Their reference points are related to the body tag, the element that wraps all the webpage:</p>
<p><em>(Click on it for a bigger size)</em></p>
<p><a class="external" title="CSS absolute and fixed positioning reference points" href="http://thecssblog.com/wp-content/uploads/2009/07/ss-position.png"><img src="http://thecssblog.com/wp-content/uploads/2009/07/ss-position.png" alt="CSS absolute and fixed positioning reference points" width="562" height="413" /></a></p>
<p>However they are not the same thing&#8230;</p>
<h5>Position absolute {position:absolute;}</h5>
<h6>Position absolute with default reference points (the body tag)</h6>
<p><strong>Code example:</strong></p>
<p>HTML</p>
<pre>&lt;div class="theParent"&gt;
     &lt;div class="theAbsoluteChild"&gt;The absolute child content&lt;/div&gt;
&lt;/div&gt;</pre>
<p>CSS</p>
<pre>.theParent {}
     .theAbsoluteChild {position:absolute; top:0; left:0; z-index:1;}</pre>
<p><a class="external" href="http://thecssblog.com/demos/css-positioning-part1/position-absolute.html">Click here to take a look at the example</a> <em>(View the source code or inspect with Firebug)</em>.</p>
<p>As you can see, absolute positioned element&#8217;s don&#8217;t care even about their parent element.</p>
<h6>Position absolute with custom reference points</h6>
<p>The good thing about position absolute is that it&#8217;s the only type in which we can change it&#8217;s reference points. So they DO take their parent element into consideration. How we can do that? By making another kind of relationship between the absolute positioned element, and it&#8217;s parent (that will be the one defining the points).</p>
<p>To create custom reference points for absolute positioned elements we must assign to it&#8217;s <strong>parent</strong> element a position <strong>relative</strong> property.</p>
<p><strong>Code example:</strong></p>
<p>HTML</p>
<pre>&lt;div class="theParent"&gt;
     &lt;div class="theAbsoluteChild"&gt;The absolute child content&lt;/div&gt;
&lt;/div&gt;</pre>
<p>CSS</p>
<pre>.theParent {<strong>position:relative;</strong>}
     .theAbsoluteChild {position:absolute; top:0; left:0; z-index:1;}</pre>
<p><a class="external" href="http://thecssblog.com/demos/css-positioning-part1/position-absolute-and-relative.html">Click here to take a look at the example</a>. <em>(View the source code or inspect with Firebug)</em>.</p>
<p><em>When an absolute positioned element doesn&#8217;t have a parent element with position relative applied to it, <strong>it will use it&#8217;s default reference points</strong>. The onces related with the body tag.</em></p>
<p>In the example <strong>the coordinate 0 0 (top:0; left:0;)</strong> is related to the first top left pixel where the parent element starts.</p>
<p>If the element has 5 parent tags, it doesn&#8217;t matter, you can choose any of them. <strong>The closest parent to the absolute positioned element will be the reference.</strong></p>
<p>That&#8217;s easy right? And kind of useful <img src='http://thecssblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h5>Position fixed {position:fixed;}</h5>
<p>Fixed position looks quite the same as absolute at first, but it&#8217;s not really like this. There are three differences:</p>
<ol>
<li>It&#8217;s reference points are always related to the body tag. We <strong>can&#8217;t relate it with a parent element.</strong> It ignores it.</li>
<li>While in absolute the element stays in one place, in the fixed position it <strong>moves along with the scroll bar</strong> <em>(See example link below)</em></li>
<li>It&#8217;s almost 100% cross-browser except for <strong>IE6 that doesn&#8217;t understand the property</strong> (there are hacks to fix it if you like, google it)</li>
</ol>
<p>All this is better explained with a simple example: <a class="external" href="http://thecssblog.com/demos/css-positioning-part1/position-fixed.html">Position fixed example</a> <em>(View the source code or inspect with Firebug)</em>. <strong>Scroll to check it.</strong></p>
<h5>Last thing: z-index in an HTML example</h5>
<p><a class="external" href="http://thecssblog.com/demos/css-positioning-part1/position-z-index.html">View the z-index example</a>. <em>(View the source code or inspect with Firebug).</em></p>
<h4>Optional Download</h4>
<p>Here you can <a class="external" href="http://thecssblog.com/download/css-positioning-part1.zip">download all the example files</a>.</p>
<h4>Final thoughts</h4>
<p>Sites are usually done with 95% static elements with float properties (soon in part 2).<br />
Anyway, it&#8217;s important for you to know these properties because they are used frequently in sites. But <strong>be selective</strong>.<br />
It has no sense making everything absolute or relative.</p>
<p>I&#8217;ll explain floats soon so you can have a 100% knowledge on positioning of CSS, and decide for yourself when to use each type of position.</p>
<p>I hope the article helps you out and is easy to understand. I tried my best to accomplish that. See you soon with floats <img src='http://thecssblog.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://thecssblog.com/tutorials/stopping-the-css-positioning-panic-part-1/feed/</wfw:commentRss>
		<slash:comments>107</slash:comments>
		</item>
		<item>
		<title>Creating rounded corners (the &#8220;Deviant art&#8217;s&#8221; way)</title>
		<link>http://thecssblog.com/tutorials/creating-rounded-corners-the-deviant-arts-way/</link>
		<comments>http://thecssblog.com/tutorials/creating-rounded-corners-the-deviant-arts-way/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 12:48:43 +0000</pubDate>
		<dc:creator>Nacho</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://thecssblog.com/?p=17</guid>
		<description><![CDATA[One of my favorite sites is definetely DeviantArt. Is a great source of inspiration when you are a bit lost, and it also has a great amount of brush resources and free stock photography. So check it out  
I was browsing the site yesterday and I wanted to know how they were doing their [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favorite sites is definetely <a title="DeviantArt" href="http://www.deviantart.com" target="_blank">DeviantArt</a>. Is a great source of inspiration when you are a bit lost, and it also has a great amount of brush resources and free stock photography. So check it out <img src='http://thecssblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I was browsing the site yesterday and I wanted to know how they were doing their rounded corners. So I grabbed my little friend <a title="Get firebug!" href="http://getfirebug.com/" target="_blank">Firebug</a> (if you don&#8217;t have it, go and get it. It&#8217;s definitely a must) and I inspected them. I found what they were doing very interesting and I wanted to share it with you.</p>
<p><span id="more-17"></span></p>
<h4>Demo</h4>
<p>Because we always understand it better with a demo:</p>
<p><a href="http://www.thecssblog.com/demos/deviantart-corners/" target="_blank">Visit an online demonstration</a>.</p>
<h4>Why their technique is cool?</h4>
<ul>
<li>No images</li>
<li>100% Crossbrowser (Tested in IE6, IE7, IE8, FF, Safari, Opera and Chrome)</li>
<li>Dimention freedom. Can have any size and never break.</li>
<li>You can have multiple box styles by just changing few lines in your css.</li>
<li>Lightweight</li>
</ul>
<p>Well, here is the catch. We must generate a code that is not semantic. A bunch of spans/divs or whatever you like with no content.</p>
<p><strong>Then why are you showing me this?</strong><br />
Because with a little <em>javascript</em> (Jquery) we can include these elements easily and POOF, our un-semantic code is gone <img src='http://thecssblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Let&#8217;s get started.</p>
<h4>The HTML</h4>
<p>Never mind the class names. They are for explainatory purposes. Use shorter names if you want of course.</p>
<h5>The final code when the javascript is done:</h5>
<pre>&lt;div class="roundedBox"&gt;
     &lt;div class="roundedBox-content"&gt;
          &lt;p&gt;The content of the rounded box.&lt;/p&gt;
     &lt;/div&gt;
&lt;/div&gt;</pre>
<p>The inner div is necessary. You&#8217;ll see that later.</p>
<h5>The code before making our javascript:</h5>
<p>The amount of tags we need will be relative to the corner size. The bigger the corner, the more tags we need.</p>
<p>You can choose any HTML tag you want to make this. I will be using divs and spans.<br />
The tags that will create our corners are the following:</p>
<h6>The top corners</h6>
<pre>&lt;div class="c t t1"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="c t t2"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="c t t3"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="c t t4"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="c t t5"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="c t t6"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;</pre>
<p>Class <strong>c</strong> stands for corner. Class <strong>t</strong> stands for top, and class <strong>tx</strong> for the number.</p>
<h6>The bottom corners</h6>
<pre>&lt;div class="c b b1"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="c b b2"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="c b b3"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="c b b4"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="c b b5"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="c b b6"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;</pre>
<p>Class <strong>b</strong> stands for bottom and class <strong>bx</strong> for the number.</p>
<p>I&#8217;m using 6 divs due to my corner&#8217;s size. This will depend on the corner radius you are planning to make (as I said before).</p>
<h6>This would be the final code for each box before creating the javascript:</h6>
<pre><strong>&lt;div class="roundedBox"&gt;
</strong>     &lt;div class="c t t1"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c t t2"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c t t3"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c t t4"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c t t5"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c t t6"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
<strong>     &lt;div class="roundedBox-content"&gt;&lt;/div&gt;
          &lt;p&gt;The content of the rounded box.&lt;/p&gt;
     &lt;/div&gt;
</strong>     &lt;div class="c b b1"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c b b2"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c b b3"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c b b4"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c b b5"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c b b6"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
<strong>&lt;/div&gt;</strong></pre>
<p>As you can see, if we don&#8217;t integrate this technique with javascript it totally sucks, because it&#8217;s extremely hard to manage and maintain. But with the integration with Jquery it goes really smooth. When we create new content we won&#8217;t even worry about this.</p>
<p>Now let&#8217;s look at the CSS Code, the one that makes the magic.</p>
<h4>The CSS code</h4>
<p>My divs will have borders. To to this, I will assign a border on both left and right sides of the extra elements, and give them a margin too. With only that, the corners will be done <img src='http://thecssblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h6>1) Simple reset + body tag</h6>
<pre>* {padding:0; margin:0;}
     body {background:#D6DED4; font:85%/150% Helvetica, Arial, sans-serif; padding:20px 0;}</pre>
<h6>2) The box</h6>
<pre>.cornerBox {width:600px; margin:50px auto;}
     .cornerBox-content {padding:20px;}</pre>
<ol>
<li>We define a width and margin <em>(just for the example. It&#8217;s not really necessary).</em></li>
<li>We apply a padding to the content <em>(just for the example too)</em>.</li>
</ol>
<h6>3) The corners and their borders</h6>
<pre>.c, .c span {height:1px;}
     .c span {display:block; overflow:hidden;}

     /* borders */
     .cornerBox-content,
     .c span {border-left:1px solid; border-right:1px solid;}</pre>
<p>We define a height of 1px to every extra element and apply a display:block; to the span inside. <em>The overflow hidden property is there due to IE6</em>. If we don&#8217;t use it, it creates something  weird.</p>
<h6>4) Making the curve:</h6>
<p>A rounded cornered box has a symmetry between the top curve, and the bottom one:</p>
<p><img src="http://thecssblog.com/wp-content/uploads/2009/07/cornerexplain-2.gif" alt="Inverted corners" /></p>
<p>As this little sketch shows, the first top element&#8217;s width <strong>(t1)</strong> is equal to the last bottom&#8217;s one <strong>(b6)</strong>. This can be applied to the rest of the elements too, because their distances are just the same, but inverted.</p>
<p>Taking this into consideration, we can define the left and right margins to both the original and the inverse element at the same time. So, let&#8217;s create the curve <img src='http://thecssblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre>
.t1, .b6 {margin:0 7px;}
.t2, .b5 {margin:0 5px;}
.t3, .b4 {margin:0 3px;}
.t4, .b3 {margin:0 2px;}
.t5, .b2 {margin:0 2px;}
.t6, .b1 {margin:0 1px;}
     .t1 span {border-top:1px solid;}
     .b6 span {border-bottom:1px solid;}
</pre>
<p>Of course the first top element <strong>(t1)</strong> and the last bottom element<strong> (b6)</strong> have a top and bottom border to assure that the border doesn&#8217;t break.</p>
<p>The code is ready. Now we just have to add id&#8217;s or classes to style each box as we like. We can style them completely differently with just a few lines. I&#8217;ve got three examples:</p>
<h5>Type 1: #cBox1</h5>
<h6>The code:</h6>
<pre>
/* Box 1 */
#cBox1 .cornerBox-content,
#cBox1 .c span {background:#FFF; border-color:#BED2CD;}
</pre>
<h6>The result:</h6>
<p><img src="http://thecssblog.com/wp-content/uploads/2009/07/da-box1b.gif" alt="Type 1" /></p>
<h5>Type 2: #cBox2 (Other colors)</h5>
<h6>The code:</h6>
<pre>
/* Box 2 */
#cBox2 .cornerBox-content,
#cBox2 .c span {background:#728776; border-color:#4E6257; color:#FFF;}
</pre>
<h6>The result:</h6>
<p><img src="http://thecssblog.com/wp-content/uploads/2009/07/da-box2.gif" alt="Type 2" /></p>
<h5>Type 3: #cBox3 (With a gradient)</h5>
<h6>The code:</h6>
<pre>
/* Box 3 */
#cBox3 .cornerBox-content,
#cBox3 .c span {border-color:#728776;}	

#cBox3 .t span {background:#C5D3C3;}
#cBox3 .b span {background:#B2C3B0;}

#cBox3 .cornerBox-content {color:#333F35; background:#B1C3B0 url(../images/box3.png) repeat-x 0 0;}
</pre>
<h6>The result:</h6>
<p><img src="http://thecssblog.com/wp-content/uploads/2009/07/da-box3.png" alt="Type 3" /></p>
<p>That&#8217;s easy!</p>
<h4>Finally, the Javascript (Jquery)</h4>
<p>Create a variable for the top corners and another one for the bottom corners. They will have all the extra elements.</p>
<pre>var <strong>topCorners</strong> = '
     &lt;div class="c t t1"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c t t2"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c t t3"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c t t4"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c t t5"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c t t6"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
';
var <strong>bottomCorners</strong> = '
     &lt;div class="c b b1"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c b b2"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c b b3"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c b b4"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c b b5"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
     &lt;div class="c b b6"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
';</pre>
<p>Now apply them before and after the box&#8217;s content:</p>
<pre>
$('<strong>.cornerBox-content</strong>').each(function(){
     $(this).before(topCorners);
     $(this).after(bottomCorners);
});
</pre>
<p>That&#8217;s it! <img src='http://thecssblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h4>Final thoughts</h4>
<p>The smaller the corner, the less noticable is the element separation. A less contrasting border from the background too.<br />
Because whether you like it or not the corners seem kind of pixeled.</p>
<p>The technique is very versatile and especially interesting to use in large sites. Be creative and explore the possibilities&#8230;</p>
<h4>Demo + Download</h4>
<ul>
<li><a href="http://www.thecssblog.com/demos/deviantart-corners/" target="_blank">Visit an online demonstration</a>.</li>
<li><a href="http://www.thecssblog.com/download/deviantart-corners.zip" target="_blank">Download the files</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://thecssblog.com/tutorials/creating-rounded-corners-the-deviant-arts-way/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
	</channel>
</rss>
