Adding CSS to form or page with attachments

Here is example of #attached attribute

<?php
function foo_block_view($delta = '') {
  if ($delta == 'foo_delta') {
    $block = array(
      'subject' => t('Foo title'),
      'content' => array(
        '#markup' => '<p>' . t('Lorem ipsum text.') . '</p>',
        '#attached' => array(
           'css' => array('path/to/my.css'),
        ),
      ),
    );
    return $block;
  }
}
?>

Now content element coming as a render array having two elements #markup and#attached both will always be included. We can add internal, external and inline css using this attribute.

We can also add css to page in similar way.

<?php
 $content = array();
 $content['#attached']['css'] = array(
    $path . 'my.css' => array(
      'group' => CSS_AGGREGATE_THEME,
      'weight' => 999
    ),
  );
  return $content;
?>

Even in Drupal 8, adding css to the page should now be done using the #attached property of a rendered array.

Sample blackbox block

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas non porta odio, id mattis erat.

Nam sed tincidunt eros. Donec a venenatis odio, vitae commodo sem. Vivamus ullamcorper pellentesque erat, in adipiscing tortor scelerisque sit amet. Nulla pulvinar urna magna. Vivamus a ipsum lorem. Etiam adipiscing pulvinar lorem.