<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>CSS Newbie</title> <link>http://www.cssnewbie.com</link> <description>Web Development Tutorials, Tips &amp; Techniques</description> <lastBuildDate>Sat, 13 Mar 2010 13:33:21 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.2</generator> <language /> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/cssnewbie" /><feedburner:info uri="cssnewbie" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>cssnewbie</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item><title>Button Height and a Broken Box Model</title><link>http://feedproxy.google.com/~r/cssnewbie/~3/-n5oDprcSnc/</link> <comments>http://www.cssnewbie.com/button-height-and-a-broken-box-model/#comments</comments> <pubDate>Tue, 02 Mar 2010 21:00:28 +0000</pubDate> <dc:creator>Rob Glazebrook</dc:creator> <category><![CDATA[Box Model]]></category> <category><![CDATA[Bugs and Fixes]]></category> <category><![CDATA[Layout]]></category> <category><![CDATA[bug]]></category> <category><![CDATA[bug fix]]></category> <category><![CDATA[css]]></category> <category><![CDATA[firefox]]></category> <category><![CDATA[padding]]></category><guid isPermaLink="false">http://www.cssnewbie.com/?p=536</guid> <description><![CDATA[Buttons don't behave like normal elements when it comes to the standard CSS Box Model. In fact, they seem to ignore the box model altogether in favor of an IE6-style model. Strangely, this seems to be true across all modern browsers!]]></description> <content:encoded><![CDATA[<p><img
src="http://www.cssnewbie.com/wp-content/uploads/2010/03/broken-button-box-model.png" alt="" title="A Broken Button Box Model" width="500" height="250" class="alignnone size-full wp-image-538" /></p><p>When I figured out the <a
href="http://www.cssnewbie.com/input-button-line-height-bug/">workaround for line-heights in form buttons</a> last week, I also discovered an interesting discrepancy (feature?) across all major browsers in terms of buttons and the box model. <a
href="http://www.cssnewbie.com/understanding-the-css-box-model/">Check out this article for a more in-depth refresher on the CSS Box Model,</a> but here&#8217;s a brief summary:</p><p>Let&#8217;s say I have a div styled like so:</p><pre class="css">div {
	height: 30px;
	padding: 10px 0;
}</pre><p>The box model states that the resulting box will take up 30px of vertical space for the div&#8217;s contents, <em>plus</em> an additional 10px on the top and bottom for the padding, for a total of 50px vertical space taken up by our div. <span
id="more-536"></span></p><p>Back in the day, the problem was that while most browsers accepted this &#8220;standard&#8221; box model, Internet Explorer decided to be all rouge-ish and maverick-y and come up with a box model of their own. In their model, the entire div should take up 30px of vertical space, which would mean in our example above that 10px was subtracted from the top and bottom of the div&#8217;s interior to make room for padding&#8230; meaning there was only 10px left over for the div&#8217;s contents.</p><p>As you can imagine, this caused all sorts of problems for web designers trying to design for both box models. There were workarounds of course, but they bloated our code and made our lives just that much less glamorous. I mean, who has time for champagne pool parties when you&#8217;ve got to write browser-specific stylesheets?</p><p>To be fair, Internet Explorer has since begun to adhere to the standard box model. Old pages that used the old box model still work as such in &#8220;Quirks&#8221; mode, while new standards-compliant pages are rendered appropriately in &#8220;Standards&#8221; mode (<a
href="http://www.cssnewbie.com/standards-vs-quirks-mode/">there&#8217;s an article on Quirks vs. Standards modes here</a>).</p><h3>Back to the Future</h3><p>So what the heck does this have to do with us? After all, we live in The Future. Well, as far as I can tell, <strong>buttons still use the non-standard box model.</strong> And the strangest thing to me is, this holds true in every single browser I tested: Chrome, Safari, Firefox, Opera, and Internet Explorer.<br
/> Let&#8217;s look at an example. Here&#8217;s the (essentially identical) CSS for a nice input-style submit button:</p><pre class="css">button {
	height: 30px;
	padding: 10px 0;
}</pre><p>I&#8217;ve specified a height of 30px and top and bottom padding of 10px, which according to the box model should result in a total height of 50px overall, right? Except as you can see in the image up top, Firebug (running in the most recent edition of Firefox) reports a total height of just 30px: it&#8217;s <em>subtracting the padding I&#8217;ve applied,</em> in addition to the height of the default button borders.</p><p>Which means that, in addition to <a
href="http://www.cssnewbie.com/input-button-line-height-bug/">Firefox ignoring line-height on buttons</a>, it also ignores the modern box model altogether!</p><p>So what gives? What makes a button any different than any other element on the page? I wish I knew. In fact, if you do happen to know the answer to this riddle, please share with the class in the comments below. Until then, I don&#8217;t have a solution to this particular peccadillo. But knowledge is half the battle!</p><div
id="crp_related"><h3>Related Reading:</h3><ul><li><a
href="http://www.cssnewbie.com/input-button-line-height-bug/" rel="bookmark" class="crp_title">The Firefox Input Button Line-Height Bug</a></li><li><a
href="http://www.cssnewbie.com/understanding-the-css-box-model/" rel="bookmark" class="crp_title">Understanding the CSS Box Model</a></li><li><a
href="http://www.cssnewbie.com/easy-rounded-corners-with-border-radius/" rel="bookmark" class="crp_title">Easy Rounded Corners with Border-Radius</a></li><li><a
href="http://www.cssnewbie.com/firefox-error-console-review/" rel="bookmark" class="crp_title">Tool Review: Firefox Error Console</a></li><li><a
href="http://www.cssnewbie.com/standards-vs-quirks-mode/" rel="bookmark" class="crp_title">Standards vs Quirks Mode</a></li></ul></div>
<p><a href="http://feedads.g.doubleclick.net/~a/POZHHVqySg0cwIGb8whsJ8gsLxg/0/da"><img src="http://feedads.g.doubleclick.net/~a/POZHHVqySg0cwIGb8whsJ8gsLxg/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/POZHHVqySg0cwIGb8whsJ8gsLxg/1/da"><img src="http://feedads.g.doubleclick.net/~a/POZHHVqySg0cwIGb8whsJ8gsLxg/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cssnewbie?a=-n5oDprcSnc:101J_WvPFVU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=-n5oDprcSnc:101J_WvPFVU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=-n5oDprcSnc:101J_WvPFVU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=-n5oDprcSnc:101J_WvPFVU:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=-n5oDprcSnc:101J_WvPFVU:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=-n5oDprcSnc:101J_WvPFVU:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=-n5oDprcSnc:101J_WvPFVU:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=-n5oDprcSnc:101J_WvPFVU:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=-n5oDprcSnc:101J_WvPFVU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=-n5oDprcSnc:101J_WvPFVU:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cssnewbie/~4/-n5oDprcSnc" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.cssnewbie.com/button-height-and-a-broken-box-model/feed/</wfw:commentRss> <slash:comments>12</slash:comments> <feedburner:origLink>http://www.cssnewbie.com/button-height-and-a-broken-box-model/</feedburner:origLink></item> <item><title>The Firefox Input Button Line-Height Bug</title><link>http://feedproxy.google.com/~r/cssnewbie/~3/7kFuQuJ2eKU/</link> <comments>http://www.cssnewbie.com/input-button-line-height-bug/#comments</comments> <pubDate>Thu, 25 Feb 2010 22:21:00 +0000</pubDate> <dc:creator>Rob Glazebrook</dc:creator> <category><![CDATA[Box Model]]></category> <category><![CDATA[Bugs and Fixes]]></category> <category><![CDATA[bug]]></category> <category><![CDATA[bug fix]]></category> <category><![CDATA[button]]></category> <category><![CDATA[firefox]]></category> <category><![CDATA[input]]></category> <category><![CDATA[opera]]></category><guid isPermaLink="false">http://www.cssnewbie.com/?p=528</guid> <description><![CDATA[I recently ran into a bug in Firefox and Opera when I tried to set the line height of text inside a button (which affects input "submit" buttons as well as the HTML button tag). The bug? The line height can't be changed!]]></description> <content:encoded><![CDATA[<p><img
src="http://www.cssnewbie.com/wp-content/uploads/2010/02/button-sizes-by-browser.png" alt="" title="Button Sizes Using Line-Height by Browser" width="500" height="101" class="alignnone size-full wp-image-529" /></p><p>I recently ran into a bug in Firefox and Opera when I tried to set the line height of text inside a button (which affects input &#8220;submit&#8221; buttons as well as the HTML button tag). The bug? The line height can&#8217;t be changed!</p><p>For example, take the following code:</p><pre class="css">input#button {
	border: 2px solid #06f;
	color: #06f;
	background-color: #6cf;
	font: bold 12px Arial, Helvetica, sans-serif;
	line-height: 50px;
}</pre><p>In a perfect world, this code would be a quick and easy way to vertically center text inside a button and set the button&#8217;s height, since text is always centered inside of the space created by its line-height.</p><p>But the results are inconsistent. Chrome, Safari, and (I can&#8217;t believe this either) Internet Explorer 8 all center the text and resize the button just like I&#8217;d expect. But the results are less than perfect in Firefox and Opera (see the image above). <span
id="more-528"></span></p><h3>The Problem, Defined</h3><p>A quick look at Firebug proves enlightening: even though I&#8217;ve specified a line-height of 50px, Firefox is using a line-height of 15px instead.</p><p><img
src="http://www.cssnewbie.com/wp-content/uploads/2010/02/firebug-firefox-line-height.png" alt="" title="Firebug showing Firefox&#039;s incorrect line-height" width="500" height="220" class="alignnone size-full wp-image-530" /></p><p>So what gives?</p><p>It seems our bug isn&#8217;t really a bug at all, but a &#8220;feature&#8221;: that is, it&#8217;s a deliberate decision by Firefox to limit line heights on buttons. This is evidenced by this line of CSS in Firefox&#8217;s default CSS:</p><pre class="css">button, input[type="reset"], input[type="button"], input[type="submit"] {
	line-height:normal !important;
}</pre><p>Basically, Firefox is setting the line-height to &#8220;normal&#8221; on buttons and is enforcing this decision with an !important declaration. This is a frustrating decision on their part, particularly considering (as Eric Meyer has pointed out at great and detailed length), <a
href="http://meyerweb.com/eric/thoughts/2008/05/06/line-height-abnormal/">line-height: normal is anything but.</a></p><p>And while trying to work around this rule, I discovered something that makes the situation a little more dire: browser-defined !important rules cannot be over-ruled by author-defined !important rules. This rule cannot be overruled by a CSS file, an inline style &mdash; anything.</p><p>So what&#8217;s to be done?</p><p>After a couple of hours of teeth-gnashing, I&#8217;ve settled on the following as an acceptable workaround. <strong>Instead of using line-height, use padding.</strong></p><p>So to take the example from earlier, we&#8217;d convert it to look like this instead:</p><pre class="css">input#button {
	border: 2px solid #06f;
	color: #06f;
	background-color: #6cf;
	font: bold 12px Arial, Helvetica, sans-serif;
	padding: 18px 6px;
}</pre><p>This effectively centers the text inside our buttons, but it isn&#8217;t ideal. It means there&#8217;s no easy way to ensure our buttons are using the same line-height as the rest of our content, and it means the size of the button can&#8217;t be dependent upon the size of the button text. But I&#8217;ll take what I can get on this one.</p><div
id="crp_related"><h3>Related Reading:</h3><ul><li><a
href="http://www.cssnewbie.com/button-height-and-a-broken-box-model/" rel="bookmark" class="crp_title">Button Height and a Broken Box Model</a></li><li><a
href="http://www.cssnewbie.com/firefox-error-console-review/" rel="bookmark" class="crp_title">Tool Review: Firefox Error Console</a></li><li><a
href="http://www.cssnewbie.com/css-shorthand/" rel="bookmark" class="crp_title">Writing CSS Shorthand</a></li><li><a
href="http://www.cssnewbie.com/reset-style-sheets/" rel="bookmark" class="crp_title">Level the Playing Field with Reset Style Sheets</a></li><li><a
href="http://www.cssnewbie.com/great-resources-elsewhere-may-08-to-may-15/" rel="bookmark" class="crp_title">Great Resources Elsewhere: May 08 to May 15</a></li></ul></div>
<p><a href="http://feedads.g.doubleclick.net/~a/jB5TMS5AEOz18xocdcaqFiMtyAc/0/da"><img src="http://feedads.g.doubleclick.net/~a/jB5TMS5AEOz18xocdcaqFiMtyAc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/jB5TMS5AEOz18xocdcaqFiMtyAc/1/da"><img src="http://feedads.g.doubleclick.net/~a/jB5TMS5AEOz18xocdcaqFiMtyAc/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cssnewbie?a=7kFuQuJ2eKU:pjYattvcjGA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=7kFuQuJ2eKU:pjYattvcjGA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=7kFuQuJ2eKU:pjYattvcjGA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=7kFuQuJ2eKU:pjYattvcjGA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=7kFuQuJ2eKU:pjYattvcjGA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=7kFuQuJ2eKU:pjYattvcjGA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=7kFuQuJ2eKU:pjYattvcjGA:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=7kFuQuJ2eKU:pjYattvcjGA:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=7kFuQuJ2eKU:pjYattvcjGA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=7kFuQuJ2eKU:pjYattvcjGA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cssnewbie/~4/7kFuQuJ2eKU" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.cssnewbie.com/input-button-line-height-bug/feed/</wfw:commentRss> <slash:comments>15</slash:comments> <feedburner:origLink>http://www.cssnewbie.com/input-button-line-height-bug/</feedburner:origLink></item> <item><title>New Example Page Formatting</title><link>http://feedproxy.google.com/~r/cssnewbie/~3/8M-jTkHFN-k/</link> <comments>http://www.cssnewbie.com/new-example-page-formatting/#comments</comments> <pubDate>Tue, 16 Feb 2010 21:21:05 +0000</pubDate> <dc:creator>Rob Glazebrook</dc:creator> <category><![CDATA[Site News]]></category><guid isPermaLink="false">http://www.cssnewbie.com/?p=524</guid> <description><![CDATA[I've never liked that my CSS Newbie demo pages have no visual consistency. So, I took some time and added a new wrapper. It makes them more consistent and more useful at the same time.]]></description> <content:encoded><![CDATA[<p><a
href="http://www.cssnewbie.com/example/equal-heights/"><img
src="http://www.cssnewbie.com/wp-content/uploads/2010/02/cssnewbie-example.png" alt="" title="CSS Newbie Example Page" width="500" height="150" class="alignnone size-full wp-image-525" /></a></p><p>I&#8217;ve always liked that most of my articles here at CSS Newbie have live demos &mdash; places where you can go and check out the code working in the real world. It&#8217;s a nice complement to the tutorials, I think.</p><p>But one think I&#8217;ve never liked about this setup is that my demos have no visual consistency. Some have background colors, some don&#8217;t. Some are centered while others are left-justified. Almost none of them show the source in the page itself. And not a single one of them is “branded” CSS Newbie.</p><p>This has always been a background annoyance, until a few weeks ago when I installed Google Analytics on my example pages and realized they were getting a lot more traffic than I expected. And further, many people visited the example pages without ever first seeing the corresponding CSS Newbie article. So people were getting examples with no context, and no way to respond.</p><p>I wasn&#8217;t cool with that. <span
id="more-524"></span></p><p>So, I took some time and added a new wrapper to my CSS Newbie example pages. It solves the branding problem, makes tracking more consistent, and makes the example pages more useful to boot.</p><p>Now when you visit a CSS Newbie demo page, you&#8217;ll see the live demo on the left, and the code generating that demo on the right. It&#8217;s as easy as copy/paste to get the code for your own, without having to view the source. And to make things even simpler, there&#8217;s a link to download the source code to your computer.</p><p>Here&#8217;s a few example pages to try out:</p><ul><li><a
href="http://www.cssnewbie.com/example/clickable-li/">Make an Entire List Item Clickable</a></li><li><a
href="http://www.cssnewbie.com/example/equal-heights/">Equal Height Columns with jQuery</a></li><li><a
href="http://www.cssnewbie.com/example/popout-ad/">Building a jQuery Popout Ad</a></li><li><a
href="http://www.cssnewbie.com/example/styling-blockquotes/">Styling Blockquotes</a></li><li><a
href="http://www.cssnewbie.com/example/zebra-tables/">Easy Zebra Striping Tables</a></li></ul><p>So please, take a look at the new CSS Newbie example pages. Let me know what you think: what you like, what you don&#8217;t, or what you&#8217;d like to see added in the future.</p><div
id="crp_related"><h3>Related Reading:</h3><ul><li><a
href="http://www.cssnewbie.com/the-best-css-newbie-articles-of-2009/" rel="bookmark" class="crp_title">The Best CSS Newbie Articles of 2009</a></li><li><a
href="http://www.cssnewbie.com/styling-zebra-striped-tables-with-css/" rel="bookmark" class="crp_title">Styling Zebra Striped Tables With CSS</a></li><li><a
href="http://www.cssnewbie.com/equal-height-columns-with-jquery/" rel="bookmark" class="crp_title">Equal Height Columns with jQuery</a></li><li><a
href="http://www.cssnewbie.com/make-an-entire-list-item-clickable/" rel="bookmark" class="crp_title">Improve Usability and CTR: Make an Entire List Item Clickable</a></li><li><a
href="http://www.cssnewbie.com/equalheights-jquery-plugin/" rel="bookmark" class="crp_title">The EqualHeights jQuery Plugin</a></li></ul></div>
<p><a href="http://feedads.g.doubleclick.net/~a/84CZVKNdC6LIpK-EwAgGsVqvRSQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/84CZVKNdC6LIpK-EwAgGsVqvRSQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/84CZVKNdC6LIpK-EwAgGsVqvRSQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/84CZVKNdC6LIpK-EwAgGsVqvRSQ/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cssnewbie?a=8M-jTkHFN-k:NfA7ARtbXgQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=8M-jTkHFN-k:NfA7ARtbXgQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=8M-jTkHFN-k:NfA7ARtbXgQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=8M-jTkHFN-k:NfA7ARtbXgQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=8M-jTkHFN-k:NfA7ARtbXgQ:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=8M-jTkHFN-k:NfA7ARtbXgQ:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=8M-jTkHFN-k:NfA7ARtbXgQ:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=8M-jTkHFN-k:NfA7ARtbXgQ:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=8M-jTkHFN-k:NfA7ARtbXgQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=8M-jTkHFN-k:NfA7ARtbXgQ:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cssnewbie/~4/8M-jTkHFN-k" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.cssnewbie.com/new-example-page-formatting/feed/</wfw:commentRss> <slash:comments>1</slash:comments> <feedburner:origLink>http://www.cssnewbie.com/new-example-page-formatting/</feedburner:origLink></item> <item><title>The Simple Secret to Good Dropdown Navigation</title><link>http://feedproxy.google.com/~r/cssnewbie/~3/upTRQU0jlw4/</link> <comments>http://www.cssnewbie.com/the-simple-secret-to-good-dropdown-navigation/#comments</comments> <pubDate>Wed, 03 Feb 2010 21:26:23 +0000</pubDate> <dc:creator>Rob Glazebrook</dc:creator> <category><![CDATA[Basic Techniques]]></category> <category><![CDATA[Lists]]></category> <category><![CDATA[Navigation]]></category> <category><![CDATA[Quick Tip]]></category> <category><![CDATA[tip]]></category><guid isPermaLink="false">http://www.cssnewbie.com/?p=518</guid> <description><![CDATA[Almost every example of good dropdown navigation on the web today relies on a single, simple HTML structure: the nested unordered list. Of course, that means that knowing how to build a good nested unordered list is critical to this entire process.]]></description> <content:encoded><![CDATA[<p><a
href="http://www.cssnewbie.com/example/horizontal-menu/centered.html"><img
src="http://www.cssnewbie.com/wp-content/uploads/2009/12/centered-navbar-beauty.png" alt="Centered Navigation Bar - Click to see it in action!" title="Centered Navigation Bar - Click to see it in action!" width="500" height="100" class="alignnone size-full wp-image-473" /></a></p><p>Almost every example of good dropdown navigation on the web today relies on a single, simple HTML structure: the nested unordered list. Without this structure, dropdown menus would be much more complex to build than they already are.</p><p>Of course, that means that knowing how to build a good nested unordered list is critical to this entire process. I’ve seen many examples of people trying to build a dropdown menu on their own, running into problems they didn’t understand, and throwing their hands up in frustration&#8230; only to discover their problem wasn’t some obscure CSS problem, but an error in their list structure.</p><p>As such, I thought it would be a good idea to go over what a good nested unordered list structure looks like (and what a bad one often looks like too!).<span
id="more-518"></span></p><h3>The Good</h3><p>Here’s a well-structured nested unordered list &#8212; a perfect foundation for a dropdown menu.</p><pre class="html">&lt;ul id=&quot;navbar&quot;&gt;
	&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Nav Item 1&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Nav with Subnav&lt;/a&gt;
		&lt;ul&gt;
			&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Subnav 1&lt;/a&gt;&lt;/li&gt;
			&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Subnav 2&lt;/a&gt;&lt;/li&gt;
		&lt;/ul&gt;
	&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Nav Item 3&lt;/a&gt;
&lt;/ul&gt;</pre><p>The critical detail in the example above is where the nested unordered list appears in relation to the list item it is “under”. The unordered list is <strong>inside</strong> the list item that will act as its parent in the navigation.</p><p>That’s a very critical bit. Without that little detail, it becomes much more difficult to determine which submenus “belong” to which main list items.</p><h3>The Bad</h3><p>By comparison, this is what I see 90% of the time when someone has a misbehaving menu:</p><pre class="html">&lt;ul id=&quot;navbar&quot;&gt;
	&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Nav Item 1&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Nav with Subnav&lt;/a&gt;&lt;/li&gt;
	&lt;ul&gt;
		&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Subnav 1&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Subnav 2&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
	&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Nav Item 3&lt;/a&gt;
&lt;/ul&gt; </pre><p>It’s an extremely minor difference. The only change here is that the nested unordered list appears <strong>immediately after</strong> the list item they expect it to appear under, instead of <strong>directly inside</strong> like it should be.</p><p>And that makes a difference, obviously, in our CSS and JavaScript.  Elements aren’t where the CSS is looking for them, relationships are harder to determine in JavaScript, and so forth.</p><h3>List-Based Navigation Menus</h3><p>And since we are talking about list-based navigation menus, here are a few that have been discussed on CSS Newbie. All rely on unordered lists as their structure:</p><ul><li><a
href="http://www.cssnewbie.com/full-width-centered-navigation-bar/">A Full-Width Centered Navigation Bar</a></li><li><a
href="http://www.cssnewbie.com/super-simple-horizontal-navigation-bar/">A Super Simple Horizontal Navigation Bar</a></li><li><a
href="http://www.cssnewbie.com/horizontal-dropdown-menus/">Horizontal CSS Dropdown Menus</a></li><li><a
href="http://www.cssnewbie.com/easy-css-dropdown-menus/">Easy CSS Dropdown Menus</a></li></ul><p>And with that, I’ll be gone on vacation until next week. Take good care of the place, peeps!</p><div
id="crp_related"><h3>Related Reading:</h3><ul><li><a
href="http://www.cssnewbie.com/super-simple-horizontal-navigation-bar/" rel="bookmark" class="crp_title">A Super Simple Horizontal Navigation Bar</a></li><li><a
href="http://www.cssnewbie.com/horizontal-dropdown-menus/" rel="bookmark" class="crp_title">Horizontal CSS Dropdown Menus</a></li><li><a
href="http://www.cssnewbie.com/easy-css-dropdown-menus/" rel="bookmark" class="crp_title">Easy CSS Dropdown Menus</a></li><li><a
href="http://www.cssnewbie.com/full-width-centered-navigation-bar/" rel="bookmark" class="crp_title">A Full-Width Centered Navigation Bar</a></li><li><a
href="http://www.cssnewbie.com/fixing-ie6-whitespace-bug/" rel="bookmark" class="crp_title">Fixing the IE6 Whitespace Bug</a></li></ul></div>
<p><a href="http://feedads.g.doubleclick.net/~a/C1jr18fZz-Wy2CP5eJjqGgLVxlM/0/da"><img src="http://feedads.g.doubleclick.net/~a/C1jr18fZz-Wy2CP5eJjqGgLVxlM/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/C1jr18fZz-Wy2CP5eJjqGgLVxlM/1/da"><img src="http://feedads.g.doubleclick.net/~a/C1jr18fZz-Wy2CP5eJjqGgLVxlM/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cssnewbie?a=upTRQU0jlw4:HUDn3IdhyJc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=upTRQU0jlw4:HUDn3IdhyJc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=upTRQU0jlw4:HUDn3IdhyJc:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=upTRQU0jlw4:HUDn3IdhyJc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=upTRQU0jlw4:HUDn3IdhyJc:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=upTRQU0jlw4:HUDn3IdhyJc:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=upTRQU0jlw4:HUDn3IdhyJc:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=upTRQU0jlw4:HUDn3IdhyJc:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=upTRQU0jlw4:HUDn3IdhyJc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=upTRQU0jlw4:HUDn3IdhyJc:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cssnewbie/~4/upTRQU0jlw4" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.cssnewbie.com/the-simple-secret-to-good-dropdown-navigation/feed/</wfw:commentRss> <slash:comments>2</slash:comments> <feedburner:origLink>http://www.cssnewbie.com/the-simple-secret-to-good-dropdown-navigation/</feedburner:origLink></item> <item><title>How To Emulate a Foreach Loop in JavaScript</title><link>http://feedproxy.google.com/~r/cssnewbie/~3/PBQz6e1DiOc/</link> <comments>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/#comments</comments> <pubDate>Thu, 21 Jan 2010 19:53:11 +0000</pubDate> <dc:creator>Rob Glazebrook</dc:creator> <category><![CDATA[Basic Techniques]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[jquery]]></category> <category><![CDATA[tip]]></category> <category><![CDATA[tutorial]]></category><guid isPermaLink="false">http://www.cssnewbie.com/?p=511</guid> <description><![CDATA[I have trouble with JavaScript array and array-manipulation. JavaScript lacks a specific foreach contstruct, which trips me up. Here's how to fake it.]]></description> <content:encoded><![CDATA[<p>JavaScript does many things well, but one area I&#8217;ve always had trouble with is the array and array-manipulation department. For example, this weekend I was working on a project that required me to iterate through a JavaScript array and peform a set of functions on the values within. In PHP, that would have looked something like this:</p><pre class="php">$myArray = array(&quot;item 1&quot;, &quot;item 2&quot;, item 3&quot;);
foreach($myArray as $item) {
	$item = &quot;Do Something Here&quot;;
}</pre><p>Nice and simple, right? All we&#8217;re doing is iterating through the array with the foreach() construct and replacing each item with the phrase &#8220;Do Something Here,&#8221; but you can see how this could/would expand. A similar construct exists in Perl, another language I grew up on.</p><p>But JavaScript lacks a specific &#8220;foreach&#8221; statement, which trips me up every time. So how do we fake it? <span
id="more-511"></span></p><h3>Plain-Jane JavaScript Foreach</h3><p>In JavaScript, we make use of the for() loop construct to fake a foreach scenario. It looks something like this:</p><pre class="js">myArray = new Array(&quot;item 1&quot;, &quot;item 2&quot;, &quot;item 3&quot;);
for(i=0; i&lt;myArray.length; i++) {
	myArray[i] = &quot;Do Something Here&quot;.
}</pre><p>This lets us loop over our array and do something with each item in turn. It&#8217;s not pretty, but it technically does the trick.</p><h3>jQuery Foreach Loops</h3><p>As with many things in the JavaScript world, jQuery makes the concept of a foreach loop simpler. To iterate over an array in jQuery, we&#8217;ll use the jQuery.each() (or in shorthand, $.each()) function.</p><p><strong>Note:</strong> This is a different function than the more commonly known $(whatever_element).each(), and has slightly different usage!</p><p>The jQuery $.each() function works like this:</p><pre class="js">myArray = new Array(&quot;item 1&quot;, &quot;item 2&quot;, &quot;item 3&quot;);
$.each(myArray, function() {
	this = &quot;Do Something Here&quot;;
});</pre><p>In short, we pass the $.each() function two parameters: the first is the array we want to iterate over, and the second is the function we want to perform on that array. Within the function, we simply refer to the current item as &#8220;this,&#8221; which is pretty standard fare.</p><p>I prefer the jQuery functionality for two reasons:</p><ol><li>It reads slightly more like a PHP foreach statement, and</li><li>I&#8217;m generally loading jQuery anyway, so there&#8217;s no harm in using it.</li></ol><p>Your needs may vary – I&#8217;m not suggesting loading jQuery on an otherwise framework-free page just to make array loops easier. But if you&#8217;ve already got jQuery loaded, this might save you a bit of time (and in my case, frustration).</p><div
id="crp_related"><h3>Related Reading:</h3><ul><li><a
href="http://www.cssnewbie.com/generating-automatic-website-footnotes-with-jquery/" rel="bookmark" class="crp_title">Generating Automatic Website Footnotes with jQuery</a></li><li><a
href="http://www.cssnewbie.com/the-simple-secret-to-good-dropdown-navigation/" rel="bookmark" class="crp_title">The Simple Secret to Good Dropdown Navigation</a></li><li><a
href="http://www.cssnewbie.com/make-an-entire-list-item-clickable/" rel="bookmark" class="crp_title">Improve Usability and CTR: Make an Entire List Item Clickable</a></li><li><a
href="http://www.cssnewbie.com/equalheights-jquery-plugin/" rel="bookmark" class="crp_title">The EqualHeights jQuery Plugin</a></li><li><a
href="http://www.cssnewbie.com/simple-jquery-stylesheet-switcher/" rel="bookmark" class="crp_title">A Simple jQuery Stylesheet Switcher</a></li></ul></div>
<p><a href="http://feedads.g.doubleclick.net/~a/xsvb-M1bAYkYsGPgfqU-PwgV1dc/0/da"><img src="http://feedads.g.doubleclick.net/~a/xsvb-M1bAYkYsGPgfqU-PwgV1dc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/xsvb-M1bAYkYsGPgfqU-PwgV1dc/1/da"><img src="http://feedads.g.doubleclick.net/~a/xsvb-M1bAYkYsGPgfqU-PwgV1dc/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cssnewbie?a=PBQz6e1DiOc:KH0Ibex9MOA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=PBQz6e1DiOc:KH0Ibex9MOA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=PBQz6e1DiOc:KH0Ibex9MOA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=PBQz6e1DiOc:KH0Ibex9MOA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=PBQz6e1DiOc:KH0Ibex9MOA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=PBQz6e1DiOc:KH0Ibex9MOA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=PBQz6e1DiOc:KH0Ibex9MOA:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=PBQz6e1DiOc:KH0Ibex9MOA:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=PBQz6e1DiOc:KH0Ibex9MOA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=PBQz6e1DiOc:KH0Ibex9MOA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cssnewbie/~4/PBQz6e1DiOc" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/feed/</wfw:commentRss> <slash:comments>14</slash:comments> <feedburner:origLink>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/</feedburner:origLink></item> <item><title>Improve Usability and CTR: Make an Entire List Item Clickable</title><link>http://feedproxy.google.com/~r/cssnewbie/~3/vlWLPXhxVlI/</link> <comments>http://www.cssnewbie.com/make-an-entire-list-item-clickable/#comments</comments> <pubDate>Tue, 12 Jan 2010 22:31:29 +0000</pubDate> <dc:creator>Rob Glazebrook</dc:creator> <category><![CDATA[Basic Techniques]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[Lists]]></category> <category><![CDATA[Navigation]]></category> <category><![CDATA[anchor]]></category> <category><![CDATA[hover]]></category> <category><![CDATA[jquery]]></category><guid isPermaLink="false">http://www.cssnewbie.com/?p=501</guid> <description><![CDATA[Larger clickable areas make for greater usability and a higher click-through rate. Here's how to expand a clickable area to an entire list item using jQuery.]]></description> <content:encoded><![CDATA[<p><a
href="http://www.cssnewbie.com/example/clickable-li/"><img
src="http://www.cssnewbie.com/wp-content/uploads/2010/01/clickable-list-items.png" alt="" title="Clickable List Items." width="500" height="150" class="alignnone size-full wp-image-502" /></a></p><p>I was recently working on the navigation for a new Wordpress site (to be announced later) and I encountered a bit of a conundrum when it came to my category navigation.</p><p>I had developed some snazzy PHP/WordPress scripting to output my category information into a list containing both the category name as well as the category description. It was a nice way to show people what categories the site had, as well as offering some insight into what the category might contain. The code looked something like this: <span
id="more-501"></span></p><pre class="html">&lt;ul id=&quot;categories&quot;&gt;
	&lt;li id=&quot;cat-1&quot;&gt;
		&lt;h2&gt;&lt;a href=&quot;/some/where/&quot;&gt;Category One&lt;/a&gt;&lt;/h2&gt;
		&lt;p&gt;This is a description of category 1.&lt;/p&gt;
	&lt;/li&gt;
	&lt;li id=&quot;cat-2&quot;&gt;
		&lt;h2&gt;&lt;a href=&quot;/some/where/else/&quot;&gt;Category Two&lt;/a&gt;&lt;/h2&gt;
		&lt;p&gt;Category 2 description goes here.&lt;/p&gt;
	&lt;/li&gt;
	(...and so on...)
