﻿{"id":417,"date":"2009-12-11T11:33:00","date_gmt":"2009-12-11T11:33:00","guid":{"rendered":"https:\/\/chayilyam.com\/stories\/?p=417"},"modified":"2009-12-11T11:33:00","modified_gmt":"2009-12-11T11:33:00","slug":"css-positioning","status":"publish","type":"post","link":"https:\/\/chayilyam.com\/stories\/css-positioning\/","title":{"rendered":"CSS Positioning"},"content":{"rendered":"<div>Position:Static<\/div>\n<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.<br \/>Normally you wouldn&#8217;t specify this unless you needed to override a positioning that had been previously set.<\/p>\n<pre>#div-1 {   <br \/>position:static;  }<\/pre>\n<p>\u00a0<\/p>\n<div>Position:Relative<\/div>\n<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>\n<p>Let&#8217;s move div-1 down 20 pixels, and to the left 40 pixels:<\/p>\n<pre>#div-1 {   <br \/>position:relative;<br \/>top:20px;<br \/>left:-40px;  }  <\/pre>\n<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&#8217;s because div-1 still occupies that   original space in the document, even though we have moved it.<\/p>\n<p>It appears that <em>position:relative<\/em> is not very useful, but it will perform an important task later in this tutorial.<\/p>\n<p><\/p>\n<div>Position:Absolute<\/div>\n<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>\n<p>Let&#8217;s move div-1a to the top right of the page:<\/p>\n<pre>#div-1a {   <br \/>position:absolute;   <br \/>top:0;   <br \/>right:0;   <br \/>width:200px;  }  <\/pre>\n<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>\n<p>Also notice that div-1a was positioned in the top right corner of the   page. It&#8217;s nice to be able to position things directly the page, but   it&#8217;s of limited value.<\/p>\n<p>What I really want is to position div-1a <em>relative<\/em> to div-1. And that&#8217;s where relative position comes back into play.<\/p>\n<div>Footnotes<\/div>\n<ul>\n<li>There is a bug in the Windows IE browser: if you specify a relative   width (like &#8220;width:50%&#8221;) then the width will be based on the parent   element instead of on the positioning element.<\/li>\n<\/ul>\n<div>Position:Relative + Position:Absolute<\/div>\n<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>\n<pre>#div-1 {   <br \/>position:relative;  }  <br \/><br \/><br \/>#div-1a {   <br \/>position:absolute;   <br \/>top:0;   <br \/>right:0;   <br \/>width:200px;  }  <\/pre>\n<div>Two Column Absolute<\/div>\n<p>Now we can make a two-column layout using relative and absolute positioning!<\/p>\n<pre>#div-1 {   <br \/>position:relative;  }  <br \/><br \/><br \/>#div-1a {   <br \/>position:absolute;  <br \/> top:0;   <br \/>right:0;   <br \/>width:200px;  } <br \/><br \/><br \/> #div-1b {   <br \/>position:absolute;  <br \/> top:0;   <br \/>left:0;   <br \/>width:200px;  }  <\/pre>\n<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>\n<p>But wait &#8211; what happened to the other elements? They are being   obscured by the absolutely positioned elements. What can we do about   that?<\/p>\n<p>\u00a0<\/p>\n<div>float<\/div>\n<p>For variable height columns, absolute positioning does not work, so let&#8217;s come up with another solution.<\/p>\n<p>We can &#8220;float&#8221; 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&#8217;s the only tool we have).<\/p>\n<pre>#div-1a {   <br \/>float:left;  <br \/> width:200px;  }  <\/pre>\n<div>float columns<\/div>\n<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>\n<pre>#div-1a {  <br \/>float:left;   <br \/>width:150px;  }  <br \/><br \/><br \/>#div-1b {   <br \/>float:left;   <br \/>width:150px;  }  <\/pre>\n<p>    <\/p>\n<div>float columns with clear<\/div>\n<p>Then after the floating elements we can &#8220;clear&#8221; the floats to push down the rest of the content.<\/p>\n<pre>#div-1a {   <br \/>float:left;  <br \/> width:190px;  }  <br \/><br \/><br \/>#div-1b {   <br \/>float:left;  <br \/>width:190px;  }  <br \/><br \/><br \/>#div-1c {   <br \/>clear:both;  }  <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Position:Static The default positioning for all elements is position:static, which means the element is not positioned and occurs where it normally would in the document.Normally you wouldn&#8217;t specify this unless you needed to override a positioning that had been previously set. #div-1 { position:static; } \u00a0 Position:Relative If you specify position:relative, then you can use &hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[81,84,85,183],"tags":[1627,1646,1349],"class_list":["post-417","post","type-post","status-publish","format-standard","hentry","category-css","category-css-tags","category-css3","category-help","tag-css","tag-help","tag-tutorial"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pfoK1S-6J","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/chayilyam.com\/stories\/wp-json\/wp\/v2\/posts\/417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chayilyam.com\/stories\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chayilyam.com\/stories\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chayilyam.com\/stories\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chayilyam.com\/stories\/wp-json\/wp\/v2\/comments?post=417"}],"version-history":[{"count":0,"href":"https:\/\/chayilyam.com\/stories\/wp-json\/wp\/v2\/posts\/417\/revisions"}],"wp:attachment":[{"href":"https:\/\/chayilyam.com\/stories\/wp-json\/wp\/v2\/media?parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chayilyam.com\/stories\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chayilyam.com\/stories\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}