Archive for April, 2010
CSS3 Colors
Apr 28th
You might be wondering what is so special about CSS3 colors and why they’re even different considering that the color and background-color property and their values have not changed since CSS1 specification were released.
As set by CSS1 we can use the following for the color and background-color properties:
| Color | Background-color |
|---|---|
|
|
|
|
Aside from these standard mundane implementations of colors, CSS3 added a whole new color space: HSL.
HSL
HSL stands for Hue Satuation and Lightness. The break down is as follows:
| Hue | Satuation | Lightness |
|---|---|---|
Hue is the degree of a color wheel that goes from 0 to 360. Here as some general colors:
|
Satuation is the amount of color, or how “full” the color is. Here are some values:
|
Lightness is also a percentage but deals with how much white and black is added to the color. Here are some examples:
|
Here is an example of HSL:
Here is an example with RGB:
Here is an example with HEX:
The corresponding CSS for HSL colors is the following:
1 2 3 4 5
background-color:hsl ([0-360], [x]%,[x]%);
background-color:hsl (360, 100% 50%);
background-color:hsl (133,85%, 61%);
RGBA
There is nothing new about the RGB color space. We learned about the RGB color space in Basics of CSS Part 2 and we have seen many colors in the RGB color space and in hex format Color Reference.
Common examples of RGB would be the following:
1 2 3 4 5
background-color:rgb(255, 0, 0); /*red*/
background-color:rgb(0, 255, 0); /*green*/
background-color:rgb(0, 0, 255); /*blue*/
So what does the “a” mean in RGBA? The A stands for alpha which deals with transparency or the opacity of colors. The syntax for the alpha value will span from 0-1 in decimal form. For example:
1 2 3
background-color:rgba(0, 0, 255, 0.4);
color:rgba(0, 0, 255, 0.8);
The first example will set the opacity of pure blue to 40% and the second example will set the opacity of pure blue to 80%.
Alternatively, the RGB colors will be the following:
| RGBA | RGB |
|---|---|
|
background-color:rgba([color 1], [color 2], [color 3], [alpha]); background-color:rgba(0, 0, 255, 1); background-color:rgba(0, 0, 255, 0.8); background-color:rgba(0, 0, 255, 0.6); background-color:rgba(0, 0, 255, 0.4); background-color:rgba(0, 0, 255, 0.2); |
background-color:rgb([color 1], [color 2], [color 3]); background-color:rgb(0, 0, 255); background-color:rgb(50, 50, 254); background-color:rgb(101, 101, 254); background-color:rgb(152, 152, 254); background-color:rgb(203, 203, 254); |
| These colors are not contingent on the color of the background. Since these colors have alpha transparency, they will show any colors or background images behind the elements. | These colors are contingent on the color of the background. If you wish for the element to be a certain color then you need to specify the color you wish to have. Will not show any color behind the element and will not show any background images behind the element because no alpha transparency exists. |
For example
Notice how above they were the same colors and now since we have a different color background the colors with rgba will allow the black background to show through where as the rgb colors will display their respective color values.
Which Browsers Support RGBA?
- Firefox 3.x+
- Safari 3+
- Safari 4+
- Google Chrome
- Opera 10.10+
- Flock 2.02
- Mobile Safari
- Blackberry Browser
IE Hack
Mission someone? Of course we are! We’re missing lovely Internet Explorer and all of its versions. Luckly, there is a hack for IE.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<!--[if IE]>
<style type="text/css">
selector {
background:transparent;
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#990000FF,endColorstr=#990000FF);
}
</style>
<![endif]-->
We can use this hack as inline css or use in an internal or external style sheet. If you want a specific class selector or element to have an translucent color then use the hack for that element or class like the example above. If you want to use inline css then place the hack in the style attribute.
You might be wondering what #800000FF is. Think of this as #AARRGGBB where AA is the alpha value in two integers, RR are the red hexadecimals, GG are the green hexadecimals, and BB are the blue hexadecimals.
In order to covert our initial RGBA value to Hex we need to think interms of Hex to Decimal values.
Hex to Decimal
In order to convert Hex to Decimal we need to think mathematically. We’ll start with an easy example:
| Hex | Decimal |
|---|---|
| #ffffff | rgb(255, 255, 255) |
| #0000ff | rgb(0, 0, 255) |
| #ff00ff | rgb(255, 0, 255) |
How did we get these values of the top of our head? The solution is simple. Follow these steps from Jackson Hines’s post on RGB to HEX and vice versa:
HEX to RGB (Ex: #a84fff)
- Take the first digit of the red part of the hex color. (ex: a)
- If this digit is a letter, replace with the letter of the alphabet and add 9. If it is not a letter, leave it. (ex: a = 1; 1 + 9 = 10)
- Take the second digit of the red part of the hex color. (ex: 8 )
- If this digit is a letter, replace with the letter of the alphabet and add 9. If it is not a letter, leave it. (ex: 8 )
- Divide this number by 16. (ex: 8 ÷ 16 = 0.5)
- Add the number you got in step two and the number you got in step 5. (ex: 10 + 0.5 = 10.5)
- Multiply this number by 16 to get the red value. (ex: 10.5 × 16 = 168)
- Repeat steps 1 through 8 with green and blue values. (ex: 168, 79, 255))
Alpha (#AA) & A
So what about alpha? Knowing the information we have obtained by Jackson’s helpful article, we can convert our rgb value to hex and vice versa. If you wish not to do so much math and want to get results quickly, I recommend using Colorpicker.com.
#AARRGGBB to RGBA
Let us assume #660000FF is our #AARRGGBB color of choice. Convert #0000FF to RGB which will be rgb(0, 0, 255) and now time to convert #AA.
Following HEX to RGB conversion of #66:
- The first number is 6
- The second number is 6 ÷ 16 = 0.375
- The third number is the (first number + the second number) × 16 = 102
- 102 ÷ 255 = 0.4 for RGBA
If you don’t want to that much math then you can use this conversion tool to convert any TWO hex characters to decimal or any series of Decimal characters. Once you have your decimal number, divide by 255 and you will have your RGBA number.
HSLA
Using the same principles as RGBA and HSL, just add the A to the end of the HSL values. For example
1 2 3
background-color:hsla ([0-360], [x]%, [x]%, [0-1]);
background-color:hsla (133, 85%, 61%, 0.6);
For more information an examples on the IE Hack for RGBA and HSLA, please download css3-colors.zip and open in IE.

css3-colors.zip
Size: 1.71 KB
CSS3 Text Effects
Apr 26th
Today we’re going to talk about text effects and how they make a website become eye candy. If you, for example, visit atomicpages.net under the website management, website setup, and latest blog posts you will notice a slightly darker shadow right below the text. This is a text effect that makes a website less boring to look at.
text-shadow
The text-shadow property isn’t at all new to CSS3. Originally, this property was going to be used with CSS2 which is what all of the Basics of CSS tutorials are based off of.
This property will allow us to create photoshop-like effects without having to use photoshop and saving the text as an image. Here is an example:
Notice how the font has a dark shadow as if the light were coming from about 11 o’clock.
The general syntax for the text-shadow property is as follows:
1 2 3
text-shadow:[x-pos] [y-pos] [blur radius] [color];
text-shadow:2px 2px 2px #000;
In the example above, the shadow will be 2px on the positive x-axis, 2px on the positive y-axis, and have a blur radius of 2px with a color of #000 (black).
Note: the [x-pos] and [y-pos] will take any positive or negative integer.
Which browsers support text-shadow?
Not all browsers today support the text-shadow property, here is a list of browsers that support this property:
| Supported | Not Supported |
|---|---|
|
|
word-wrap
Amazingly enough, the word-wrap was invented by Microsoft and has been implemented into CSS3. This property allows for long words in any given element to be broken and wrapped in a new line. Consider the following:
Without word-wrap
Width word-wrap
The values for word-wrap are normal and break-word.
1 2 3
.wrap {word-wrap:break-word;}
.wrap {word-wrap:normal;} /*this is default*/
In the example above, we used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<!--without word-wrap-->
<div style="border:1px solid #000; padding:3px; width:200px;
word-wrap:normal;">Some text here.
Inthebrokwnwindowsargument,peoplearguethatthereisno
net gain from the creation of jobs.</div>
<!--with word-wrap-->
<div style="border:1px solid #000; padding:3px; width:200px;
word-wrap:break-word;">Some text here.
Inthebrokwnwindowsargument,peoplearguethatthereisno
net gain from the creation of jobs.</div>
Using this method you can break larger words into smaller chunks.
Sources:
1: http://www.w3.org/TR/css3-text/
PHP #5: More Loops
Apr 19th
There are a few other types of loops in addition to while loops that we learned about in Part 4.
Do-While Loop
A do-while loop is very similar to a while loop. The only difference is that the condition is evaluated after each loop iteration, rather than before. The syntax is like this:
1 2 3 | do { // some code here } while (condition) |
Since the condition isn’t evaluated until after the loop body, the loop body is always executed at least once, whether or not the condition is true. Once it does get to the condition, if it is true, it goes back up to the top of the loop body. If it is false, the loop terminates.
1 2 3 4 5 | $i = 11; do { print "The number is $i<br />"; ++$i; } while ($i <= 10) |
The above code prints:
The number is 11<br />
even though the condition is not true. The loop body always executes once.
For Loop
The for loop is a bit more complicated. The syntax looks like this:
1 2 3 | for (initial-expression; condition; loop-end-expression) { //some code goes here } |
The initial-expression is evaluated only once, at the very beginning of the loop. The condition is then evaluated. If the condition is true, the body of the loop executes. Then the loop-end-expression is run. The process then starts over again with evaluating the condition. A for loop is generally used to do simple bounding loops, like this:
1 2 3 | for ($i = 1; $i <= 10; ++$i) { print "The number is $i<br />" } |
As you’ll notice, the same exact thing can be done with a while loop, but many prefer the for loop as it is more compact:
1 2 3 4 5 | $i = 1; while ($i <= 10) { print "The number is $i<br />"; ++$i; } |
More Looping Examples
Let’s try using a for loops to create a multiplication table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | $start = 1; $end = 10; print '<table border="1" cellspacing="0" cellpadding="5">'; for ($y = $start - 1; $y <= $end; ++$y) { print "<tr>"; for ($x = $start - 1; $x <= $end; ++$x) { if ($y == $start - 1) { print "<td><b>$x</b></td>"; } else if ($x == $start - 1) { print "<td><b>$y</b></td>"; } else { print "<td>" print $x*$y; print "</td>"; } } print "</tr>"; } print "</table>"; |
Notice that there is a for loop inside of another for loop. These are called nested for loops. The outer loop loops through each row, and while the inner loop loops through each column. This means on every iteration of the outer loop, the entire inner loop runs. Trace the above code carefully to see how it works. This code will produce the following table:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 2 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
| 3 | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 |
| 4 | 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 40 |
| 5 | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 |
| 6 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 |
| 7 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | 70 |
| 8 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 |
| 9 | 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 |
| 10 | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 |
Test for Understanding
- In the code above how many times does line 6 run?
- In the code above how many times does line 8 run?
Answers:
- Line 6 runs while $y <= 10. $y starts at 0 ($start - 1) and goes up by 1 each time. So it runs while $start = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. That's 11 times!
- Line 8 runs on every iteration of the inner loop. The inner loop runs while $x <= 10. $x starts at 0 and goes up by 1 each time. So the inner loop runs 11 iterations. But the inner loop happens again on every iteration of the outer loop, which also iterates 11 times. 11 x 11 = 121 times.
Website Layout
Apr 16th
What is generally accept as a “good website layout”? A subject of debate, however, a good layout will have a clearly defined header area, sidebar (optional), content area, and footer area. It can look like the following:

The sidebar can be neglected, on the left side, the right side, or on both sides. The fact still remains that there is a clear cut header, sidebar, content area, and footer, however.
Note: the widths and height of these elements will differ of course.
Tables or CSS?
Starting with CSS2.1 in table designed websites started to phase out rapidly. This is because rich content management systems like WordPress, Drupal, and Joomla were being born and refined. The developers of these fine CMSs did not use tables to do their bidding for their rich admin panels and client-side website interactivity. Today, tables are nearly deprecated when it comes to web layout. CSS is used to create tableless layouts. If you don’t know CSS and you want to learn CSS you can click here to learn it!
How to Start?
When I start a website I will work on my external style sheet first and foremost. I will set all of the link colors, define by background, etc… Like the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
html, body {
margin:0;
padding:0;
}
body {
margin:auto;
width:1000px;
background:#3b3b3b url (bg.png) repeat-x top center;
font-family: Tahoma, Arial, sans serif;
font-size:12px;
color:#fff;
}
a, a:visited {
text-decoration:underline;
color:#ff9300;
outline:none;
}
a:hover {
text-decoration:none;
color:#ff9300;
outline:none;
}
img {border:none;}
Once I have my foundation elements all set and ready to go I will create my ID selectors that I will use for the website. If you are unfamiliar with ID selectors or just want a refresher click here for basics or click here for id v. class selectors. The main ID selectors we want are the following:
- header
- sidebar (optional)
- content
- footer
The CSS could look like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#header {
height:75px;
background:#4c4c4c url(images/header-bg.gif) repeat-x;
border:2px solid #fff;
}
#sidebar {
float:left;
width:150px;
background:#DBDBDB url(images/sidebar-bg.gif) repeat-x;
}
#content {
float:right;
width:850px;
}
#footer {
clear:both;
height:75px;
background-color:#4c4c4c;
}
The HTML code to make this happen would be the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div id="header">
<p>Add content here</p>
</div>
<div id="sidebar">
<p>content here</p>
</div>
<div id="content">
<p>content here</p>
</div>
<div id="footer">
<p>content here</p>
</div>
</body>
</html>
We are using a div element that is a block level element so we can negate display:block; for the ID selector. It is common practice that we use div elements to call certain ID selectors since div, by default, does nothing.
Once we have the basic structure of the website we can add our custom styles, class selectors, additional class selectors, nested selectors, pseudo-classes, pseudo-elements, etc… Consider the following for the sidebar:
1 2 3 4 5 6 7 8 9 10 11
#sidebar.container {
background:#DBDBDB url(images/gradient.png) repeat-x top top;
width:125px;
max-height:250px;
overflow:hidden;
}
This creates a class selector called container which can only be used for the ID called sidebar. This container will be called multiple times in the sidebar (hence, the class selector) and will reside inside the sidebar since it’s a child element.
Want to see a working example and then some? Please download web-layout.zip where you can see multiple examples of web layouts each with a combined and their own individual style sheet so you can easily see what the CSS is doing to each layout.