&lt;/ul&gt;</pre><p> I styled it such that the style changed when the user hovered over the list item (as in the image above). But while the list item contained a lot of information, the link to the category (inside the heading) &mdash; and therefore the actual clickable area &mdash; was rather small. I wanted the entire list item to be clickable.</p><p>And this isn&#8217;t a purely aesthetic choice, either (although that had something to do with it). It&#8217;s a usability concern. If I wanted my entire list item changed on hover, people would expect that clicking the list item would elicit some sort of response – this is the way navigation works in most corners of the web. But in this case, only those fortunate enough to click the anchor inside the heading tag would get anywhere. Not good.</p><p>Further, it stands to reason that the larger your links are, the more likely someone will click on them. <a
href="http://en.wikipedia.org/wiki/Fitts's_law">Fitts&#8217; law</a> suggests that the time it takes to successfully click on an object is a function of the distance the mouse is from the object and the size of the object. Thus, the larger the clickable area, the higher the click-through ratio (CTR).</p><p>This line of reasoning left me with three options:</p><ol><li>Wrap the contents of the entire list item in an anchor tag. This isn&#8217;t ideal, because an anchor tag is an inline element, and inline elements technically shouldn&#8217;t contain block-level elements (like heading or paragraph tags).</li><li>Wrap the contents of each block-level element in an anchor tag. This also isn&#8217;t a great solution, because it grows my code and requires me to style several different links.</li><li>Use jQuery to do what I needed.</li></ol><p>Can you guess which one I chose? If you selected option #3&#8230; well, you&#8217;ve visited this site before.</p><h3>Our jQuery Script</h3><p>This tiny snippet of code makes it all happen:</p><pre class="js">$(document).ready(function() {
	$("#categories li:has(a)").click(function() {
		window.location = $("a:first",this).attr("href");
	});
});</pre><p>What this does is assigns a new click function to every list item in our categories list that contains (&#8220;has&#8221;) at least one anchor tag. When someone clicks on the list item, it redirects the browser using window.location to the location indicated in the first anchor&#8217;s href.</p><p>I&#8217;m using an neat jQuery trick here I haven&#8217;t talked about before:</p><pre class="js">$("a:first",this)</pre><p>The &#8220;this&#8221; in the code above is a context selector, which is limiting the area in which we&#8217;re looking for the first anchor tag to the element that was just clicked. There is always a context applied to every jQuery expression you write: if you don&#8217;t specify a context, it uses the current HTML document. Here we&#8217;re limiting the context to the just-clicked list item, which reduces the complexity of our search (and presumably, calculation time) considerably.</p><p>That&#8217;s it? Yep! Five lines of code and you&#8217;re done! <a
href="http://www.cssnewbie.com/example/clickable-li/">You can see a demo of this technique here.</a></p><h3>A jQuery Plugin Alternative</h3><p>Once I started working on this solution, I discovered that there&#8217;s already <a
href="http://newism.com.au/blog/post/58/bigtarget-js-increasing-the-size-of-clickable-targets/">a jQuery plugin called BigTarget</a> that does something rather similar to what I&#8217;m doing. If you&#8217;re in to jQuery plugins, definitely give it a look-see. If you&#8217;re planning to apply this in a lot of places, it&#8217;d probably speed up your development time. For my one-off use, however, I think five lines of code are preferable to a large plugin.</p><div
id="crp_related"><h3>Related Reading:</h3><ul><li><a
href="http://www.cssnewbie.com/fixing-ie6-whitespace-bug/" rel="bookmark" class="crp_title">Fixing the IE6 Whitespace Bug</a></li><li><a
href="http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/" rel="bookmark" class="crp_title">How To Emulate a Foreach Loop in JavaScript</a></li><li><a
href="http://www.cssnewbie.com/the-simple-secret-to-good-dropdown-navigation/" rel="bookmark" class="crp_title">The Simple Secret to Good Dropdown Navigation</a></li><li><a
href="http://www.cssnewbie.com/super-simple-horizontal-navigation-bar/" rel="bookmark" class="crp_title">A Super Simple Horizontal Navigation Bar</a></li><li><a
href="http://www.cssnewbie.com/generating-automatic-website-footnotes-with-jquery/" rel="bookmark" class="crp_title">Generating Automatic Website Footnotes with jQuery</a></li></ul></div>
<p><a href="http://feedads.g.doubleclick.net/~a/oUcwfS15yt-pc6ZMlyJRXxpDiTE/0/da"><img src="http://feedads.g.doubleclick.net/~a/oUcwfS15yt-pc6ZMlyJRXxpDiTE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/oUcwfS15yt-pc6ZMlyJRXxpDiTE/1/da"><img src="http://feedads.g.doubleclick.net/~a/oUcwfS15yt-pc6ZMlyJRXxpDiTE/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cssnewbie?a=vlWLPXhxVlI:VeTFQvrKXWA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=vlWLPXhxVlI:VeTFQvrKXWA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=vlWLPXhxVlI:VeTFQvrKXWA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=vlWLPXhxVlI:VeTFQvrKXWA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=vlWLPXhxVlI:VeTFQvrKXWA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=vlWLPXhxVlI:VeTFQvrKXWA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=vlWLPXhxVlI:VeTFQvrKXWA:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=vlWLPXhxVlI:VeTFQvrKXWA:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=vlWLPXhxVlI:VeTFQvrKXWA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=vlWLPXhxVlI:VeTFQvrKXWA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cssnewbie/~4/vlWLPXhxVlI" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.cssnewbie.com/make-an-entire-list-item-clickable/feed/</wfw:commentRss> <slash:comments>9</slash:comments> <feedburner:origLink>http://www.cssnewbie.com/make-an-entire-list-item-clickable/</feedburner:origLink></item> <item><title>The Best CSS Newbie Articles of 2009</title><link>http://feedproxy.google.com/~r/cssnewbie/~3/mtyyYq41oQI/</link> <comments>http://www.cssnewbie.com/the-best-css-newbie-articles-of-2009/#comments</comments> <pubDate>Tue, 05 Jan 2010 18:55:15 +0000</pubDate> <dc:creator>Rob Glazebrook</dc:creator> <category><![CDATA[Polls]]></category> <category><![CDATA[Review]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[jquery]]></category> <category><![CDATA[Navigation]]></category> <category><![CDATA[poll]]></category><guid isPermaLink="false">http://www.cssnewbie.com/?p=488</guid> <description><![CDATA[Happy 2010, readers of CSS Newbie! This year, more than a quarter million people stopped by, visiting more than half a million pages. Here are the Top 10 CSS Newbie Articles of 2009.]]></description> <content:encoded><![CDATA[<p><a
href="http://www.cssnewbie.com/horizontal-dropdown-menus/"><img
src="http://www.cssnewbie.com/wp-content/uploads/2008/05/horizontalmenu-400.gif" alt=""  width="400" height="62" class="alignnone size-full wp-image-129" /></a></p><p>Happy 2010, readers of CSS Newbie! It&#8217;s a bitterly cold day here in Des Moines, Iowa &mdash; the sort that causes me to question the wisdom of throwing off the blankets in the morning. However, today I get to reflect on the last year of CSS Newbie, and that&#8217;s enough to warm my heart, if not my toes.</p><p>I&#8217;ve said it before and I&#8217;ll say it again: CSS Newbie is nothing without you awesome readers out there. This year, <strong>more than a quarter million people</strong> stopped by, visiting <strong>more than half a million pages.</strong> For that, I am very, very grateful. It also makes me excited to see what we can do for those numbers in 2010!</p><p>To celebrate the year gone by, I&#8217;d like to recap the Top 10 CSS Newbie Articles of 2009. The first five I&#8217;ve left up to you &mdash; they&#8217;re the five most visited articles in the last year. The second five were my choice. They include articles I enjoyed writing or include content that I thought particularly useful or interesting. <span
id="more-488"></span></p><h3>Readers&#8217; Choice</h3><ol><li><a
href="http://www.cssnewbie.com/horizontal-dropdown-menus/">Horizontal CSS Dropdown Menus.</a> This was the clear winner of the popularity contest. Last year, more than 7% of the site&#8217;s pageviews went to this one article! Horizontal dropdowns must be a popular design tool these days.</li><li><a
href="http://www.cssnewbie.com/easy-css-dropdown-menus/">Easy CSS Dropdown Menus.</a> Another navigation article in the top five. These articles commanding the two top spots have inspired me to write more navigation-related articles recently and in the future.</li><li><a
href="http://www.cssnewbie.com/css-only-accordion/">The CSS-Only Accordion Effect.</a> This article (and its successor, the <a
href="http://www.cssnewbie.com/advanced-css-accordion-effect/">Advanced CSS Accordion Effect</a>) have received a lot of feedback, both positive and negative. The negative comments generally revolve around my ineptitude in the graphic design department. I&#8217;m comfortable with that, I think.</li><li><a
href="http://www.cssnewbie.com/showhide-content-css-javascript/">Show/Hide Content with CSS and JavaScript.</a> This is one of those basic technique articles that people seem to have found really useful. Perhaps this indicates I should write more basics articles?</li><li><a
href="http://www.cssnewbie.com/equal-height-columns-with-jquery/">Equal Height Columns with jQuery.</a> I&#8217;m happy to see this article make it into the top five &mdash; it&#8217;s a useful solution to a common problem!</li></ol><h3>Author&#8217;s Choice</h3><ol><li><a
href="http://www.cssnewbie.com/15-surefire-ways-to-break-your-css/">15 Surefire Ways to Break Your CSS.</a> This article was a ton of fun to write. Essentially, it&#8217;s a big list of what not to do &mdash; the opposite approach I normally take here.</li><li><a
href="http://www.cssnewbie.com/build-custom-frameworks/">Build Custom Frameworks Easily with CSS Classes.</a> This article never got as popular as I expected &mdash; I blame an overly-verbose title. The article shows you how to build your own grid-based website without having to rely on the overly-complicated frameworks out there. Check it out!</li><li><a
href="http://www.cssnewbie.com/how-to-create-simple-stylish-and-swappable-image-captions/">How To Create Simple, Stylish and Swappable Image Captions.</a> I had a lot of fun coming up with this technique and boiling it down to its bare essentials for the article. I received a lot of great feedback, and hope to release a new version this year.</li><li><a
href="http://www.cssnewbie.com/super-simple-horizontal-navigation-bar/">A Super Simple Horizontal Navigation Bar.</a> Navbars don&#8217;t get much simpler to build than this. This article is truly Navigation for Newbies, and is worth a look.</li><li><a
href="http://www.cssnewbie.com/advanced-jquery-tabbed-box-techniques/">Advanced jQuery Tabbed Box Techniques.</a> This article was more advanced and jQuery-heavy than my normal fare, but it contains tons of useful information and explains how to build a pretty useful product.</li></ol><h3>Have a Say</h3><p>So that was 2009 in a nutshell. But now it&#8217;s time to look forward. What would you like to see from CSS Newbie in 2010? I&#8217;ve started a new poll in the site&#8217;s footer (also available below), and I&#8217;d love to get your feedback. What sort of articles would you most like to see appear here? You can select as many choices as you&#8217;d like. If you&#8217;d like to see something I haven&#8217;t thought of, leave me a comment below!</p> Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.<p>Thanks again for a great year, folks. Let&#8217;s make this next one even better!</p><div
id="crp_related"><h3>Related Reading:</h3><ul><li><a
href="http://www.cssnewbie.com/the-year-in-review-2008/" rel="bookmark" class="crp_title">The Year In Review: 2008</a></li><li><a
href="http://www.cssnewbie.com/the-simple-secret-to-good-dropdown-navigation/" rel="bookmark" class="crp_title">The Simple Secret to Good Dropdown Navigation</a></li><li><a
href="http://www.cssnewbie.com/new-to-css/" rel="bookmark" class="crp_title">New to CSS? New to CSSnewbie? Start Here!</a></li><li><a
href="http://www.cssnewbie.com/5-great-uses-for-the-css-display-property/" rel="bookmark" class="crp_title">5 Great Uses for the CSS Display Property</a></li><li><a
href="http://www.cssnewbie.com/super-simple-horizontal-navigation-bar/" rel="bookmark" class="crp_title">A Super Simple Horizontal Navigation Bar</a></li></ul></div>
<p><a href="http://feedads.g.doubleclick.net/~a/DwPv-tXAsqgbNLTWrN7dHq1whOY/0/da"><img src="http://feedads.g.doubleclick.net/~a/DwPv-tXAsqgbNLTWrN7dHq1whOY/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/DwPv-tXAsqgbNLTWrN7dHq1whOY/1/da"><img src="http://feedads.g.doubleclick.net/~a/DwPv-tXAsqgbNLTWrN7dHq1whOY/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cssnewbie?a=mtyyYq41oQI:7iCuQfgI9L0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=mtyyYq41oQI:7iCuQfgI9L0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=mtyyYq41oQI:7iCuQfgI9L0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=mtyyYq41oQI:7iCuQfgI9L0:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=mtyyYq41oQI:7iCuQfgI9L0:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=mtyyYq41oQI:7iCuQfgI9L0:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=mtyyYq41oQI:7iCuQfgI9L0:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=mtyyYq41oQI:7iCuQfgI9L0:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=mtyyYq41oQI:7iCuQfgI9L0:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=mtyyYq41oQI:7iCuQfgI9L0:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cssnewbie/~4/mtyyYq41oQI" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.cssnewbie.com/the-best-css-newbie-articles-of-2009/feed/</wfw:commentRss> <slash:comments>3</slash:comments> <feedburner:origLink>http://www.cssnewbie.com/the-best-css-newbie-articles-of-2009/</feedburner:origLink></item> <item><title>Build a Bare-Bones WordPress 2.8+ Widget</title><link>http://feedproxy.google.com/~r/cssnewbie/~3/nlQ9rPjTmmE/</link> <comments>http://www.cssnewbie.com/build-a-bare-bones-wordpress-2-8-widget/#comments</comments> <pubDate>Mon, 21 Dec 2009 21:22:56 +0000</pubDate> <dc:creator>Rob Glazebrook</dc:creator> <category><![CDATA[Advanced Techniques]]></category> <category><![CDATA[Downloads]]></category> <category><![CDATA[advanced]]></category> <category><![CDATA[download]]></category> <category><![CDATA[php]]></category> <category><![CDATA[widget]]></category> <category><![CDATA[wordpress]]></category><guid isPermaLink="false">http://www.cssnewbie.com/?p=478</guid> <description><![CDATA[WordPress widgets have been around for quite some time, but the way they were developed changed with WordPress 2.8. This tutorial walks you through building one from scratch.]]></description> <content:encoded><![CDATA[<p><img
src="http://www.cssnewbie.com/wp-content/uploads/2009/12/barebones-beauty.png" alt="" title="Bare Bones WordPress Widget" width="500" height="210" class="alignnone size-full wp-image-481" /></p><p>This tutorial is going to be a little more advanced than some of the fare around here, but in the end you&#8217;ll have all the information you need to know to create your very own WordPress widget! And that&#8217;s worth the effort, I think. However, you will need to understand a little bit of PHP for this to make much sense.</p><p>Widgets &mdash; essentially fancy drag-and-drop plugins, commonly used in sidebars &mdash; have existed in WordPress for quite some time, but with the release of WordPress 2.8, developers have a brand new widget API that makes creating widgets for WordPress a little bit easier. This tutorial will use the new system for developing our barebones &#8220;Hello World&#8221; widget.</p><p><a
href="http://www.cssnewbie.com/download/barebones.zip">Download the zipped source code here.</a></p><p>I should also note up front that this isn&#8217;t the only way to build a widget. This is simply the way I have learned, and a way that is compatible with the new WordPress 2.8+ widget API.<span
id="more-478"></span></p><h3>Make the Widget Appear</h3><p>There are two things you need to do in ~any~ WordPress plugin, including widgets, to make it appear in your WordPress admin:</p><ol><li>Give it a descriptive naming comment at the top, and</li><li>Drop it into the /wp-content/plugins/ directory.</li></ol><p>Since we&#8217;re worried about the code here, let&#8217;s start with the naming comment.</p><pre class="php">&lt;?php
/*
Plugin Name: Bare Bones Widget
Plugin URI: http://www.cssnewbie.com/build-a-bare-bones-wordpress-2-8-widget/
Description: This is a very Bare Bones WordPress 2.8+ widget. Hello, World!
Author: Rob L Glazebrook
Version: 1.0
Author URI: http://www.cssnewbie.com/
*/
?&gt;</pre><p>Open any WordPress plugin, and you&#8217;ll see something very much like this at the top.  The comment tells WordPress the name of the plugin, as well as who built it, where it can be downloaded, what version it is, and more. Note that all widgets are plugins, but not all plugins are widgets.</p><p>Many people also put copyright, GPL or MIT license information up here, but I&#8217;ve chosen to leave that out. Modify this to suit your specific needs.</p><p>Technically, this is all you need to make the plugin appear in your WordPress &#8220;Manage Plugins&#8221; list. But it won&#8217;t do anything until we give it some functionality!</p><p><img
src="http://www.cssnewbie.com/wp-content/uploads/2009/12/barebones-activate.png" alt="" title="barebones-activate" width="500" height="56" class="alignnone size-full wp-image-482" /></p><h3>Extending the WordPress Widget Class</h3><p>WordPress 2.8 gives us a PHP class called WP_Widget that we can extend. This makes our development faster and keeps things somewhat consistent from place to place. The basic structure we&#8217;re going to use looks like this:</p><pre class="php">class WP_Widget_BareBones extends WP_Widget {
	function WP_Widget_BareBones() {
		// Construct goes here.
	}
	function widget($args, $instance) {
		// Our Widget Function
	}
	function update($new_instance, $old_instance) {
		// Our Update Function
	}
	function form($instance) {
		// Our Form Function
	}
}
add_action('widgets_init', create_function('', 'return register_widget("WP_Widget_BareBones");'));</pre><p>We&#8217;re creating a PHP class for our widget that extends the base WP_Widget class. The class contains four functions:</p><ul><li><strong>WP_Widget_BareBones.</strong> You can call this whatever you want. It&#8217;s what tells WordPress what your widget is called in the program and loads all of our options when the time comes. It&#8217;s commonly referred to as the &#8220;construct&#8221; function.</li><li><strong>widget.</strong> This, along with the next two functions, is a standard name that should not be changed. We&#8217;re overwriting a default class in WP_Widget with our own information. This one displays the widget on the screen when the widget is activated.</li><li><strong>update.</strong> This saves our information when a change is made in the WordPress &#8220;Widgets&#8221; admin.</li><li><strong>form.</strong> This generates the form we use to make changes in the &#8220;Widgets&#8221; admin.</li></ol><p>The final line at the end, add_action, is loading our widget into the widgets_init list, which is called by WordPress when it&#8217;s ready to load all of the activated widgets. Without this, your widget will never show up on the page, so make sure it&#8217;s there!</p><p>Now let&#8217;s go through these functions one by one and fill them in.</p><h3>The Construct</h3><p>This is the function that pulls everything together and makes it work. Ours will look like this:</p><pre class="php">// The widget construct. Mumbo-jumbo that loads our code.
function WP_Widget_BareBones() {
	$widget_ops = array( 'classname' =&gt; 'widget_BareBones', 'description' =&gt; __( &quot;A BareBones Widget&quot; ) );
	$this-&gt;WP_Widget('bareBones', __('BareBones'), $widget_ops);
} // End function WP_Widget_BareBones</pre><p>To be perfectly honest, this is the part that I understand least of all, but it works pretty much like this:</p><ul><li>The $widget_ops variable gets loaded with an array that contains our widget classname (which has to be unique from all other widgets) and a brief description, which is used in the Widget display.</li><li>The second (and final!) line creates an instance of our widget, and loads it with the options array.</li></ul><p>Clear as mud? No worries: this is all you need to do in this section. If you&#8217;re creating a widget of your own, just replace the &#8220;BareBones&#8221; stuff with whatever you want to call your widget, and then give it a description that makes sense for your application.</p><h3>The Widget Function</h3><p>The widget function is what creates our widget and outputs it to the screen. It pulls in two variables which are automatically provided by piggybacking onto the WP_Widgets class.</p><p><strong>$args</strong> contains a list of standard widget variables &mdash; specifically, what should be displayed before and after your widget, and what to put before and after the widget title. These variables can be customized by individual WordPress themes, so it&#8217;s polite (and common practice) to include them.</p><p><strong>$instance</strong> is a cool addition to widgets in WordPress 2.8+. Previously, you could only have a single copy of any widget running at any one time. But now we can have several <em>instances</em> (think: copies) of a widget running at once. And the $instance variable lets WordPress keep track of which copy we&#8217;re working with at any moment. It is an associative array that stores all of our widget-specific settings, which we set via the form function later.</p><p>Our code looks like this:</p><pre class="php">// This code displays the widget on the screen.
function widget($args, $instance) {
	extract($args);
	echo $before_widget;
	if(!empty($instance['title'])) {
		echo $before_title . $instance['title'] . $after_title;
	}
	echo &quot;&lt;p&gt;Hello world!&lt;/p&gt;&quot;;
	echo $after_widget;
} // End function widget.</pre><p>First we&#8217;re using the extract() function to create the more recognized $args variables: $before_widget, $after_widget, $before_title, and $after_title. If you&#8217;ve ever done any WordPress template building, you&#8217;ve probably seen these before in the functions.php file.</p><p>Next, all we&#8217;re doing is echoing stuff to the screen. We echo $before_widget, then check to see if this instance of our widget has a title set. If so, we echo $before_title, our title, then $after_title. Then we echo a paragraph that says, &#8220;Hello world!&#8221; And we close by echoing $after_widget.</p><h3>The Update Function</h3><p>This function is used to save our widget information to the database. Every time someone goes into the Widget screen in WordPress and changes some options, this function will be called. Our is extremely simple:</p><pre class="php">// Updates the settings.
function update($new_instance, $old_instance) {
	return $new_instance;
} // End function update</pre><p>As you can see, we get two variables to work with:</p><ul><li><strong>$new_instance</strong>, which contains all of our new information about this particular instance of the widget, and</li><li><strong>$old_instance</strong>, which contains all of the previously saved widget information.</li></ul><p>If we wanted to, we could write a bunch of code in here to make sure our variables are sanitary before writing them to the database, check them against the old variables, and whatever. All we&#8217;re doing here is returning the new information, which saves it to the database.</p><h3>The Form Function</h3><p>This form creates our widget administration panel on the Widget page in the WordPress control panel. We&#8217;re only going to have one editable option, the title, in this example. Complex widgets often have dozens of options, but once you see how to do one, the others should make sense.</p><pre class="php">// The admin form.
function form($instance) {
	echo '&lt;div id=&quot;bareBones-admin-panel&quot;&gt;';
	echo '&lt;label for=&quot;' . $this-&gt;get_field_id(&quot;title&quot;) .'&quot;&gt;BareBones Title:&lt;/label&gt;';
	echo '&lt;input type=&quot;text&quot; class=&quot;widefat&quot; ';
	echo 'name=&quot;' . $this-&gt;get_field_name(&quot;title&quot;) . '&quot; ';
	echo 'id=&quot;' . $this-&gt;get_field_id(&quot;title&quot;) . '&quot; ';
	echo 'value=&quot;' . $instance[&quot;title&quot;] . '&quot; /&gt;';
	echo '&lt;p&gt;This widget will display the title you choose above followed by a &quot;Hello World!&quot; statement.&lt;/p&gt;';
	echo '&lt;/div&gt;';
} // end function form</pre><p>I&#8217;ve broken this code up over several &#8220;echo&#8221; lines just to make it easier to read and dissect; normally I would have done this all in one or two lines. But this makes it easier to see a few important variables to point out.</p><ul><li><strong>$this-&gt;get_field_id(&#8220;title&#8221;)</strong> calls a WordPress-generated ID for the title of our widget. I&#8217;m applying it to the label for usability purposes; if you include a for=&#8221;whatever&#8221; attribute in your labels, and then have an input with a matching ID, clicking the label selects the form.</li><li><strong>$this-&gt;get_field_name(&#8220;title&#8221;)</strong> calls a WordPress-generated name field. This is important to have: WordPress uses it to assign and call database information.</li><li>We&#8217;re using <strong>$this-&gt;get_field_id(&#8220;title&#8221;)</strong> again within our input tag, as mentioned above.</li><li>We&#8217;re also putting $instance["title"] in our value attribute. This puts the current title in our text field. It&#8217;s an important usability consideration: without this line, people would type in a title, save, and the title would disappear! It&#8217;d be safe in the database, but the user wouldn&#8217;t know that.</li></ul><p>It&#8217;s probably also worth noting that, although we&#8217;re including an input tag like you&#8217;d normally find in a form, we <em>are not</em> including a form tag.</p><h3>Putting it All Together</h3><p>And with that, we have a functioning widget! Our full code looks like this:</p><pre class="php">&lt;?php
/*
Plugin Name: Bare Bones Widget
Plugin URI: http://www.cssnewbie.com/build-a-bare-bones-wordpress-2-8-widget/
Description: This is a very Bare Bones WordPress 2.8+ widget. Hello, World!
Author: Rob L Glazebrook
Version: 1.0
Author URI: http://www.cssnewbie.com/
*/

