HTML & CSS: Lesson 8 – Debugging CSS

If you have spent any time actually trying to build web pages with CSS and HTML then you understand how critical being able to debug your code is.  CSS is an extremely powerful technology that allows you to build engaging web pages but it is also a temperamental  pain in the neck.  Sometimes getting the CSS to do what you want consistently on all of the 3 major browsers will make you want to slap the electricity out of your computer.

So lets look at some tools for debugging your HTML and CSS.  Google Chrome will soon become your new favorite browser.  The developer tool bar is an powerful utility that allows you to be able to inspect the HTML, CSS and JavaScript of any web page.  If you have not already installed Google Chrome, go back to Lesson 1 and follow the instructions for installing it.

The Developer Tool Bar

Fire up Google Chrome.  In the top right hand corner of the screen you should see a wrench.  Click on it and go to the wrench.  Then go to the tools menu and then click on the Developer Tools.

You should see a new panel appear in the browser window.  This window will become your new best friend.

First thing you should notice is the tabs across the top of the developer tools panel.  Elements is the only one we care about right now.  Click on it.  (The other tabs are totally awesome and are invaluable when developing web apps.  But just not for this post.)  The elements tab gives us all of the information about the HTML and CSS.

It gets better.  As you drill into the HTML you will notice that the web page will highlight that area in the browser.  Conversely, if you right click on anything on the page and select “Inspect Element” from the pop menu.  It will find that element in the HTML and highlight it for you.

And if that wasn’t cool enough, the CSS for that element will be displayed on the panel on the right.  Some tag acting weird and not doing what you thought it should be doing?  This panel will give you some clues as to why.

This also gives you a great way to easily study someone else’s work.  Sometimes I‘ll be cruzing the web and see some slick styles.  Right click and inspect element – review the CSS and now I have a new tool in my tool box.

One more nugget – you can edit the page styles from the CSS panel.  Click on a style and you can edit it.  Not sure you like the font color.  No problem.  Find the style for that tag and change it in the editor.

This is a great way to prototype web page design.   Just remember what you changed because the Developer tools will not modify the CSS for you.

The Chrome Developer Tools are fantastic.  And they have many more great features that we will discuss in future blog posts.

HTML & CSS: Lesson 7 – CSS Super Powers

In the last lesson we glossed over a lot of concepts in CSS.  So this lesson we are going to demystify CSS.  In the last lesson we discussed what CSS was and how it interacts with the HTML.  We also saw how it is used to style our page to make it look cool!

The main points that you need to know (see the last lesson if any of this does not sound familiar!) are:

  1. You must include a <link> tag in the HTML so that you can link your HTML document to the style sheet if you choose to use an external style sheet.
  2. CSS lives in .CSS files (aka style sheets)
  3. CSS changes the size, color, position, appearance of HTML tags.
  4. CSS selects the HTML tags by using classes, ids and tag names
  5. An HTML tag may have 1 id and / or many classes

CSS Selectors

Lets review our menu CSS to really understand how CSS is affecting how the HTML tags are rendered by the browser.  Below is the CSS that takes our ugly div and list and turns them into a beautiful menu bar and menu.

#menuBar
{
height: 84px;
width: 100%;
background: url(images/menu-bg.png) repeat-x;
}

#menu
{
list-style-type: none;
text-align: center;
display: inline;
}

#menu li
{
display: inline;
padding: .2em 1em;
background-color: #036;
margin: 15px;
}

#menu li a{
text-decoration: none;
color: #fff;
}

#menu li:hover
{
color: #fff;
background-color: #369;
}

A CSS selector is simply a mechanism to define which HTML tags will be affected by the styles.  Lets look at the #menuBar CSS.  This select says for every tag that has an id=”menuBar” set the height, width and background.

#menuBar
{
height: 84px;
width: 100%;
background: url(images/menu-bg.png) repeat-x;
}

In CSS the # symbol means we are looking at HTML tags that have id=”SomeValue”.  Typically, we are working with a single tag on the page, but this is not a rule.  And HTML document may have multiple tags with the same id.  But this is considered bad practice and strongly discouraged.  This is where the spandex rule kicks in – “Just because you can doesn’t mean you should.”

We can also select HTML tags by class.  For example, look at the following HTML.

<div class=”article”> I am 16px font </div>

<div class=”article red”> I am 16px font and red </div>

<div class=”article ads”> I am 16px font </div>

Suppose we wanted all articles to have a 16 point font.  Then we could use the . (dot) selector to apply the font-size to all tags that have an “article” in the class attribute.

.article {
font-size: 16px;
}

Now suppose we want all tags that have a red class to have a red font color.

.red{
color: red;
}

We can reuse and combine CSS styles to create complex and reusable styles.  This is important.  Don’t miss this.  You can COMBINE styles to create more complex and REUSABLE styles.  Take the time to really digest what a powerful feature this is.  You do not (nor should not) have an individual style for every element on a page.  So if you notice that you are constantly repeating a particular decorator (font-size, color, background, width, height) that you should probably make it an individual style and apply the style to the classes that you need it.

Why do this you may ask? Sounds like a lot of work?  Just a bunch of extra thinking?  Trust me.  It is way better to change the color, width, font, or whatever in one place than to have to hunt the change down in dozens of places and hope that you got them all.  No fun.  Beside other web developers will ridicule your code and talk about your code behind your back.

So we have discussed selecting tags by id and by class.  But you can also select them by name.  Let suppose I want all <h3> tags to be red.

h3{
color: red;
}

Yep.  That simple.

So far you have seen that CSS is extremely useful – but wait there is more!  You can combine these selectors to identify specific tags.  Lets suppose I want all <a> tags in the header div to be red.

<div id=”header”>
<a href=”#”>I am red</a>
<a href=”#”>Me too</a>
</div>
<div id=”mainContent”>
<a href=”#” class=”big”>I am NOT red</a>
<a href=”#” class=”little”>So not red!</a>
</div>

#header a{
color: red;
}

Or if I wanted all tags with a “big” class in the mainContent div to be 24px.

#mainContent .big{
font-size: 24px;
}

Ok so what if I wanted all of the odd links  to be red and all the even links to be blue?  Can CSS do that?  Huh?

#header a:nth-child(odd){
color: red;
}

#header a:nth-child(even){
color: blue;
}

These types of selectors are called pseudo-classes and there are many types.  Some of the more common pseudo-classes are

:hover – the style is activated when the mouse is over the tag.

:focus – the style is activated when the tag has focus

CSS has another super power called inheritance.  Child tags will inherit the decorators of their parent tags.  For example,  lets say I wanted all text on my web page to default to 16px.  I would set the font-size of the <body> tag and then all my children tags inherit that style.  Children tags are any tags that live inside of another tag.  You may have noticed that HTML tags can contain other HTML tags. If a tag contains another tag then that tag then the top level tag is said to be the parent and all of the inner tags are the children.  This a very powerful feature.

Children can override their parents.  So if a parent has a font-size: 16px but the child element needs the font size to be 24px then the child’s style will win.

CSS is very powerful and has many features.  I could not possible discuss all of the decorators or pseudo-classes in this blog post.  But in my next post I will show you how to use the Google Chrome developer tools to steal other CSS super hero’s powers!