{"id":75,"date":"2008-03-31T07:00:26","date_gmt":"2008-03-31T12:00:26","guid":{"rendered":"http:\/\/cssnewbie.com\/harnessing-positioning-2\/"},"modified":"2008-03-31T07:00:26","modified_gmt":"2008-03-31T12:00:26","slug":"harnessing-positioning-2","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/harnessing-positioning-2\/","title":{"rendered":"Harnessing CSS Positioning: Part 2"},"content":{"rendered":"<p><img src='https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/03\/yoga-400.jpg' alt='Yoga poses' title='\u201cthreesome_trikonasana_close\u201d by myyogaonline. Used under a Creative Commons License.' \/><\/p>\n<p>We\u2019re continuing our look at CSS positioning in this article. If you missed <a href=\"\/harnessing-positioning-1\/\" title=\"Harnessing CSS Positioning: Part 1\">last week\u2019s article on the subject,<\/a> you might want to start there. Today\u2019s article will take a look at two more ways in which absolute and relative positioning can really come in handy: absolute positioning ads and messages to ensure they\u2019re not missed, and creating attractive fractions in your text with CSS and some semantically meaningful XHTML.<\/p>\n<h3>Absolute Positioning Ads and Messages<\/h3>\n<p>There may be times when you really, <em>really<\/em> need your website patrons to notice a certain bit of information. Perhaps you\u2019ve added a new section to the site and want to draw attention to it. Maybe you\u2019re moving your entire site to a new domain and want to give your readers a heads-up. Or maybe you\u2019ve just released a new product and want to ensure it gets the attention it deserves. In these cases, absolutely positioning the information can really make it stand out.<\/p>\n<p>The first thing you need to do is decide what you want your container to be: if you want to position your element according to the edges of the body tag, then you don\u2019t need to do anything. If you want to position your message according to some other element (like a containing div), be sure to <a href=\"\/harnessing-positioning-1\/\" title=\"Harnessing CSS Positioning: Part 1\">refer to last week\u2019s tip on positioning inside of other elements.<\/a><\/p>\n<p>Then, all you need to do is style your element and absolutely position it on the page. Something like this would do the trick:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">.message {\n\t\/* Positioning the message box. *\/\n\tposition: absolute;\n\ttop: 0;\n\tleft: 0;\n\t\/* Styling the message. *\/\n\ttext-align: center;\n\twidth: 100%;\n\tborder-bottom: 1px solid #c00;\n\tbackground-color: #faa;\n\tfont-weight: bold; \n\tz-index: 10; }<\/pre>\n<p>And these styles would result in a <a href=\"\/example\/harnessing-positioning\/absolute-message.html\">difficult-to-miss message box that looks a little something like this.<\/a> Or, for a real-world example, check out this absolutely-positioned advertisement on <a href=\"http:\/\/www.woodsmith.com\/\"><em>Woodsmith<\/em> magazine\u2019s homepage:<\/a><\/p>\n<p><a href=\"http:\/\/www.woodsmith.com\/\"><img src='https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/03\/woodsmith-400.jpg' alt='Woodsmith magazine\u2019s website.' \/><\/a><\/p>\n<h3>Classier Fractions with Relative Positioning<\/h3>\n<p>Every once in a while, you&#8217;ll come across a situation in which fractions could really come in handy (such as styling a recipe). If you already create a lot of documents with fractions, you\u2019ve probably noticed by now that while it\u2019s very easy to create attractive fractions in XHTML using the &amp;frac14;, &amp;frac12;, and &amp;frac34; character entities (creating &frac14;, &frac12;, and &frac34;, respectively), anything else comes out looking sub-par. There just isn\u2019t a special character code to properly style nine-sixteenths. However, there is an easy way to do it with XHTML and CSS.<\/p>\n<p>We\u2019ll start with some XHTML that looks like this:<\/p>\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">&lt;p&gt;Once planed, the board should be \n&lt;span class=&quot;frac&quot;&gt;&lt;sup&gt;9&lt;\/sup&gt;\/&lt;sub&gt;16&lt;\/sub&gt;&lt;\/span&gt;&quot; \nthick.&lt;\/p&gt;<\/pre>\n<p>Already, just by using the &lt;sub&gt; (superscript) and &lt;sub&gt; (subscript) tags, we\u2019re getting a lot closer to a realistic fraction. But let\u2019s use CSS to get us a whole lot closer. With the markup given above, we have three selector \u201chooks\u201d: the class on the span, the sup tag, and the sub tag. So let\u2019s style all three:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">.frac {\n\tfont-style: italic; }\n.frac sup, .frac sub {\n\tfont-style: normal;\n\tfont-size: 65%;\n\tposition: relative; }\n.frac sup {\n\ttop: 0.1em;\n\tleft: 0.05em;\n\tvertical-align: text-top; }\n.frac sub {\n\ttop: 0.1em;\n\tleft: -.1em;\n\tvertical-align: text-bottom; }<\/pre>\n<p>We start out by italicizing our fraction: this makes the forward slash between our numerator and denominator even more pronounced. Then, we reset the font-style on our sub and sub tags (because they\u2019re not italicized), make them a little smaller than normal, and give them a relative position. Finally, we position our superscript and subscript tags \u2013 bringing each slightly closer to the forward slash. We also ensure we\u2019re measuring from the top of the superscript, and the bottom of the subscript, just to regularize any positioning problems.<\/p>\n<p><a href=\"\/example\/harnessing-positioning\/fractions.html\">The result, as you can see here, are fractions that are virtually indistinguishable<\/a> from those created using XHTML entities. This means you could easily use both types of fractions in a single document and your readers would likely be none the wiser.<\/p>\n<p>Thus concludes our look at the power and versatility of CSS positioning. Of course, these are only a few examples \u2013 there are countless other things you could accomplish with positioning and a bit of ingenuity. And if you have a favorite \u201cposition\u201d or two that I haven\u2019t mentioned, be sure to share them in the comments!<\/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>We\u2019re continuing our look at CSS positioning in this article. If you missed last week\u2019s article on the subject, you might want to start there. Today\u2019s article will take a look at two more ways in which absolute and relative [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/harnessing-positioning-2\/\" title=\"Click to read 'Harnessing CSS Positioning: Part 2'\">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":[178],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/75"}],"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=75"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}