class WP_Widget_BareBones extends WP_Widget {

	// The widget construct. Mumbo-jumbo that loads our code.
	function WP_Widget_BareBones() {
		$widget_ops = array( 'classname' =&gt; 'widget_BareBones', 'description' =&gt; __( &quot;A BareBones Widget&quot; ) );
		$this-&gt;WP_Widget('bareBones', __('BareBones'), $widget_ops);
	} // End function WP_Widget_BareBones

	// This code displays the widget on the screen.
	function widget($args, $instance) {
		extract($args);
		echo $before_widget;
		if(!empty($instance['title'])) {
			echo $before_title . $instance['title'] . $after_title;
		}
		echo &quot;&lt;p&gt;Hello world!&lt;/p&gt;&quot;;
		echo $after_widget;
	} // End function widget.

	// Updates the settings.
	function update($new_instance, $old_instance) {
		return $new_instance;
	} // End function update

	// The admin form.
	function form($instance) {
		echo '&lt;div id=&quot;bareBones-admin-panel&quot;&gt;';
		echo '&lt;label for=&quot;' . $this-&gt;get_field_id(&quot;title&quot;) .'&quot;&gt;BareBones Title:&lt;/label&gt;';
		echo '&lt;input type=&quot;text&quot; class=&quot;widefat&quot; ';
		echo 'name=&quot;' . $this-&gt;get_field_name(&quot;title&quot;) . '&quot; ';
		echo 'id=&quot;' . $this-&gt;get_field_id(&quot;title&quot;) . '&quot; ';
		echo 'value=&quot;' . $instance[&quot;title&quot;] . '&quot; /&gt;';
		echo '&lt;p&gt;This widget will display the title you choose above followed by a &quot;Hello World!&quot; statement.&lt;/p&gt;';
		echo '&lt;/div&gt;';
	} // end function form

} // end class WP_Widget_BareBones

