{"id":72,"date":"2008-03-26T07:00:50","date_gmt":"2008-03-26T12:00:50","guid":{"rendered":"http:\/\/cssnewbie.com\/harnessing-positioning-1\/"},"modified":"2008-03-26T07:00:50","modified_gmt":"2008-03-26T12:00:50","slug":"harnessing-positioning-1","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/harnessing-positioning-1\/","title":{"rendered":"Harnessing CSS Positioning: Part 1"},"content":{"rendered":"<p><img src='https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/03\/racehorse-400.jpg' alt='racehorse in mid-run' title=\"'Churchill Downs' by Jeff Kubina. Used under a Creative Commons license.\" \/><\/p>\n<p>Earlier this week, I explained <a href=\"\/all-about-css-positioning\/\">the basics of CSS positioning<\/a>: what it is and what your options are. Now I\u2019d like to take some time and explain a couple of tricks for using positioning in the real world\u2026 stuff that really harnesses the true power of CSS positioning.<\/p>\n<h3>Absolute Positioning Inside Relative Positioning<\/h3>\n<p>One of the coolest and simplest ways to harness the power of positioning is to use a combination of relative and absolute positioning. Specifically, to absolute position an object <em>inside<\/em> of a relatively positioned object. Why is this so cool? Because of one of the under-understood rules of positioning.<\/p>\n<p>Any time an element is absolutely positioned, it\u2019s actually being positioned according to the boundaries of its containing block. By default, with no other objects positioned on the page, the containing block is the &lt;body&gt; tag. However, if you position any element further down in the hierarchy (like a containing div, for example), that positioned element becomes your <strong>new<\/strong> containing block. What\u2019s that mean in practice? It means you can have some XTHML that looks like this:<\/p>\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">&lt;div id=\"wrap\"&gt;\n\t&lt;p&gt;This stuff is in the wrapper&lt;\/p&gt;\n\t&lt;div id=\"sidebar\"&gt;\n\t\t&lt;p&gt;Sidebar information is cool&lt;\/p&gt;\n\t&lt;\/div&gt;\n&lt;\/div&gt;<\/pre>\n<p>Then, you can size and relatively position your wrapper div, like so:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">#wrap {\n\twidth: 500px;\n\tmargin: 0 auto;\n\tposition: relative;\n}<\/pre>\n<p>Here, all we\u2019re doing is giving it a width, centering it with an auto margin trick, and then setting a relative position. But we\u2019re not actually moving the div anywhere! Setting a relative position, but then not setting a top, bottom, left or right property means the element doesn\u2019t move anywhere. But, because it <em>has<\/em> a \u201cposition\u201d set, it becomes our new containing block.  Now, we can specify styles on our sidebar that look something like this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">#sidebar {\n\tposition: absolute;\n\ttop: 0;\n\tright: 0;\n}<\/pre>\n<p>As we learned last week, setting our top and right properties to zero, with an absolute position, should move our sidebar to the top-right of the page. However, because our wrapper div became our new containing block through use of the relative position, the sidebar is actually just moved to the top-right corner of the <em>wrapper<\/em> div. This can be extremely useful when you have a fixed-width and centered web page: you may want to absolutely position elements according to your \u201cpage,\u201d but <strong>not<\/strong> according to the browser window. This is how that\u2019s done. <a href=\"\/example\/harnessing-positioning\/rel-abs.html\">You can see a slightly more involved example here.<\/a><\/p>\n<h3>Fixed-Position Navigation<\/h3>\n<p>If you have a website that tends to be a bit on the wordy\/lengthy side (like, erm\u2026 this one?), it could be useful to ensure that the site&#8217;s navigation is always at the top of the page, no matter how far down your users scroll. For that, fixed positioning can really come in handy.<\/p>\n<p>For this trick, all you need to do is develop a navigation bar of some sort (<a href=\"\/tab-based-navigation\/\">covered in more detail here<\/a>), and then \u201cfix\u201d it to the top of your website. You\u2019d start out with some XHTML like this:<\/p>\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">&lt;div id=\"wrap\"&gt;\n\t&lt;ul id=\"nav\"&gt;\n\t\t&lt;li&gt;Home&lt;\/li&gt;\n\t\t&lt;li&gt;Contact&lt;\/&gt;\n\t\t&lt;li&gt;Etc&lt;\/li&gt;\n\t&lt;\/ul&gt;\n\t&lt;h1&gt;Page Title&lt;\/h1&gt;\n\t&lt;p&gt;This page is cooler than it looks.&lt;\/p&gt;\n&lt;\/div&gt;<\/pre>\n<p>Next, you\u2019d want to pull your \u201cfix\u201d your navbar to the top of the page:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">#nav {\n\tposition: fixed;\n\ttop: 0;\n}<\/pre>\n<p>And really, that\u2019s the meat of your code. The fixed position tells the browser to pull the navbar out of the normal document flow, and then affix it to the top of the page. Of course, because it has been taken out of your document flow, the navbar might be overlapping your text, which will move up the page to fill the space that the navbar has vacated. To ensure that doesn\u2019t happen, you\u2019d want to add some space to the top of your website. Something like this would do the trick:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">#wrap {\n\t\/* Your padding should be the height\n\tof your navigation bar, plus the \n\tspace you want between the navbar\n\tand the top of your page. *\/\n\tpadding-top: 50px;\n}<\/pre>\n<p>As the comment above states, you\u2019ll want to add enough padding to compensate for the height of your navbar, as well as any extra spacing you want between your navbar and the content below. So if your navbar were 30 pixels tall, and you wanted 20 pixels of space before you got to your header, you\u2019d want 30 + 20 = 50 pixels of space at the top.<\/p>\n<p>And voila! You now have a navigation bar perma-fixed to the top of your browser window. No matter how far down your page your users scroll, your navigation will never be more than a few pixels away. <a href=\"\/example\/harnessing-positioning\/fixed-nav.html\">You can see it in action here.<\/a><\/p>\n<p>These are just two ways in which the position property can really make your life easier, your websites cooler, and your wife more satisfied. <a href=\"http:\/\/feeds.feedburner.com\/cssnewbie\">Stay tuned<\/a>, because next week I\u2019ll show you a couple of other neat positioning tricks: one for creating big visual impact with absolute positioning, and another for making subtle (but useful!) text changes using relative positioning.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<\/p>\n<p>Earlier this week, I explained the basics of CSS positioning: what it is and what your options are. Now I\u2019d like to take some time and explain a couple of tricks for using positioning in the real world\u2026 stuff that [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/harnessing-positioning-1\/\" title=\"Click to read 'Harnessing CSS Positioning: Part 1'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[165,178],"tags":[109,62,323,108],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/72"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}