diff --git a/core/lib/Drupal/Core/Asset/CssCollectionGrouper.php b/core/lib/Drupal/Core/Asset/CssCollectionGrouper.php
index 6ed0662..734feb8 100644
--- a/core/lib/Drupal/Core/Asset/CssCollectionGrouper.php
+++ b/core/lib/Drupal/Core/Asset/CssCollectionGrouper.php
@@ -57,7 +57,9 @@ public function group(array $css_assets) {
           // Group file items if their 'preprocess' flag is TRUE.
           // Help ensure maximum reuse of aggregate files by only grouping
           // together items that share the same 'group' value.
-          $group_keys = $item['preprocess'] ? array($item['type'], $item['group'], $item['media'], $item['browsers']) : FALSE;
+          // The CSS optimizer inlines 'media' statements for everything except
+          // 'print', so only vary groups based on that.
+          $group_keys = $item['preprocess'] ? array($item['type'], $item['group'], $item['media'] === 'print', $item['browsers']) : FALSE;
           break;
 
         case 'inline':
@@ -80,6 +82,9 @@ public function group(array $css_assets) {
         // properties are unique to the item and should not be carried over to
         // the group.
         $groups[$i] = $item;
+        if ($item['media'] !== 'print') {
+          $groups[$i]['media'] = 'all';
+        }
         unset($groups[$i]['data'], $groups[$i]['weight'], $groups[$i]['basename']);
         $groups[$i]['items'] = array();
         $current_group_keys = $group_keys ? $group_keys : NULL;
diff --git a/core/lib/Drupal/Core/Asset/CssOptimizer.php b/core/lib/Drupal/Core/Asset/CssOptimizer.php
index 704ee57..67184e9 100644
--- a/core/lib/Drupal/Core/Asset/CssOptimizer.php
+++ b/core/lib/Drupal/Core/Asset/CssOptimizer.php
@@ -62,7 +62,9 @@ public function clean($contents) {
    */
   protected function processFile($css_asset) {
     $contents = $this->loadFile($css_asset['data'], TRUE);
-
+    if ($css_asset['media'] !== 'print' && $css_asset['media'] !== 'all') {
+      $contents = '@media ' . $css_asset['media'] . '{' . $contents . '}' . "\n";
+    }
     $contents = $this->clean($contents);
 
     // Get the parent directory of this file, relative to the Drupal root.
diff --git a/core/tests/Drupal/Tests/Core/Asset/CssCollectionGrouperUnitTest.php b/core/tests/Drupal/Tests/Core/Asset/CssCollectionGrouperUnitTest.php
index 0a35eec..7759a73 100644
--- a/core/tests/Drupal/Tests/Core/Asset/CssCollectionGrouperUnitTest.php
+++ b/core/tests/Drupal/Tests/Core/Asset/CssCollectionGrouperUnitTest.php
@@ -59,7 +59,7 @@ function testGrouper() {
         'group' => -100,
         'type' => 'file',
         'weight' => 0.004,
-        'media' => 'all',
+        'media' => 'screen',
         'preprocess' => TRUE,
         'data' => 'core/misc/ui/themes/base/jquery.ui.core.css',
         'browsers' => array('IE' => TRUE, '!IE' => TRUE),
diff --git a/core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php b/core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php
index d4cad06..07031bf 100644
--- a/core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php
+++ b/core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php
@@ -101,6 +101,19 @@ function providerTestOptimize() {
         ),
         file_get_contents($absolute_path . 'css_input_without_import.css.optimized.css'),
       ),
+      array(
+        array(
+          'group' => -100,
+          'type' => 'file',
+          'weight' => 0.012,
+          'media' => 'screen',
+          'preprocess' => TRUE,
+          'data' => $path . 'css_input_simple.css',
+          'browsers' => array('IE' => TRUE, '!IE' => TRUE),
+          'basename' => 'css_input_simple.css',
+        ),
+        file_get_contents($absolute_path . 'css_input_simple_with_media.css.optimized.css'),
+      ),
       // File. Tests:
       // - Proper URLs in imported files. (https://www.drupal.org/node/265719)
       // - A background image with relative paths, which must be rewritten.
diff --git a/core/tests/Drupal/Tests/Core/Asset/css_test_files/css_input_simple.css b/core/tests/Drupal/Tests/Core/Asset/css_test_files/css_input_simple.css
new file mode 100644
index 0000000..54a2a13
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/css_test_files/css_input_simple.css
@@ -0,0 +1 @@
+body{margin:0;padding:0;background:#edf5fa;font:76%/170% Verdana,sans-serif;color:#494949;}
diff --git a/core/tests/Drupal/Tests/Core/Asset/css_test_files/css_input_simple_with_media.css.optimized.css b/core/tests/Drupal/Tests/Core/Asset/css_test_files/css_input_simple_with_media.css.optimized.css
new file mode 100644
index 0000000..cdf8ee8
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/css_test_files/css_input_simple_with_media.css.optimized.css
@@ -0,0 +1,2 @@
+@media screen{body{margin:0;padding:0;background:#edf5fa;font:76%/170% Verdana,sans-serif;color:#494949;}
+}