// Register the widget.
add_action('widgets_init', create_function('', 'return register_widget(&quot;WP_Widget_BareBones&quot;);'));
?&gt;</pre><p>And when we upload it to our /wp-content/plugins/ directory and enable it on our Plugins screen, we&#8217;ll see the widget appear on our Widgets control panel.</p><p><a
href="http://www.cssnewbie.com/wp-content/uploads/2009/12/barebones-widgetlist.png"><img
src="http://www.cssnewbie.com/wp-content/uploads/2009/12/barebones-widgetlist.png" alt="" title="Widget List containing our Bare-Bones Widget" width="500" height="210" class="alignnone size-full wp-image-483" /></a></p><p>And when we drag it into a sidebar, we&#8217;ll be able to edit the title to whatever we want.</p><p><img
src="http://www.cssnewbie.com/wp-content/uploads/2009/12/barebones-widgetform.png" alt="" title="Completed Widget!" width="500" height="210" class="alignnone size-full wp-image-480" /></p><p>And now, presuming you have a widget-enabled theme, your widget will show up automatically on your blog!</p><p>That&#8217;s all for today. In the future I hope to expand this tutorial into a second part, where we go through creating a more complex WordPress widget. Until then, if you&#8217;d like to download a copy of the BareBones WordPress widget for yourself, <a
href="http://www.cssnewbie.com/download/barebones.zip">the source code (zipped) is available here.</a></p><div
id="crp_related"><h3>Related Reading:</h3><ul><li><a
href="http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/" rel="bookmark" class="crp_title">How To Emulate a Foreach Loop in JavaScript</a></li><li><a
href="http://www.cssnewbie.com/simple-jquery-stylesheet-switcher/" rel="bookmark" class="crp_title">A Simple jQuery Stylesheet Switcher</a></li><li><a
href="http://www.cssnewbie.com/make-an-entire-list-item-clickable/" rel="bookmark" class="crp_title">Improve Usability and CTR: Make an Entire List Item Clickable</a></li><li><a
href="http://www.cssnewbie.com/how-to-fix-wordpress-feedburner-plugins-after-converting-to-google-feedburner/" rel="bookmark" class="crp_title">How to Fix WordPress Feedburner Plugins After Converting to Google Feedburner</a></li><li><a
href="http://www.cssnewbie.com/runing-jquery-with-other-frameworks-via-noconflict/" rel="bookmark" class="crp_title">Running jQuery with Other Frameworks Via noConflict</a></li></ul></div>
<p><a href="http://feedads.g.doubleclick.net/~a/KBHNY3zGnLn4extjRAFeUkUT7t8/0/da"><img src="http://feedads.g.doubleclick.net/~a/KBHNY3zGnLn4extjRAFeUkUT7t8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/KBHNY3zGnLn4extjRAFeUkUT7t8/1/da"><img src="http://feedads.g.doubleclick.net/~a/KBHNY3zGnLn4extjRAFeUkUT7t8/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cssnewbie?a=nlQ9rPjTmmE:1tpZ0uzjEhQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=nlQ9rPjTmmE:1tpZ0uzjEhQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=nlQ9rPjTmmE:1tpZ0uzjEhQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=nlQ9rPjTmmE:1tpZ0uzjEhQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=nlQ9rPjTmmE:1tpZ0uzjEhQ:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=nlQ9rPjTmmE:1tpZ0uzjEhQ:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=nlQ9rPjTmmE:1tpZ0uzjEhQ:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=nlQ9rPjTmmE:1tpZ0uzjEhQ:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=nlQ9rPjTmmE:1tpZ0uzjEhQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=nlQ9rPjTmmE:1tpZ0uzjEhQ:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cssnewbie/~4/nlQ9rPjTmmE" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.cssnewbie.com/build-a-bare-bones-wordpress-2-8-widget/feed/</wfw:commentRss> <slash:comments>9</slash:comments> <feedburner:origLink>http://www.cssnewbie.com/build-a-bare-bones-wordpress-2-8-widget/</feedburner:origLink></item> <item><title>A Full-Width Centered Navigation Bar</title><link>http://feedproxy.google.com/~r/cssnewbie/~3/idejsB8Lguc/</link> <comments>http://www.cssnewbie.com/full-width-centered-navigation-bar/#comments</comments> <pubDate>Thu, 17 Dec 2009 22:57:08 +0000</pubDate> <dc:creator>Rob Glazebrook</dc:creator> <category><![CDATA[Basic Techniques]]></category> <category><![CDATA[Floats and Positioning]]></category> <category><![CDATA[Navigation]]></category> <category><![CDATA[css]]></category> <category><![CDATA[float]]></category> <category><![CDATA[Lists]]></category> <category><![CDATA[pseudo-class]]></category><guid isPermaLink="false">http://www.cssnewbie.com/?p=472</guid> <description><![CDATA[A friend challenged me to develop a simple horizontal navigation bar that could be span the width of the screen but center the navigation area over his content. This is the result of that challenge.]]></description> <content:encoded><![CDATA[<p><a
href="http://www.cssnewbie.com/example/horizontal-menu/centered.html"><img
src="http://www.cssnewbie.com/wp-content/uploads/2009/12/centered-navbar-beauty.png" alt="Centered Navigation Bar - Click to see it in action!" title="Centered Navigation Bar - Click to see it in action!" width="500" height="100" class="alignnone size-full wp-image-473" /></a></p><p>Right around the time I was developing the code for the <a
href="http://www.cssnewbie.com/super-simple-horizontal-navigation-bar/">Super Simple Navigation Bar</a> I wrote about a while back, a friend came to me with an interesting problem. He needed a horizontal navigation bar like the one I was creating, with the following changes:</p><ol><li>The navigation bar background should stretch the full width of the screen (not just the width of the centered content area), but</li><li>The navigation elements themselves should still be centered over the content area.</li></ol><p>The sketch below outlines the basic design concept. <span
id="more-472"></span></p><p><img
src="http://www.cssnewbie.com/wp-content/uploads/2009/12/centered-navbar-sketch.png" alt="Sketch of a centered full-width navigation bar." title="Sketch of a centered full-width navigation bar." width="500" height="321" class="alignnone size-full wp-image-474" /></p><p>My friend had found some code elsewhere that did what he wanted, but it required two divs in addition to the navigation list to do its centering magic. He wanted to know if I could do it better.</p><p>This tutorial is the result of that challenge.</p><h3>The HTML</h3><p>I was able to reduce the number of extra divs required down to one. Sadly, I don&#8217;t think it&#8217;s possible to do this without at least one extra div &mdash; or turning to JavaScript instead of relying entirely on CSS.</p><pre class="html">&lt;div id=&quot;nav&quot;&gt;
	&lt;ul&gt;
		&lt;li&gt;&lt;a href=&quot;#&quot;&gt;About Us&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Our Products&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;#&quot;&gt;FAQs&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Login&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
