{"id":66,"date":"2008-03-24T07:00:22","date_gmt":"2008-03-24T12:00:22","guid":{"rendered":"http:\/\/cssnewbie.com\/all-about-css-positioning\/"},"modified":"2008-03-24T07:00:22","modified_gmt":"2008-03-24T12:00:22","slug":"all-about-css-positioning","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/all-about-css-positioning\/","title":{"rendered":"All About: CSS Positioning"},"content":{"rendered":"<p><img src='https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/03\/swissarmy2-400.jpg' alt='swiss army knife' title=\"'Swiss Army Knife' by DOS82. Used under a Creative Commons license.\" \/><\/p>\n<p>At some point or another, if you want to lay out a complex CSS design, you\u2019re probably going to have to turn to the veritable Swiss Army knife of advanced CSS layout: the position property. There are four possible values for this property: static, relative, absolute, and fixed. Let\u2019s briefly go through each.<\/p>\n<p><strong>The static position value<\/strong> is sort of like the giant oversized flathead screwdriver in the Swiss Army knife: initially, it really doesn\u2019t look like it does much at all. That\u2019s because all objects are \u201cstatic\u201d positioned by default. Therefore, if you whip out a \u201cstatic\u201d position on an object that doesn\u2019t have anything else applied, you can expect it to do\u2026 absolutely nothing. However, it can be useful if, for example, you\u2019ve set a \u201crelative\u201d position on every image in your website, and then you need on specific image to behave normally. That\u2019s when you suddenly realize that the giant flathead screwdriver is <em>exactly<\/em> the right size for replacing the screws on the bathroom vent cover. It has finally earned its keep.<\/p>\n<p><strong>Relative positioning<\/strong> in CSS is a bit like the pair of scissors in a Swiss Army knife: it has a pretty obvious function, but it doesn\u2019t always work as well as you\u2019d expect (or at least, it doesn\u2019t <em>the same way<\/em> as you\u2019d expect). Using the relative position value, in conjunction with the top, bottom, left, or right properties to move the object, moves an object from its initial location in the document flow to a new one. However, the old space where the object used to live is left open. Therefore, if you have three paragraphs, and position the third paragraph like so:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">p#two {\n\tposition: relative;\n\ttop: 2em;\n\tright: 3em; }<\/pre>\n<p>Then you\u2019d end up with <a href=\"\/example\/css-positioning\/relative.html\">a document that looked something like this<\/a> (I\u2019ve added some styling to the paragraphs to make the positioning easier to see). As you can see, the paragraph as moved down and left (or more accurately, away from the top and right) from where it was originally, but the space reserved for the paragraph has remained intact. This can be useful, as long as you understand what you\u2019re getting when you relatively position. It\u2019s like understanding that the scissors in your knife are okay at cutting fabric in an emergency, but they\u2019re never going to replace your kitchen shears.<\/p>\n<p><strong>Absolute positioning<\/strong> is the knife of the Swiss Army knife: it\u2019s what everyone generally thinks of (and expects) when they think of positioning elements. Absolute positioning an element pulls it completely out of the document\u2019s flow \u2013 the space it would have otherwise occupied is closed up, and the element is placed wherever you\u2019d like it to be placed (according to the top\/right\/bottom\/left properties). So this little bit of code:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">p#two {\n\tposition: absolute;\n\tbottom: 2px; }<\/pre>\n<p>Will result in <a href=\"\/example\/css-positioning\/absolute.html\">something like this.<\/a> Note a few things about this example: first, since we set the \u201cbottom\u201d property the element is positioned according to the bottom of the browser window (and <stronng>not<\/strong> according to its initial position), but since we didn\u2019t set a left or right property, it isn\u2019t moved from its original position along the x-axis. Second, because the element is no longer contained within the original block that defined its width, it has expanded to fill the size of the browser window. These aren\u2019t really bad things, but they\u2019re important to recognize.<\/p>\n<p>And last but certainly not least is the <strong>fixed position property<\/strong>. It\u2019s a little like that slightly sharp, oddly curved piece of metal in your Swiss Army knife \u2013 you\u2019re not quite sure what to do with it at first, but once you realize it\u2019s perfect for popping the top off a beer bottle, it quickly becomes  your favorite tool in the arsenal. Fixed positioning behaves a little like absolute: it pulls the element out of the document flow and closes the original space behind it. However, unlike absolute positioning, once you\u2019ve specified a spot for the element, it stays there&#8230; even if the page behind it moves. So take this code:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">p#two {\n\tposition: fixed;\n\ttop: 10em;\n\twidth: 35em; \n\tbackground-color: #fcc; }<\/pre>\n<p><a href=\"\/example\/css-positioning\/fixed.html\">That will produce a document that behaves like this.<\/a> No matter how much you scroll, that paragraph will stay in the exact same spot. This can be really useful if you need something to always stay in one spot, like an important navigation bar at the top of the page, or footer information that you always want to be visible at the bottom of the window.<\/p>\n<p>Once you learn to master these properties, advanced CSS layouts start to become a whole heck of a lot less mysterious. If you\u2019d like to learn more, be sure to <a href=\"http:\/\/feeds.feedburner.com\/cssnewbie\">subscribe to the CSSnewbie RSS feed<\/a>, because later this week I\u2019ll be talking more about how to use these properties in useful and interesting ways.<\/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>At some point or another, if you want to lay out a complex CSS design, you\u2019re probably going to have to turn to the veritable Swiss Army knife of advanced CSS layout: the position property. There are four possible values [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/all-about-css-positioning\/\" title=\"Click to read 'All About: CSS Positioning'\">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":[168,178],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/66"}],"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=66"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/66\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}