web-layout.zip
Size: 14.43 KB
Creating an Inline Menu
Apr 15th
Creating an inline menu should seem pretty straight forward, right? You think we could just create an unordered list and that would be the end of the story. Unfortunately, if we create an unordered list we’ll quickly see that these lists types merely list item one on top of the other.
- Item 1
- Item 2
- Item 3
This is because the ul and li elements are block-level elements and not inline elements. For example:
1
ul, li {display:block;}
Is a piece of code that is understood by every modern browser that follows the W3C standards.
Our solution is simple then. If we recall that an inline display value will use only what is immediately around the element. Consider the following:
1
p.inline {display:inline;}
1 2 3 4 5 6 7 8 9 10 11 12 13
<p class="inline">Porttitor eros dictumst nunc lundium porta proin
ultrices placerat velit ultrices parturient tincidunt?
In. Nisi tincidunt scelerisque cras velit sed risus
arcu turpis! Mauris et.</p>
<p class="inline">Et ac. Nec tortor quis pulvinar tristique augue
dis sed adipiscing aenean nascetur arcu, magna, elit enim cras?
Amet proin, nascetur amet et eu diam.</p>
Since we overrode the default display:block; for p elements, the output will be the following:
Porttitor eros dictumst nunc lundium porta proin ultrices placerat velit ultrices parturient tincidunt? In. Nisi tincidunt scelerisque cras velit sed risus arcu turpis! Mauris et.
Et ac. Nec tortor quis pulvinar tristique augue dis sed adipiscing aenean nascetur arcu, magna, elit enim cras? Amet proin, nascetur amet et eu diam.
So let us examine the list a little bit more.
1 2 3 4 5 6 7 8 9
<ul>
<li><a href="#">Item 1</a></li>
<li><span>Item 2</span></li>
<li><em>Item 3</em></li>
</ul>
The <ul> in itself if a block-level element; however, the <li> are also block-level elements. All of the elements surrounding the list items are inline elementse.g. the <a> is an inline element.
In our inline menu, we don’t want the menu itself to be an inline element but rather its items to be inline elements. These items are the <li> elements. Consider the following menu with basic CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#menu {
height:50px; /*sets height of menu*/
background-color:#4c4c4c;
}
#menu ul {
list-style:none; /*gets rid of bullets*/
width:800px; /*sets width of menu, not necessary*/
}
#menu li {
display:inline; /*critical to inline menu*/
padding-left:10px; /*sets space between menu items*/
}
Now that we have our CSS the HTML structure will look like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13
<div id="menu">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
See the downloadable file for working example and additional examples.
Pretty boring, huh? All of that CSS for just a few lines of lousy HTML. This is a secret behind the creation of inline CSS menus, just remember that you want the menu items to be a series of inline elements but not the actual menu itself.
From here on out we can style our inline menu to our liking. If you are unfamiliar with CSS then I recommend you visit the CSS Tutorials and learn CSS!
From there we can style our inline menu to our liking. We can create simple effects that will make the men stand out. Consider the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#menu {
height:50px;
font-size:1.2em;
background-color:#4c4c4c;
border:2px solid #fff;
color:#fff;
}
#menu ul {list-style:none;}
#menu li {
display:inline;
padding-left:10px;
}
#menu a:link, #menu a:visited {
text-decoration:none;
color:#fff;
padding:10px;
}
#menu a:hover {
background-color:#3b3b3b;
text-decoration:none;
color:#DBDBDB;
padding:10px;
}
This will give us a nice looking menu that will have a roll over effect on the links within the menu. From there, we can add custom images, jQuery drop down menus, and many other effects using client-side code.
For examples and editable code, please download the file!

Inline Menu
Size: 11.48 KB
