26-Jul-2017

Troubleshooting CSS

Learning how to fix CSS problems in just a few steps, which can be repeated many times over.

From a course on Sitepoint css-troubleshooting-in-six-easy-steps-2869.

Using a Browser

Many browsers now have an inspect element feature, either right click or press F12.

Scroll Into View

Right clicking on an element and selecting "Scroll into view" will show the element on the web page without having to scroll to it yourself.

Editing HTML

You can edit HTML in the Elements window. Changing the opening tag will also change the closing one. For example changing h1 to h2 will also change the closing tag to </h2>.

CSS Syntax

Note that CSS rules with the incorrect syntax are not applied. The Developer Tools window shows a warning if the rules shown are wrong. Watch placement of commas and ensure there are no spaces within hyphenated properties.

Targetting CSS

CSS must be targetted correctly or the desired rule won't work. Try putting a red outline (not a border) with CSS on the element you are trying to style to confirm the selector is correct.

Selector Weight

SelectorIDsClassesTypesWeightTotal
h2 {}0010-0-11
#nav h2 {}1011-0-15
.intro h2 {}0110-1-13

The #nav h2 {} rule would be used as it has the most weight. The values are calculated as if they are a binary number.

If two rules have the same "weight" then the last one will be implemented.

Isolating the Issue

Reduce your HTML/CSS down to a small test case by:

  • Comment out chunks of HTML/CSS
  • Create new test files
  • Build a simple demo using online tools eg, JS Fiddle, CSS Desk or Pastebin.

Dealing with Old Browsers (eg. IE 6, 7 and 8)

Test your page using the latest IE browser by pressing F12 to bring up the Developer Tools panel and then change the User Agent String to the required browser.

This doesn't replicate all of IE's bugs with the older versions.

It may be worth installing old browsers on your computer, or set up virtual machine with Virtual Box and set up as many old browsers as you like.

Old Browser CSS

Old browsers can have their own CSS by using:

<!--[if lt IE 7]><html class="lt-ie9 lt-ie8 lt-ie7" lang="en"><![endif]-->
<!--[if IE 7]><html class="lt-ie9 lt-ie8" lang="en"><![endif]-->
<!--[if IE 8]><html class="lt-ie9" lang="en"><![endif]-->
<!--[if gt IE 8]><!--> <html lang="en"> <!--<![endif]-->

Target old browsers by using for example:

.lt-ie9 li.menu {
     

[ie v7 rules here]

}

Online Resources




CSS cascade and selector weight


Online code snippet sites


Further reading

Leave a Reply

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram