Index: wp-includes/formatting.php
===================================================================
--- wp-includes/formatting.php	(revision 10885)
+++ wp-includes/formatting.php	(working copy)
@@ -76,6 +76,39 @@
 }
 
 /**
+ * clean invalid chars from css class names
+ * 
+ * invalid chars will be replaced with '_', if the class
+ * should not be treated as a suffix for an existing class,
+ * then invalid classes will be prefixed with 'a_' to 
+ * become valid again. 
+ * 
+ * @since 2.7.2
+ * 
+ * @param  array|string $class  array or string with class(es)
+ * @param  bool         $suffix optional wether or not class(es) 
+ * 								are treated as being a suffix 
+ * 								(defaults to true)
+ * @return string       classname(es) cleaned  
+ */
+function clean_css_classnames($class, $suffix = true)
+{
+	if (is_array($class)) {
+		$return = array();
+		foreach($class as $single)
+			$return[] = clean_css_classnames($single, $suffix);
+		return $return;	
+	}	
+	
+	$class = preg_replace('|[^_a-z0-9-]|i', '_', $class);
+	
+	if (false == $suffix && 0 == preg_match('|^-?[_a-z]+[_a-z0-9-]*$|i', $class))
+		$class = 'a_' . $class;
+			
+	return $class;
+}
+
+/**
  * Accepts matches array from preg_replace_callback in wpautop() or a string.
  *
  * Ensures that the contents of a <<pre>>...<</pre>> HTML block are not
Index: wp-includes/post-template.php
===================================================================
--- wp-includes/post-template.php	(revision 10885)
+++ wp-includes/post-template.php	(working copy)
@@ -320,13 +320,15 @@
 		if ( empty($cat->slug ) )
 			continue;
 		$classes[] = 'category-' . $cat->slug;
+		$classes[] = 'categoryid-' . $cat->term_id;
 	}
 
 	// Tags
 	foreach ( (array) get_the_tags($post->ID) as $tag ) {
 		if ( empty($tag->slug ) )
 			continue;
-		$classes[] = 'tag-' . $tag->slug;
+		$classes[] = 'tag-' . $tag->slug;		
+		$classes[] = 'tagid-' . $tag->term_id;
 	}
 
 	if ( !empty($class) ) {
@@ -334,6 +336,8 @@
 			$class = preg_split('#\s+#', $class);
 		$classes = array_merge($classes, $class);
 	}
+	
+	$classes = clean_css_classnames($classes, false);
 
 	return apply_filters('post_class', $classes, $class, $post_id);
 }
