Customising your AMP sub-theme
Now that you’ve set up AMP and created a sub-theme, you’ll need to create CSS to style your pages. In this guide, we'll cover the following topics:
AMP (Accelerated Mobile Pages) are a stripped down form of HTML, designed to be lightweight and serve mobile content much faster than regular pages. Naturally, this creates limitations to ensure fast delivery of content.
Custom JS and additional libraries can add a lot of weight to page load times, so AMP pages do not support these.
Your AMP sub-theme CSS styles (which are actually held in a Drupal
html.twig file) cannot exceed 50kb. This roughly equates to 3000 lines of uncompressed CSS. See recommended approach to CSS for more info.
||This is a necessary requirement to enable AMP to enforce its element sizing rules.|
||External stylesheets cannot be referenced, with the exception of custom fonts|
||Styles may not reference CSS selectors for -amp- classes and i-amp tags. These are reserved for internal use by the AMP runtime.|
|Only GPU-accelerated properties (currently opacity and transform) can be transitioned/animated on AMP pages.|
||Overflow properties can’t be styled as auto or scroll - as such no user-defined elements in AMP pages can have a scrollbar.|
Any imported fonts you’ve specified in
Website settings >
Font libraries will be available to your AMP sub-theme to use in CSS.
The following origins are whitelisted by AMP:
Google Fonts: https://fonts.googleapis.com
Uploaded font packages will not work unless you create
@font-face declarations for these fonts in your sub-theme CSS file. These can be copied from the
This also applies to icon fonts specified in
Website settings >
Make sure that any custom font you use is as lightweight as possible, as this will impact page load time.
DX8 provides significant time and efficiency gains when theming regular Drupal pages, because the CSS is generated through our innovative style builder system. This efficiency is lost with theming AMP pages because CSS has to be generated manually.
|Field Formatter Class||Allows classes to be added to the outer wrapper of any Drupal field display, which provides more control for theming.|
|Title Field for Manage Display||Creates a
|Breadcrumb Extra Field||Creates a
|Context (and Context UI submodule)||In combination with
Initially your AMP pages will look very different to regular pages as no styles will be applied - these have to be generated manually.
We recommend you identify a content type that is most likely to benefit from AMP optimisation (eg. blog) as a starting point. You can then expand to other content types as necessary, providing you do not exceed the 50kb CSS limit.
Once you’ve identified a content type to work with, you may need to reorganise your block and content type field placement to provide the best user experience. DX8 templated content (master templates, content templates etc) are not supported with AMP themes.
Select the correct tab before making changes to block or content type field layout, so that other displays are unaffected. For blocks this is your sub-theme, for content types it will be
Make sure any fields on your content type are using the correct formatter. For example,
Image needs to be changed to
See the useful modules section for some community modules that will help with this process.
To keep your theme CSS as light as possible, we recommend using a CSS preprocessor such as SCSS. By setting the SCSS output style to
compressed, it will minify the CSS into a single line and provide room for extra CSS.
Keep CSS selectors as short as possible - single-level element (e.g.
H1) or class (e.g.
.myclass) selectors are ideal.
Never use ID selectors (e.g.
#myid) as these are much harder to override.
If multi-level selectors are necessary, limit to no more than three levels (e.g.
ul li a) - if possible add classes to these elements to give more control.
Don’t use an autoprefixer - whilst this is normally recommended for legacy browser support, AMP is a modern development framework that targets mobile devices that don’t require autoprefixing. It will also reduce the amount of CSS generated.
Keep it simple - your AMP pages won’t always look the same as your regular pages, given that page load speed is the priority.
Whilst the CSS you define for your AMP sub-theme will differ from project to project, we recommend using the following outline.
||Always put your font declarations first, so that you can specify
|DX8 grid styles||As no external styles are loaded, you will need to duplicate DX8’s grid CSS to implement a responsive layout. These include
|DX8 base styles||Some styles that you’ve defined through DX8 base styles may need duplicating. For example
|Drupal core styles||you may need to duplicate some of Drupal's core CSS. Examples include
|Drupal element styles||CSS that targets Drupal elements, blocks or fields. This would include styles for any custom classes you’ve added to content type fields.|
|DX8 custom styles||Some styles that you’ve defined through DX8 custom styles may need duplicating.|
|DX8 inline element styles||Styles that you’ve applied directly to DX8 elements through the