<?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>KirkHings.com &#187; CSS</title>
	<atom:link href="http://kirkhings.com/tag/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://kirkhings.com</link>
	<description>The '-berger' part trips up so many folks that I sometimes simplify my name for you. Yes, just for you.</description>
	<lastBuildDate>Sat, 13 Jun 2009 18:35:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to Count CSS Specificity Scores and Leverage !Important Overrides</title>
		<link>http://kirkhings.com/code/css/how-to-count-css-specificity-scores-and-leverage-important-overrides/401/</link>
		<comments>http://kirkhings.com/code/css/how-to-count-css-specificity-scores-and-leverage-important-overrides/401/#comments</comments>
		<pubDate>Mon, 25 May 2009 00:43:09 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[concept]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[element]]></category>
		<category><![CDATA[id]]></category>
		<category><![CDATA[inline]]></category>
		<category><![CDATA[selector]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=401</guid>
		<description><![CDATA[ photo credit: laffy4k
If you are having difficulty getting a particular CSS style to &#8220;appear&#8221;, one thing to check is your specificity. CSS specificity is a four digit score for resolving cascade conflicts. You can actually add up the specificity score to determine which conflicting style should prevail. The four digit score is comprised of [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/css/space-critical-css-selectors/65/' rel='bookmark' title='Permanent Link: Space is Critical in CSS Selectors'>Space is Critical in CSS Selectors</a> <small> photo credit: makelessnoise If you are having difficulty getting...</small></li>
<li><a href='http://kirkhings.com/code/c/test-scores-and-letter-grade/413/' rel='bookmark' title='Permanent Link: Test Scores and Letter Grade'>Test Scores and Letter Grade</a> <small> photo credit: quinn.anya This programming challenge is from an...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/73207064@N00/173579871/" title="Score Kept Here" target="_blank"><img src="http://farm1.static.flickr.com/45/173579871_4cae652b36.jpg" alt="Score Kept Here" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/73207064@N00/173579871/" title="laffy4k" target="_blank">laffy4k</a></small></div>
<p>If you are having difficulty getting a particular CSS style to &#8220;appear&#8221;, one thing to check is your specificity. CSS specificity is a four digit score for resolving cascade conflicts. You can actually add up the specificity score to determine which conflicting style should prevail. The four digit score is comprised of inline style, id, class, and element. You can express it as #, #, #, #. Examples are provided below.</p>
<p>For example, what gets displayed when you have two conflicting styles specified for the same element?</p>
<pre class="brush: css;">
li { background-color: blue; }
li { background-color: yellow; }
</pre>
<p>Of course we know intuitively and through practice that the second style is displayed because it is read after the first and so it &#8220;overwrites&#8221; that first style. Less intuitively, you should know that the second wins because it has the same level of specificity as that first style. The two styles above each have the score 0, 0, 0, 1 because they are not inline styles, they have no id or class specification, and they specify only one element each.</p>
<p>Specificity refers to how specific you identify an element. Referring to just &#8216;li&#8217; is just the single list item element, you are not referring to additional element information. This is like specifying different styles based on nesting, such as &#8216;div a&#8217; vs. &#8216;p a&#8217;. Those could have two different styles because they have different nesting.</p>
<p>For another example, which style do you think will get displayed?</p>
<pre class="brush: css;">
ul li { background-color: blue; }
li { background-color: yellow; }
</pre>
<p>The first style will win over the second because of greater element specificity, despite the potential of overwriting later in the script. The first style&#8217;s specificity score is 0, 0, 0, 2 while the second style&#8217;s score is still just 0, 0, 0, 1. That tiny extra specificity causes the first style to win out and get displayed. Now the power of specificity should be dawning on you, and why specificity can cause frustrating display problems in a large stylesheet. Let&#8217;s explore further. </p>
<p>Which style do you think will get displayed?</p>
<pre class="brush: css;">
.nav li { background-color: blue; }
ul li { background-color: yellow; }
</pre>
<p>Of course the first style wins again because the class selector has greater specificity than the element selectors. The score is 0, 0, 1, 1, versus 0, 0, 0, 2.</p>
<p>Which style do you think will get displayed?</p>
<pre class="brush: css;">
#nav li { background-color: blue; }
.nav ul li { background-color: yellow; }
</pre>
<p>Of course the first style wins again because the id selector has greater specificity than the class selector. The score is 0, 1, 0, 1, versus 0, 0, 1, 2.</p>
<p>Which style do you think will get displayed?</p>
<pre class="brush: css;">
#nav .nav div ul li { background-color: yellow; }
...
&lt;body&gt;
&lt;div id=&quot;nav&quot;&gt;&lt;ul&gt;&lt;li style=&quot;background-color: blue;&quot;&gt;Data&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/body&gt;
</pre>
<p>The inline style gets displayed here (though only on the elements containing the inline style) because inline styles have slightly higher specificity than id, class, or element styles specified in header styles or external stylesheets (remember &#8216;cascading&#8217; is a key concept). The score is 1, 0, 0, 0 versus 0, 1, 1, 3. Although the specificity is greater here, you realize inline styles are an evil thing because you are mixing style with content. You should know the rule exists and how it works, and then pledge to never take it that far unless you are debugging code. </p>
<p>Speaking of debugging code, what if you specify something as much as you reasonably can, yet you cannot get it to display? Rather than dropping it into an inline style, you can use the !important keyword in your style, which functions as a kind of override function as far as specificity goes. </p>
<p>Which style do you think will get displayed?</p>
<pre class="brush: css;">
#nav .nav div ul li { background-color: blue !important; }
#nav .nav div ul li { background-color: yellow; }
</pre>
<p>Of course the first style wins again because of the !important keyword (yes, it will also beat an inline style specification). This case indicates you have conflicting styles in your stylesheet and you need to check specificity to target what is conflicting. If your style does not display when you use !important, it indicates you have another problem like misspelling an element, class, or id. </p>
<p>After you have fixed the conflict, go back and remove the !important keyword or you will forever be chasing your tail across stylesheets with more and more !important declarations. Use !important only for debugging purposes. </p>
<p>Finally, remember specificity only comes into play when styles conflict. You can have dozens of separate style statements work beautifully together so long as they are specifying different style elements (such as background-color, font-style, color, padding, etc.) It is only when they specify the same style elements with conflicting values does the specificity score become important.</p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/css/space-critical-css-selectors/65/' rel='bookmark' title='Permanent Link: Space is Critical in CSS Selectors'>Space is Critical in CSS Selectors</a> <small> photo credit: makelessnoise If you are having difficulty getting...</small></li>
<li><a href='http://kirkhings.com/code/c/test-scores-and-letter-grade/413/' rel='bookmark' title='Permanent Link: Test Scores and Letter Grade'>Test Scores and Letter Grade</a> <small> photo credit: quinn.anya This programming challenge is from an...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/css/how-to-count-css-specificity-scores-and-leverage-important-overrides/401/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random BINGO Card Generator with Ajax Reset</title>
		<link>http://kirkhings.com/code/javascript/random-bingo-card-generator-with-ajax-reset/224/</link>
		<comments>http://kirkhings.com/code/javascript/random-bingo-card-generator-with-ajax-reset/224/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 17:17:10 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[randomizer]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=224</guid>
		<description><![CDATA[ photo credit: Keees
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/54366416@N00/6443146/" title="bingo" target="_blank"><img src="http://farm1.static.flickr.com/6/6443146_de0b591435.jpg" alt="bingo" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/54366416@N00/6443146/" title="Keees" target="_blank">Keees</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a BINGO card using HTML and JavaScript. Generate random integers between 1 and 75, and insert them into each of 24 squares, skipping the center FREE space. Integer ranges are as follow: B = 1-15, I = 16-30, N = 31-45, G = 46-60, O = 67-75. Ensure no two squares contain duplicate numbers. Bonus: After initial card load, enable resetting the card without a server call (quasi-Ajax-powered).
</p></blockquote>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Make Your Own Bingo Card&lt;/title&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;script.css&quot; /&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;
	&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Create A Bingo Card&lt;/h1&gt;
&lt;table&gt;
	&lt;tr&gt;
		&lt;th width=&quot;20%&quot;&gt;B&lt;/th&gt;
		&lt;th width=&quot;20%&quot;&gt;I&lt;/th&gt;
		&lt;th width=&quot;20%&quot;&gt;N&lt;/th&gt;
		&lt;th width=&quot;20%&quot;&gt;G&lt;/th&gt;
		&lt;th width=&quot;20%&quot;&gt;O&lt;/th&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square0&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square1&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square2&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square3&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square4&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square5&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square6&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square7&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square8&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square9&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square10&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square11&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;free&quot;&gt;Free&lt;/td&gt;
		&lt;td id=&quot;square12&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square13&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square14&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square15&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square16&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square17&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square18&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square19&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square20&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square21&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square22&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square23&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;a href=&quot;script.html&quot; id=&quot;reload&quot;&gt;Click here&lt;/a&gt; to create a new card&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
	color: black;
	font-size: 20px;
	font-family: &quot;Lucida Grande&quot;, Verdana, Arial, Helvetica, sans-serif;
}

h1, th {
	font-family: Georgia, &quot;Times New Roman&quot;, Times, serif;
}

h1 {
	font-size: 28px;
}

table {
	border-collapse: collapse;
}

th, td {
	padding: 10px;
	border: 2px #666 solid;
	text-align: center;
	font-size: 24px;
}

#free {
	background-color: #ffff00;
}
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
window.onload = initializePage;

// even though arrays are 0-based, creating 76 positions generates elements 0-75, we will later ignore position 0
// note that newly created arrays have all elements set to false
var usedNumbers = new Array(76);

function initializePage() {
	// so older browsers do not get 24 error messages
	if (document.getElementById) {
		// enable creating a new card without reloading page
		document.getElementById(&quot;reload&quot;).onclick = anotherCard;
		// generate the new bingo card
		newCard();
	} else {
		alert(&quot;Sorry, but your browser is not running JavaScript, and this game requires it.&quot;);
	}
}

function newCard() {
	for (var i = 0; i &lt; 24; i++) {
		// pass the current i as parameter to setSquare function
		setSquare(i);
	}
}

function setSquare(thisSquare) {
	// capture current corresponding html square (via id=)  to current digit that was passed over
	var currentSquare = &quot;square&quot; + thisSquare;

	// while bingo numbers are ranged 1-75, letter columns are restricted to specific number ranges
	// b = 1-15, i = 16-30, n = 31-45, g = 46-60, o = 67-75
	// OR mathematically we can generate a random number between 1-15 and add column-specific number
	// b = 0, i = 15, n = 30, g = 45, o = 60	

	// need 5 sets of numbers 0-4 (1 set for each letter)
	// note missing '2,' from third set, so as to skip the middle 'free' square
	var columnPlace = new Array(0,1,2,3,4,0,1,2,3,4,0,1,3,4,0,1,2,3,4,0,1,2,3,4);

	// capture appropriate column mulitple by referencing current square # as array element
	var columnMultiple = (columnPlace[thisSquare] * 15);

	// initialize variable to be used in following do-while loop
	var newNumber;

	// generate newNumber and check if it has been used already, if so loop again
	do {
		// add random number to the particular column multiple
		newNumber = columnMultiple + getNewNumber()  + 1;
	} while(usedNumbers[newNumber]);

	// prevent duplicate numbers by testing if the newNumber has already been used once on this card
	if (!usedNumbers[newNumber]) {
		// if duplicate was not found, this must be an original #, so set this element to true
		usedNumbers[newNumber] = true;

		// assign randomized newNum to current square
		document.getElementById(currentSquare).innerHTML = newNumber;
	}
}

function getNewNumber() {
	// capture random number between 1 and 15 for bingo values
		// math.random() returns random number between 0 and 1
		// math.floor() returns floor of a decimal number (ex. 1.75 becomes 1)
	return Math.floor(Math.random() * 15)
}

function anotherCard() {
	// housekeeping: reset the usedNumbers flag arrays to all false
	for (var i = 1; i &lt; usedNumbers.length; i++) {
		usedNumbers[i] = false;
	}

	// generate the new bingo card
	newCard();
	return false;
}
</pre>
<p>And finally here is a screenshot of the end result: </p>
<div id="attachment_231" class="wp-caption alignleft" style="width: 400px"><img src="http://kirkhings.com/wp-content/uploads/bingocard.png" alt="BINGO card screenshot" title="bingocard" width="390" height="417" class="size-full wp-image-231" /><p class="wp-caption-text">BINGO card screenshot</p></div>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/random-bingo-card-generator-with-ajax-reset/224/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Space is Critical in CSS Selectors</title>
		<link>http://kirkhings.com/code/css/space-critical-css-selectors/65/</link>
		<comments>http://kirkhings.com/code/css/space-critical-css-selectors/65/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 05:04:58 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[concept]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[selector]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=65</guid>
		<description><![CDATA[ photo credit: makelessnoise
If you are having difficulty getting a particular CSS style to &#8220;appear&#8221;, one thing to look at is your selector. A tiny space can cause two different selections if you are not careful.
For example, notice the single space difference between these two selectors:

div#navbar {
    background-color: #999;
}
div #navbar {
  [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/css/how-to-count-css-specificity-scores-and-leverage-important-overrides/401/' rel='bookmark' title='Permanent Link: How to Count CSS Specificity Scores and Leverage !Important Overrides'>How to Count CSS Specificity Scores and Leverage !Important Overrides</a> <small> photo credit: laffy4k If you are having difficulty getting...</small></li>
<li><a href='http://kirkhings.com/code/c/average-rainfall/139/' rel='bookmark' title='Permanent Link: Average Rainfall'>Average Rainfall</a> <small> photo credit: quapan This programming challenge is from an...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/13447091@N00/251478651/" title="Andromeda, again." target="_blank"><img src="http://farm1.static.flickr.com/96/251478651_e0b3f9944d.jpg" alt="Andromeda, again." border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/13447091@N00/251478651/" title="makelessnoise" target="_blank">makelessnoise</a></small></div>
<p>If you are having difficulty getting a particular CSS style to &#8220;appear&#8221;, one thing to look at is your selector. A tiny space can cause two different selections if you are not careful.</p>
<p>For example, notice the single space difference between these two selectors:</p>
<pre class="brush: css;">
div#navbar {
    background-color: #999;
}
div #navbar {
    background-color: #333;
}
</pre>
<p>That presence or lack of a single space between the (div) element and the class selection symbol (#) causes two different selections to occur. No space means the style will apply to (div) elements which are themselves marked with the class navbar. A single space means the style will apply to elements within the (div) element which themselves are marked with the class navbar. </p>
<p>For example, </p>
<pre class="brush: css;">
div#navbar {
    background-color: blue;
}
</pre>
<p>would make this entire div&#8217;s background blue:</p>
<pre class="brush: xml;">
&lt;div class=&quot;navbar&quot;&gt;
  &lt;p&gt;item&lt;/p&gt;
  &lt;p&gt;item&lt;/p&gt;
&lt;/div&gt;
</pre>
<p>However, this:</p>
<pre class="brush: css;">
div #navbar {
    background-color: blue;
}
</pre>
<p>would not apply the style. That critical space indicates a descendant element selector. It is expecting the inner (p) elements to have the navbar class. Since those (p) elements do not have navbar classes, you would see nothing and probably wonder why &#8220;it is not working&#8221;.</p>
<p>The descendant selector can be useful sometimes, but know the difference of what happens if that space appears in that critical spot so you don&#8217;t pull your hair out in frustration. If your style is not appearing, check for this easy space mistake. </p>
<p>Nerd Aside: The # symbol goes by many names around the world, such as pound sign, number sign, hash, hatch, tic-tac-toe, gate, crunch, or my favorite octothorpe. Mmm, octothorpe!</p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/css/how-to-count-css-specificity-scores-and-leverage-important-overrides/401/' rel='bookmark' title='Permanent Link: How to Count CSS Specificity Scores and Leverage !Important Overrides'>How to Count CSS Specificity Scores and Leverage !Important Overrides</a> <small> photo credit: laffy4k If you are having difficulty getting...</small></li>
<li><a href='http://kirkhings.com/code/c/average-rainfall/139/' rel='bookmark' title='Permanent Link: Average Rainfall'>Average Rainfall</a> <small> photo credit: quapan This programming challenge is from an...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/css/space-critical-css-selectors/65/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to change alternating table row colors</title>
		<link>http://kirkhings.com/code/php/how-to-change-alternating-table-row-colors/44/</link>
		<comments>http://kirkhings.com/code/php/how-to-change-alternating-table-row-colors/44/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 06:21:48 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[connection object]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[row]]></category>
		<category><![CDATA[table]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=44</guid>
		<description><![CDATA[ photo credit: Lee Carson
Everyone loves the alternating table row colors on web pages. The same idea is applied to alternating blog comment styles. You can do it quite easily, and here are the three simple steps to do it.
This technique involves PHP, CSS, and a touch of SQL. However, the logic and therefore the [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/59271446@N00/3101281900/" title="Chocolate Santa Clones" target="_blank"><img src="http://farm4.static.flickr.com/3059/3101281900_a8dc7144c0_m.jpg" alt="Chocolate Santa Clones" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by-sa/2.0/" title="Attribution-ShareAlike License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/59271446@N00/3101281900/" title="Lee Carson" target="_blank">Lee Carson</a></small></div>
<p>Everyone loves the alternating table row colors on web pages. The same idea is applied to alternating blog comment styles. You can do it quite easily, and here are the three simple steps to do it.</p>
<p>This technique involves PHP, CSS, and a touch of SQL. However, the logic and therefore the heavy lifting is found in the PHP. You can use this method with table rows (as is commonly used for WordPress comments), or for stacked div elements. I will present a table row version first, then a div version.</p>
<p><strong>Step one</strong> is to style your two table rows so they appear distinct from each other. This of course goes in your stylesheet:</p>
<pre class="brush: css;">
tr.rowA {
    background-color: #666;
    text-align: left;
}
tr.rowB {
    background-color: #333;
    text-align: right;
}
</pre>
<p><strong>Step two</strong> is to write the function to determine if the row is an even or odd row (and therefore rowA or rowB). This is accomplished using the modulus operator (% in PHP). This operation gives you the remainder of a division. No matter what number we feed it, even numbers divided by 2 result in 0 remainder (therefore boolean &#8216;false&#8217;). Odd numbers divided by 2 result in .5 remainder (therefore boolean &#8216;true&#8217;).</p>
<pre class="brush: php;">
function alternatingRows(&amp;$counter) {
	if ($counter % 2) {
		$row = 'rowA';
	}
	else {
		$row = 'rowB';
	}
	$counter++; // increment counter for next iteration
	return $row; // return rowB or rowA
}
</pre>
<p>Note that this function can be placed on the page where it is used, but a better place would a globals file that you include. Code reuse is next to godliness. </p>
<p><strong>Step three</strong> is to write the code to select your data, and present it in a loop. This is assuming you know how to create your connection object and have done so already. </p>
<pre class="brush: php;">
$myQuery= &quot;SELECT * FROM table&quot;;
$myResult = @$connectionObject-&gt;query($myQuery);
</pre>
<p>Note that if this is in a table you will need to code the table start and end tags outside the loop (or you will get multiple tables). </p>
<pre class="brush: php;">
// echo alternating table rows
echo &quot;&lt;table&gt;&quot;;
$ndx = 0;
while ($row = $myResult-&gt;fetch_assoc()) {
    $rowClass = alternatingRows($ndx);
    echo &quot;&lt;tr class=\&quot;$rowClass\&quot;&gt;&quot;;
    echo &quot;&lt;td&gt;$row[data1]&lt;/td&gt;&quot;;
    echo &quot;&lt;td&gt;$row[data2]&lt;/td&gt;&quot;;
    echo &quot;&lt;/tr&gt;&quot;;
}
echo &quot;&lt;/table&gt;&quot;;
</pre>
<p>The function logic is the simplest part. The echoing out your data is the trickiest part, but start small until it works and then you can expand the complexity. The styling is the funnest part, limited only by your imagination and CSS abilities. </p>
<p>Here is another similar example using div elements instead of table rows:</p>
<p>Stylesheet:</p>
<pre class="brush: css;">
.divA {
    float: left;
    background-color: #666;
    text-align: left;
}
.divB {
    float: right;
    background-color: #333;
    text-align: right;
}
</pre>
<p>Function:</p>
<pre class="brush: php;">
function alternatingRows(&amp;$counter) {
	if ($counter % 2) {
		$div = 'divA';
	}
	else {
		$div = 'divB';
	}
	$counter++; // increment counter for next iteration
	return $div; // return divA or divB
}
</pre>
<p>Display:</p>
<pre class="brush: php;">
// echo alternating divs
$ndx = 0;
while ($each = $myResult-&gt;fetch_assoc()) {
    $divClass = alternatingRows($ndx);
    echo &quot;&lt;div class=\&quot;$divClass\&quot;&gt;
    echo &quot;&lt;h1&gt;$each[data1]&lt;/h1&gt;&quot;;
    echo &quot;&lt;p&gt;$each[data2]&lt;/p&gt;&quot;;
    echo &quot;&lt;/div&gt;&quot;;
}
</pre>
<p>Questions or comments?</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/php/how-to-change-alternating-table-row-colors/44/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
