Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary spacing-related media queries #2737

Merged
merged 2 commits into from
Aug 1, 2022

Conversation

36degrees
Copy link
Contributor

When the $govuk-spacing-responsive-scale map is processed by the _govuk-responsive-spacing mixin, a media query is outputted in the CSS for each breakpoint defined in the map.

(The ‘null’ breakpoint is a special case and is outputted outside of any media query as the ‘baseline’ spacing.)

However, at the lower end of the spacing scale (0-3) there is no difference between the ‘baseline’ spacing and the spacing on tablet and above.

Spacing unit Small screens Large screens
0 0 0
1 5px 5px
2 10px 10px
3 15px 15px
4 15px 20px
5 15px 25px
6 20px 30px
7 25px 40px
8 30px 50px
9 40px 60px

With the existing settings, this means that we end up outputting a ‘redundant’ media query targeting the breakpoint which defines spacing that is exactly the same as the baseline spacing. For example:

 .govuk-\!-margin-1 {
     margin: 5px !important
 }

@media (min-width:40.0625em) {
    .govuk-\!-margin-1 {
        margin: 5px !important
    }
}

We can avoid this by simply omitting the tablet breakpoint from the spacing scale where it does not differ from the baseline, and relying on the baseline spacing being applied everywhere:

 .govuk-\!-margin-1 {
     margin: 5px !important
 }

As well as affecting the override classes, this also affects any CSS outputted by the govuk-responsive-margin or govuk-responsive-margin helpers where spacing units 0-3 are passed.

In the rest of the Frontend codebase this includes the:

  • Accordion
  • Header
  • Tabs
diff --git a/dist-main/govuk-frontend-4.2.0.min.css b/dist/govuk-frontend-4.2.0.min.css
index 0e8c0776..97225cfb 100644
--- a/dist-main/govuk-frontend-4.2.0.min.css
+++ b/dist/govuk-frontend-4.2.0.min.css
@@ -1204,8 +1204,7 @@

 @media (min-width:40.0625em) {
     .js-enabled .govuk-accordion__section-content {
-        padding-bottom: 50px;
-        padding-top: 15px
+        padding-bottom: 50px
     }
 }

@@ -4051,12 +4050,6 @@ only screen and (min-resolution:192dpi) {
     padding-right: 50px
 }

