Figuring out how the CSS of a website works using Firebug – Screencast

The basis of using Cascading Style Sheets (CSS) to apply formatting to an HTML document is deceptively easy to understand, but hard to master. You define selectors that designate HTML elements, which contains formatting rules that apply to those elements and their children. If more than one rule applies to an element, they are applied using a priority order, from the more general to the more specific: that’s where the “cascading” part comes in.

It’s almost impossible to predict how a website will look by looking at the code. If you have to modify the CSS of an application you don’t know “just” to change a color or a font size using only your text editor, you’re out of luck. Figuring out the priority of each selector is complicated if you have many stylesheets that applies to a page, and you can easily miss a more general selector. You can’t just look at an element on the page and immediately deduce which CSS rules are affecting it, especially if there are many contradictory rules.

That’s where tools such as Firebug for Firefox or the Developer Tools in Chrome comes in. Among many other great features, they allow you to see which rules are affecting a particular element on the page, and to modify them on the fly.

Of course, it won’t modify your original CSS since it only changes the local copy in your browser, but you can quickly test what’s the impact on your page and then make the modifications. You can also use those tools to explore your favorite websites and see how they used CSS to format elements: it can be a great learning tool if you’re looking to grow your CSS knowledge.

Here is a screencast on how to use Firebug to figure out where the colors are defined on the Twitter website:

Adding a favicon to a site

I forgot to make a favicon when I created the theme for this site, so let’s fix this. A favicon is the small icon that appears beside the title in the tab, and that is shown when the user bookmarks your site. According to the W3C, the favicon should be a .PNG, .GIF or. ICO image that’s 32×32 pixels or 16×16 pixels. So I created a .PNG image that is 32×32 pixels so it will look nice scaled down, and is a nice starting size if some browsers display it bigger. I also added the following tag to the head of each page :

<link rel="icon" type="image/png" href="" />

Unfortunately, Internet Explorer only supports .ICO for favicons, even Internet Explorer 10, so I had to also add the following in the head:

<link rel="shortcut icon" type="image/x-icon" href="" />

Depending on the browser and the order of the declarations, the .PNG or the .ICO will be used, but since it’s the same image it’s good enough for me and respects the standard. Everything now works in Internet Explorer, Chrome and Firefox on the desktop, but it will not work with mobile devices. When a the web site is added to the home screen in iOS or Android, the favicon will not be shown. iOS looks for a link tag in the head of the page with the rel=apple-touch-icon, which adds the 3D effect in iOS 6 or earlier (iOS 7 icons stays flat), or rel=apple-touch-icon-precomposed that displays the image without embellishments. Android phones will also recognise apple-touch-icon and apple-touch-icon-precomposed for the home screen images, so this is enough for both platforms. If you want to support older phones, Android 2.1 will only recognize apple-touch-icon-precomposed. I wanted to keep the same flat look as my theme, so I used the –precomposed attribute and the same icon as earlier :

<link rel="apple-touch-icon-precomposed" href="" />

Using the same icon will make it stretched on every device, since the default size is 60×60. You can add icons specific for each platform with apple-touch-icon if you need a bit more control :

<link rel="apple-touch-icon-precomposed" href="" />
<link rel="apple-touch-icon-precomposed" sizes="76x76" href="" />
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="" />
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="" />