&lt;/div&gt;</pre><p>If you&#8217;re familiar with the code for <a
href="http://www.cssnewbie.com/super-simple-horizontal-navigation-bar/">the Super Simple Navigation Bar,</a> you&#8217;ll see it&#8217;s nearly identical, except that I&#8217;ve wrapped the ul tag in a div and moved the &#8220;nav&#8221; ID to that div.</p><p>This navigation code should be placed outside of your centered content container to allow us to stretch it the full width of the browser window in our CSS.</p><p>All in all, I think this is an acceptable amount of code for a navigation bar.</p><h3>The CSS</h3><p>Again, all I did here was take the previous navigation bar CSS and modify it a bit. The results are below:</p><pre class="css">#nav {
	width: 100%;
	float: left;
	margin: 0 0 1em 0;
	padding: 0;
	background-color: #f2f2f2;
	border-bottom: 1px solid #ccc;  }
#nav ul {
	list-style: none;
	width: 800px;
	margin: 0 auto;
	padding: 0; }
#nav li {
	float: left; }
#nav li a {
	display: block;
	padding: 8px 15px;
	text-decoration: none;
	font-weight: bold;
	color: #069;
	border-right: 1px solid #ccc; }
#nav li:first-child a {
	border-left: 1px solid #ccc; }
#nav li a:hover {
	color: #c00;
	background-color: #fff; }</pre><p>Our #nav div is stretched to 100% of the browser window and floated left. The &#8220;float: left&#8221; rule might seem a little strange, because we don&#8217;t want anything to wrap around our navbar.</p><p>This is a little trick that takes advantage of how floats behave. If a container contains nothing but a floated element, that container will collapse down to a height of zero, because floating an element takes it out of the normal document flow. That is, unless the container is floated as well. Then, suddenly, our container only collapses down to the size of the contained float.</p><p>Why is this important? That&#8217;s what lets us set a background color and border on our #nav element and have it still appear. If the #nav weren&#8217;t floated, it would collapse to a height of zero, rendering our background and border invisible!</p><p>Next, we&#8217;ve given the &#8220;#nav ul&#8221; a fixed width and used the auto margin trick to center it. The width here is critical: you want it to be the same width as your centered content area. In this example, that&#8217;s 800 pixels.</p><p>I&#8217;m making use of one pseudo-class to interesting effect here. The &#8220;:first-child&#8221; pseudo-class applies to any element that is the first child in its parent. In our case, this rule is finding the first &#8220;li&#8221; in our parent &#8220;ul&#8221;. We&#8217;re then applying a border to the left side of the anchor tag inside. Without it, our first element wouldn&#8217;t have a border on the left like the others appear to have. While we could technically do that with a class or ID on the first element, I think this solution is a little more elegant.</p><p>The rest of the CSS is identical to that in our <a
href="http://www.cssnewbie.com/super-simple-horizontal-navigation-bar/">simple horizontal navigation bar</a> and is mostly presentational. You can customize the colors, padding, hovers and everything else to make the navigation bar your own.</p><p>You can see this code in action here. Hopefully others will find this solution as useful as my friend did!</p><div
id="crp_related"><h3>Related Reading:</h3><ul><li><a
href="http://www.cssnewbie.com/super-simple-horizontal-navigation-bar/" rel="bookmark" class="crp_title">A Super Simple Horizontal Navigation Bar</a></li><li><a
href="http://www.cssnewbie.com/the-simple-secret-to-good-dropdown-navigation/" rel="bookmark" class="crp_title">The Simple Secret to Good Dropdown Navigation</a></li><li><a
href="http://www.cssnewbie.com/easy-css-dropdown-menus/" rel="bookmark" class="crp_title">Easy CSS Dropdown Menus</a></li><li><a
href="http://www.cssnewbie.com/horizontal-dropdown-menus/" rel="bookmark" class="crp_title">Horizontal CSS Dropdown Menus</a></li><li><a
href="http://www.cssnewbie.com/harnessing-positioning-1/" rel="bookmark" class="crp_title">Harnessing CSS Positioning: Part 1</a></li></ul></div>
<p><a href="http://feedads.g.doubleclick.net/~a/O3wJ9TE_gRBnwjblxpyhowQ0siA/0/da"><img src="http://feedads.g.doubleclick.net/~a/O3wJ9TE_gRBnwjblxpyhowQ0siA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/O3wJ9TE_gRBnwjblxpyhowQ0siA/1/da"><img src="http://feedads.g.doubleclick.net/~a/O3wJ9TE_gRBnwjblxpyhowQ0siA/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cssnewbie?a=idejsB8Lguc:wb3rzLnD33E:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=idejsB8Lguc:wb3rzLnD33E:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=idejsB8Lguc:wb3rzLnD33E:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=idejsB8Lguc:wb3rzLnD33E:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=idejsB8Lguc:wb3rzLnD33E:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=idejsB8Lguc:wb3rzLnD33E:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=idejsB8Lguc:wb3rzLnD33E:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=idejsB8Lguc:wb3rzLnD33E:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=idejsB8Lguc:wb3rzLnD33E:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=idejsB8Lguc:wb3rzLnD33E:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cssnewbie/~4/idejsB8Lguc" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.cssnewbie.com/full-width-centered-navigation-bar/feed/</wfw:commentRss> <slash:comments>10</slash:comments> <feedburner:origLink>http://www.cssnewbie.com/full-width-centered-navigation-bar/</feedburner:origLink></item> <item><title>How To Create Simple, Stylish and Swappable Image Captions</title><link>http://feedproxy.google.com/~r/cssnewbie/~3/5VE19ew-rNA/</link> <comments>http://www.cssnewbie.com/how-to-create-simple-stylish-and-swappable-image-captions/#comments</comments> <pubDate>Thu, 03 Dec 2009 23:46:41 +0000</pubDate> <dc:creator>Rob Glazebrook</dc:creator> <category><![CDATA[Advanced Techniques]]></category> <category><![CDATA[Floats and Positioning]]></category> <category><![CDATA[Images]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[caption]]></category> <category><![CDATA[css]]></category> <category><![CDATA[image]]></category> <category><![CDATA[jquery]]></category> <category><![CDATA[semantics]]></category><guid isPermaLink="false">http://www.cssnewbie.com/?p=457</guid> <description><![CDATA[While they say a picture is worth a thousand words, not every image is self-explanatory. Sometimes a few words of description or context can make the difference between a confusing image and a clarifying one. That's why image captions are often a good idea.]]></description> <content:encoded><![CDATA[<p><a
href="http://www.cssnewbie.com/example/image-captions/"><img
src="http://www.cssnewbie.com/wp-content/uploads/2009/12/image-captions.png" alt="Image Captions" title="Simple Image Captions: Click here to view a demo." width="500" height="250" class="alignnone size-full wp-image-460" /></a></p><p>While they say a picture is worth a thousand words, not every image is self-explanatory. Sometimes a few words of description or context can make the difference between a confusing image and a clarifying one. That&#8217;s why image captions are often a good idea.</p><p>I&#8217;ve always liked the idea of giving images captions, but I rarely do. And why not? It seems to me that most image caption solutions out there:</p><ul><li>Require a lot of excess HTML,</li><li>Make it difficult to redesign (because of the rigidity, usually),</li><li>Don&#8217;t make the relationship between image and caption clear, or</li><li>All of the above.</li></ul><p>Today, I propose a solution that addresses many &mdash; if not all &mdash; of these concerns. <span
id="more-457"></span></p><p><a
href="http://www.cssnewbie.com/example/image-captions/">You can view the demo here,</a> or read on for the why- and how-to.</p><h3>How Not To Do Captions</h3><p>An example of an old-school way to deal with image captions might look like this:</p><pre class="html">&lt;table class=&quot;image-caption&quot;&gt;
	&lt;tr&gt;
		&lt;td&gt;&lt;img src=&quot;myimage.jpg&quot; alt=&quot;&quot; /&gt;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt;This is a caption for the image above.&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;</pre><p>One thing this solution does well is make the relationship between the image and the caption clear: items in the same column of a table are (or at least should be) related in some way.</p><p>However, it requires a ton of markup for something as simple as a line or two of related text. And let&#8217;s pretend you have a blog with 300 posts that contain images with this style of caption. What happens when, a year down the line, you decide the caption would look better above the image in the new design? There goes your weekend.</p><p>Here&#8217;s another example that&#8217;s much cleaner, but has problems of its own:</p><pre class="html">&lt;img src=&quot;myimage.jpg&quot; alt=&quot;&quot; /&gt;