-@media (min-width:40.0625em) {
-    .govuk-header__logo {
-        margin-bottom: 10px
-    }
-}
-
 @media (min-width:48.0625em) {
     .govuk-header__logo {
         width: 33.33%;
@@ -5106,7 +5099,6 @@ only screen and (min-resolution:192dpi) {

 @media (min-width:40.0625em) {
     .govuk-tabs {
-        margin-top: 5px;
         margin-bottom: 30px
     }
 }
@@ -5349,15 +5341,7 @@ only screen and (min-resolution:192dpi) {
         border: 1px solid #b1b4b6;
         border-top: 0
     }
-}
-
-@media (min-width:40.0625em) and (min-width:40.0625em) {
-    .js-enabled .govuk-tabs__panel {
-        margin-bottom: 0
-    }
-}

-@media (min-width:40.0625em) {
     .js-enabled .govuk-tabs__panel>:last-child {
         margin-bottom: 0
     }
@@ -6191,202 +6175,82 @@ screen and (forced-colors:active) {
     margin: 0 !important
 }

(Changes to spacing override classes omitted for brevity)

In testing this saves 3,426 bytes (167 gzipped) – 106,896 bytes (13,025 gzipped) compared to 110,322 bytes (13,192 gzipped) for the equivalent ‘dist’ CSS file without these changes applied.

When the `$govuk-spacing-responsive-scale` map is processed by the `_govuk-responsive-spacing` mixin, a media query is outputted in the CSS for each breakpoint defined in the map.

(The ‘null’ breakpoint is a special case and is outputted outside of any media query as the ‘baseline’ spacing.)

However, at the lower end of the spacing scale (0-3) there is no difference between the ‘baseline’ spacing and the spacing on tablet and above.

With the existing settings, this means that we end up outputting a ‘redundant’ media query targeting the breakpoint which defines spacing that is exactly the same as the baseline spacing. For example:

```css
 .govuk-\!-margin-1 {
     margin: 5px !important
 }

@media (min-width:40.0625em) {
    .govuk-\!-margin-1 {
        margin: 5px !important
    }
}
```

We can avoid this by simply omitting the tablet breakpoint from the spacing scale where it does not differ from the baseline, and relying on the baseline spacing being applied everywhere:

```css
 .govuk-\!-margin-1 {
     margin: 5px !important
 }
```

As well as affecting the override classes, this also affects any CSS outputted by the `govuk-responsive-margin` or `govuk-responsive-margin` helpers where spacing units 0-3 are passed.

In the rest of the Frontend codebase this includes the:

- Accordion
- Header
- Tabs

```diff
diff --git a/dist-main/govuk-frontend-4.2.0.min.css b/dist/govuk-frontend-4.2.0.min.css
index 0e8c0776..97225cfb 100644
--- a/dist-main/govuk-frontend-4.2.0.min.css
+++ b/dist/govuk-frontend-4.2.0.min.css
@@ -1204,8 +1204,7 @@

 @media (min-width:40.0625em) {
     .js-enabled .govuk-accordion__section-content {
-        padding-bottom: 50px;
-        padding-top: 15px
+        padding-bottom: 50px
     }
 }

@@ -4051,12 +4050,6 @@ only screen and (min-resolution:192dpi) {
     padding-right: 50px
 }

-@media (min-width:40.0625em) {
-    .govuk-header__logo {
-        margin-bottom: 10px
-    }
-}
-
 @media (min-width:48.0625em) {
     .govuk-header__logo {
         width: 33.33%;
@@ -5106,7 +5099,6 @@ only screen and (min-resolution:192dpi) {

 @media (min-width:40.0625em) {
     .govuk-tabs {
-        margin-top: 5px;
         margin-bottom: 30px
     }
 }
@@ -5349,15 +5341,7 @@ only screen and (min-resolution:192dpi) {
         border: 1px solid #b1b4b6;
         border-top: 0
     }
-}
-
-@media (min-width:40.0625em) and (min-width:40.0625em) {
-    .js-enabled .govuk-tabs__panel {
-        margin-bottom: 0
-    }
-}

-@media (min-width:40.0625em) {
     .js-enabled .govuk-tabs__panel>:last-child {
         margin-bottom: 0
     }
@@ -6191,202 +6175,82 @@ screen and (forced-colors:active) {
     margin: 0 !important
 }
 ```

(Changes to spacing override classes omitted for brevity)

In testing this saves 3,426 bytes (167 gzipped) – 106,896 bytes (13,025 gzipped) compared to 110,322 bytes (13,192 gzipped) for the equivalent ‘dist’ CSS file without these changes applied.
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2737 July 28, 2022 16:43 Inactive
36degrees added a commit to alphagov/govuk_publishing_components that referenced this pull request Jul 29, 2022
This is to test the changes in alphagov/govuk-frontend#2737 against the publishing components to see if any visual regressions are introduced.
36degrees added a commit to alphagov/govuk_publishing_components that referenced this pull request Jul 29, 2022
This is to test the changes in alphagov/govuk-frontend#2737 against the publishing components to see if any visual regressions are introduced.
@36degrees
Copy link
Contributor Author

I updated GOV.UK Publishing Components to use a pre-release with just these changes rebased onto the v4.2.0 tag (39abfcc) and there were no changes according to their visual regression tests either.

@36degrees 36degrees added this to the v4.3.0 milestone Aug 1, 2022
Copy link
Member

@querkmachine querkmachine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@36degrees 36degrees changed the title Avoid unneccesary spacing-related media queries Avoid unnecessary spacing-related media queries Aug 1, 2022
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2737 August 1, 2022 12:15 Inactive
@36degrees 36degrees merged commit f465da8 into main Aug 1, 2022
@36degrees 36degrees deleted the avoid-unnecessary-media-queries branch August 1, 2022 12:20
@querkmachine querkmachine mentioned this pull request Aug 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

3 participants