<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
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/"
><channel><title>The Why and The How &#187; CSS</title> <atom:link href="http://www.thewhyandthehow.com/category/css/feed/" rel="self" type="application/rss+xml" /><link>http://www.thewhyandthehow.com</link> <description>Just another WordPress weblog</description> <lastBuildDate>Fri, 09 Jul 2010 02:32:51 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.8.4</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Learn CSS</title><link>http://www.thewhyandthehow.com/learn-css/</link> <comments>http://www.thewhyandthehow.com/learn-css/#comments</comments> <pubDate>Fri, 27 Mar 2009 16:53:36 +0000</pubDate> <dc:creator>Blake Schwendiman</dc:creator> <category><![CDATA[CSS]]></category> <category><![CDATA[Development]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[jquery ui]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=389</guid> <description><![CDATA[In yesterday&#8217;s post, Geeking out, I forgot to mention the most important concept for both web designers and web developers to learn: CSS. Designers probably already understand the advantages, but developers implementing any complex functionality for a web site really need to know CSS today because of the client-side Javascript libraries that depend on CSS.
More [...]]]></description> <content:encoded><![CDATA[<script type="text/javascript" src="/wp-content/themes/thesis/custom/js/learn_css.js"></script><p>In yesterday&#8217;s post, <a
href="http://www.thewhyandthehow.com/geeking-out/">Geeking out</a>, I forgot to mention the most important concept for both web designers and web developers to learn: <a
href="http://www.w3schools.com/css/">CSS</a>. Designers probably already understand the advantages, but developers implementing any complex functionality for a web site really need to know CSS today because of the client-side Javascript libraries that depend on CSS.</p><p>More than likely if you&#8217;re a web developer you&#8217;ve written at least some client-side Javascript. It&#8217;s only going to become more likely. It should be obvious that client-side code provides a great deal of richness and responsiveness to any web application. So if you&#8217;re a web developer that&#8217;s primarily been working on back-end code and you want to stay relevant, learn Javascript, CSS and jQuery. Let me give you one brief example.</p><p>Let&#8217;s say that you want to create a handy sortable list of elements so your end-users can reorder items on their view of a page. The design team wants the list to be displayed with alternating row colors so they are easy to read. Something like this:</p><ul
class="example-sortable"><li>This is item 1</li><li>This is item 2</li><li>This is item 3</li><li>This is item 4</li></ul><p>Assuming the list is dynamically generated from a database and you&#8217;re a PHP programmer, you&#8217;re probably thinking of doing something like this:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p3893');">[<span
id="p3893_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p389code3'); return false;">View Code</a> PHP</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p3893"><td
class="code" id="p389code3"><pre class="php"><span style="color: #000033;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">'&lt;ul&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">'&lt;li '</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$i</span><span style="color: #339933;">++</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'class=&quot;alt-row&quot;'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000033;">$item</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">'&lt;/ul&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p>That&#8217;s great, but it&#8217;s only part of the solution. Since the list is sortable on the client side, the list will have to be re-striped every time an item is moved, otherwise it will look wrong. The sorting and the striping both need to occur on the client side, so simplify the PHP to just create the list with no alternating classes, then use jQuery to do the client-side heavy lifting.</p><p>The jQuery (and jQuery UI for sortable) solution looks like this:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p3894');">[<span
id="p3894_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p389code4'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p3894"><td
class="code" id="p389code4"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> learn_css_restripe<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul.example-sortable li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'alt-row'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul.example-sortable li:even'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'alt-row'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  learn_css_restripe<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// call the restripe function on page ready to set the initial stripes</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// make the list sortable</span>
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul.example-sortable'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">sortable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// whenever the list order changes, restripe</span>
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul.example-sortable'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sortchange'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> ui<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>learn_css_restripe<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p>Understanding CSS and the DOM of the document is what makes using jQuery so powerful. Notice that the restripe function, <em>learn_css_restripe</em> doesn&#8217;t do any looping or have any direct references to an DOM element ids. Instead the function simply removes the alternating row class from every element of any unordered list with the <em>example-sortable</em> class. Then some jQuery selector magic, <em>:even</em>, is used to add the alternating row class back to just the even elements of the lists.</p><p>Since this technique is so generic, it is possible to have more than one list on the same page without changing any of the code at all. Here&#8217;s another list with more elements as an example:</p><ul
class="example-sortable"><li>This is item A</li><li>This is item B</li><li>This is item C</li><li>This is item D</li><li>This is item E</li><li>This is item F</li><li>This is item G</li></ul><p>As you can see, CSS is not just for designers. Great web developers will work with great designers to simplify both the layout and the code at the same time using CSS.</p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/learn-css/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss><!--
This site's performance optimized by W3 Total Cache:

W3 Total Cache improves the user experience of your blog by caching
frequent operations, reducing the weight of various files and providing
transparent content delivery network integration.

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using memcached
Page Caching using memcached
Database Caching 8/16 queries in 0.007 seconds using memcached

Served from: a2.c0.354a.static.theplanet.com @ 2010-09-10 04:14:04 -->