&lt;p&gt;This image is swiped from Flickr.&lt;/p&gt;</pre><p>This solution is nice and clean, but it makes a lot of assumptions. Your readers should assume, for example, that paragraphs following your images describe that image. Which may be true most of the time, but probably isn&#8217;t true all of the time.</p><p>And while you could give that paragraph a certain class or ID to style it more like a caption, code-wise they&#8217;re still semantically unrelated.</p><p>So what&#8217;s the solution?</p><h3>The Goal</h3><p>My goal was to create something that would:</p><ul><li>Retain some resemblance to good, semantic code,</li><li>Keep my HTML as clean as possible, and</li><li>Be easy to style and restyle at will.</li></ul><p>But in order to do any fancy CSS work, I&#8217;d need some hooks in my HTML. And adding more HTML makes it less clean, harder to revise in the future, and reduces my odds at keeping things semantically meaningful.</p><p>To solve this thorny catch-22, I turned (yet again) to our good friend jQuery.</p><h3>The Solution</h3><p>In order to keep my HTML clean, I decided to limit myself to a traditional image tag. Nothing simpler, right?</p><pre class="html">&lt;img src=&quot;my_image.jpg&quot; alt=&quot;&quot; class=&quot;hascaption&quot; /&gt;</pre><p>I also allowed myself the luxury of a special &#8220;hascaption&#8217; class, but if all the images were going to have captions, this wouldn&#8217;t even be necessary. But then where does the caption come from? Well, why not the image tag itself?</p><p>Image tags have a &#8220;title&#8221; attribute which is supposed to contain extra information about the image. Extra, related information? That sounds like a caption to me! So let&#8217;s use it as such:</p><pre class="html">&lt;img src=&quot;my_image.jpg&quot; alt=&quot;&quot; class=&quot;hascaption&quot; title=&quot;This is going to be my caption!&quot; /&gt;</pre><p>Now our caption is contained within our image tag, which is about as semantically meaningful as you can get. And we&#8217;re off to a good start: all modern browsers display image titles as a &#8220;tooltip,&#8221; which is sort of a poor man&#8217;s caption. But we want ours to be fancier, and styleable (you can&#8217;t style tooltips).</p><p>Here&#8217;s some jQuery that provides us a nice structure:</p><pre class="js">$(document).ready(function() {
	$(&quot;img.hascaption&quot;).each(function() {
		$(this).wrap('&lt;div class=&quot;figure&quot;&gt;&lt;/div&gt;')
		.after('&lt;p class=&quot;caption&quot;&gt;'+$(this).attr(&quot;title&quot;)+'&lt;/p&gt;')
		.removeAttr('title');
	});
	$(&quot;.figure&quot;).width($(this).find('img').width());
});</pre><p>This code loops through our document and finds all the images with a class of &#8220;hascaption&#8221; and wraps them in a div called &#8220;figure&#8221;. Then it adds a paragraph after the image (inside the div) with a class of &#8220;caption&#8221;. It then fills the paragraph with the contents of the image&#8217;s title tag. Then it removes the title attribute to prevent the tooltip from competing with our caption (a bit extreme, but it works).</p><p>And the last line of code is a little trick that makes the &#8220;figure&#8221; div the exact same width as our image, so it only takes up as much room as necessary. If you chose to float your div or specify a predetermined width in your CSS, you wouldn&#8217;t need this line.</p><p>So now we have a rendered structure that looks more like this:</p><pre class="html">&lt;div class=&quot;figure&quot;&gt;
	&lt;img src=&quot;my_image.jpg&quot; alt=&quot;&quot; class=&quot;hascaption&quot; /&gt;
	&lt;p class=&quot;caption&quot;&gt;This is going to be my caption!&lt;/p&gt;
&lt;/div&gt;</pre><p>This gives us plenty of hooks for some CSS/JS fun, but it didn&#8217;t require us to write any extra code by hand, and keeps our HTML looking clean. Anyone viewing the source will just see the original image tag, not the code above.</p><p>And best of all, if a year from now we want the caption to go above the image, it&#8217;ll only take changing <em>one line of jQuery,</em> versus changing hundreds of articles manually.</p><h3>Adding Some Flair</h3><p>I wanted these captions to have a bit of flair: the caption should appear directly on top of the image when the user mouses over (close visual proximity providing relevance), but disappear when the mouse goes away (so as not to clutter or obscure the image). For that, I needed a couple more lines of jQuery:</p><pre class="js">$(document).ready(function() {
	$(&quot;img.hascaption&quot;).each(function() {
		$(this).wrap('&lt;div class=&quot;figure&quot;&gt;&lt;/div&gt;')
		.after('&lt;p class=&quot;caption&quot;&gt;'+$(this).attr(&quot;title&quot;)+'&lt;/p&gt;')
		.removeAttr('title');
	});
	$(&quot;.figure&quot;).width($(this).find('img').width());

	$(&quot;.figure&quot;).mouseenter(function(){
		$(this).find('.caption').slideToggle();
	}).mouseleave(function(){
		$(this).find('.caption').slideToggle();
	});
});</pre><p>And some quick CSS:</p><pre class="css">.figure {
	position: relative; }
.figure p.caption {
	display: none;
	position: absolute;
	bottom: 0px;
	left: 0px;
	margin: 0;
	width: 96%;
	padding: 5px 3%;
	background-color: #555;
	color: #fff;
	font-weight: bold; }
</pre><p><strong>Note:</strong> The CSS above has been modified slightly from the original to account for an IE7 bug.</p><p>The jQuery is making use of the &#8220;mouseenter&#8221; and &#8220;mouseleave&#8221; functions built into jQuery. Whenever the mouse enters or leaves the space occupied by the &#8220;figure&#8221; div, the &#8220;slideToggle&#8221; function fires.</p><p>I think slideToggle is pretty cool: it &#8220;slides&#8221; the content in if it&#8217;s not currently there, and slides the content out if it is already present: it toggles between the two states.</p><p>The CSS is doing two primary things: setting a relative position on the figure div, and an absolute position on the caption paragraph. Then we just position the caption at the bottom-left of the figure using the power of absolute-inside-relative positioning (one of my favorite positioning tricks!). I gave the caption a nice dark background and made the text white and bold to provide plenty of contrast, but yours could be styled however you like. I&#8217;m also using a percentage width, plus a percentage padding on the left and right sides, to ensure the caption always stretches the entire width of the image.</p><p><a
href="http://www.cssnewbie.com/example/image-captions/">View the whole thing in action.</a></p><p>Admittedly, this is really just a proof-of-concept and could probably stand to be cleaned up a bit. If there&#8217;s any interest, I might post a follow-up article that makes this a little prettier.</p><div
id="crp_related"><h3>Related Reading:</h3><ul><li><a
href="http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/" rel="bookmark" class="crp_title">How To Emulate a Foreach Loop in JavaScript</a></li><li><a
href="http://www.cssnewbie.com/generating-automatic-website-footnotes-with-jquery/" rel="bookmark" class="crp_title">Generating Automatic Website Footnotes with jQuery</a></li><li><a
href="http://www.cssnewbie.com/simple-jquery-stylesheet-switcher/" rel="bookmark" class="crp_title">A Simple jQuery Stylesheet Switcher</a></li><li><a
href="http://www.cssnewbie.com/make-an-entire-list-item-clickable/" rel="bookmark" class="crp_title">Improve Usability and CTR: Make an Entire List Item Clickable</a></li><li><a
href="http://www.cssnewbie.com/the-simple-secret-to-good-dropdown-navigation/" rel="bookmark" class="crp_title">The Simple Secret to Good Dropdown Navigation</a></li></ul></div>
<p><a href="http://feedads.g.doubleclick.net/~a/pTzA9h3y0Cqxy0Pf5xWBUZ4ln4Y/0/da"><img src="http://feedads.g.doubleclick.net/~a/pTzA9h3y0Cqxy0Pf5xWBUZ4ln4Y/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/pTzA9h3y0Cqxy0Pf5xWBUZ4ln4Y/1/da"><img src="http://feedads.g.doubleclick.net/~a/pTzA9h3y0Cqxy0Pf5xWBUZ4ln4Y/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cssnewbie?a=5VE19ew-rNA:78WvK2YPxes:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=5VE19ew-rNA:78WvK2YPxes:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=5VE19ew-rNA:78WvK2YPxes:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=5VE19ew-rNA:78WvK2YPxes:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=5VE19ew-rNA:78WvK2YPxes:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=5VE19ew-rNA:78WvK2YPxes:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=5VE19ew-rNA:78WvK2YPxes:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=5VE19ew-rNA:78WvK2YPxes:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/cssnewbie?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cssnewbie?a=5VE19ew-rNA:78WvK2YPxes:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cssnewbie?i=5VE19ew-rNA:78WvK2YPxes:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cssnewbie/~4/5VE19ew-rNA" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.cssnewbie.com/how-to-create-simple-stylish-and-swappable-image-captions/feed/</wfw:commentRss> <slash:comments>36</slash:comments> <feedburner:origLink>http://www.cssnewbie.com/how-to-create-simple-stylish-and-swappable-image-captions/</feedburner:origLink></item> </channel> </rss><!-- This site's performance optimized by W3 Total Cache. Dramatically improve the speed and reliability of your blog!

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk
Database Caching 12/68 queries in 1.346 seconds using disk

Served from: rhetorclick.com @ 2010-03-13 08:38:15 -->
