How do I add a  class based on the visibility of a block? Is there a way to add a CSS class to the &lt;body&gt; tag based on the visibility of a  particular block on a page?

We have certain blocks that are visible on specific pages. It would be nice to be able to change CSS styles for related content depending on whether they are present.

How can I achieve this?
You have to do it via the template_preprocess_html function.

Add this to your theme's template.php file:

function MYTHEME_preprocess_html(&amp;$variables) {
  // dpm($variables);
  $node = menu_get_object();
  if (isset($node) &amp;&amp; isset($variables['page']['sidebar_first']['system_navigation'])) {
     $variables['classes_array'][] = 'system-navigation';
  }
}


In the above code I am looking for the system navigation block in the sidebar first region and if it is there I am adding system-navigation class to the classes array. I am also checking if I am on a node page via the call to menu_get_object()

You will have to adapt the code to your use case. 

Install the devel module, then uncomment the dpm statement. Then look under the page variable at other variables without a # in front of them.