Introduction:
The Custom CSS feature enables you to add your own personal touch to your H5P content. This way you may for instance replace default H5P colors and fonts with your organization's own brand, giving you even more control of the look and feel of your content.
How the Custom CSS feature works
The Custom CSS feature allows administrators to add CSS overrides. Administrators may add overrides in a preview mode first - where the changes are only visible to the administrators. When you're satisfied with testing the styles on your content, you may apply the previewed CSS overrides for the entire organization's content.
The CSS overrides when applied to the entire organization affect all H5P content for all users.
Activating the custom CSS feature
By default the custom CSS feature is disabled and customers are to reach out to their customer representative to get access to this feature.
Warnings
The reason the custom CSS feature is disabled by default is that when used incorrectly the feature may damage the look, functionality, and accessibility of the content. Even if the content seems to be working after adding the CSS a future update of H5P that isn't compatible with the CSS changes may damage the look or behavior of the content. Access to the CSS feature also introduces security problems. It opens up some attack vectors for someone with access to a customer's administration accounts.
Recommended workflow
H5P uses massive amounts of CSS. Reading up on the CSS and DOM structure line by line is not practical. Instead, we recommend the following workflow when working on the H5P.com CSS:
- Use your browser's development tool and create CSS overrides in your browser.
- Copy the CSS overrides from your browser into a file or directly into the CSS overrides field in the H5P.com admin interface. (The interface is under Manage Organization > Settings > Custom CSS)
- Prefix all your CSS selectors with .h5p-content
- Test all types of content that may be affected by the overrides and make sure they work as expected in all browsers. Do note that if you make overrides to Multiple Choice, for instance, Multiple Choice is used within Course Presentation, Question Set, Interactive Video, and Column. You should ideally check that your overrides work as expected within all these different contexts.
- Apply the changes to your organization using the H5P.com admin interface when you feel certain that your changes have the desired effect.
What to change and what not to change
All CSS overrides do come with a risk. The H5P.com content types are updated regularly and there is always a chance that an update might make your CSS overrides stop working. Colors are the safest thing to change. The riskiest things to change are things that affect the positioning or size of objects. Such changes might both ruin the content when it is updated and ruin the content if it is exported from H5P.com and imported into a site that does not have the same CSS overrides.
Overriding fonts
In CSS, you have the possibility to load and use other fonts than the default provided by H5P. Because of the strict CSP (Content Security Policy) on h5p.com, the only option is to use fonts served by Google Fonts (https://fonts.google.com/).
CSS specificity
It's important to have a good understanding of how CSS works before creating your own customizations. There are many good resources online, and we're not gonna duplicate that here. But we're gonna look a bit into how CSS specificity works. I.e: how does the browser select which properties to apply when there are several selectors for a DOM element with the same property. We'll start off with an example:
button {
/* Will be applied to all buttons */
background-color: red;
}
button.retry {
/* For all buttons having class="retry", the background color
above will be overriden */
background-color: green;
}
In the example above "button.retry" gets a higher weighting (since it is more specific), and then it will use this background-color property for all buttons having class="retry".
Here's another example:
.h5p-joubelui-button {
background-color: green;
}
.h5p-joubelui-button {
background-color: red;
}
These two declarations have equal specificity. In this case, the last declaration found is applied to the element, i.e.: the background color will be red. This is relevant for h5p.com since the Custom CSS is loaded after the default CSS provided by h5p.com.
Read more about CSS Specificity here:https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
Best Practices
- Only override the things you want to be changed. I.e: don't add CSS that does not change anything.
- Be specific when creating the CSS selector rules. For example: if you want to change the color of a button inside Multiple Choice only, you need to make sure your selector reflects that, ref. the examples below.
- Add comments to your CSS customizations, so it is easy to figure out later what you actually are changing. In CSS, comments need to use the following format: /* Here's a comment */
Examples:
Change colors for all buttons across (most) content types
In this example, we will change the background color for buttons across different content types.
.h5p-joubelui-button {
background-color: #000;
}
/* Hover and focus effect */
.h5p-joubelui-button:hover,
.h5p-joubelui-button:focus {
background-color: #444;
}
/* Click effect */
.h5p-joubelui-button:active {
background-color: #222;
box-shadow: none;
}
When applied, it will look like this:
Do note that this won't apply to all buttons in all content types, only the ones having the h5p-joubelui-button class.
Change colors for buttons in a certain content type
In this example, we're gonna change the colors of the buttons inside Fill in the Blanks content.
.h5p-blanks .h5p-joubelui-button {
background-color: #000;
}
/* Hover and focus effect */
.h5p-blanks .h5p-joubelui-button:hover,
.h5p-blanks .h5p-joubelui-button:focus {
background-color: #444;
}
/* Click effect */
.h5p-blanks .h5p-joubelui-button:active {
background-color: #222;
box-shadow: none;
}
As you can see, we have added ".h5p-blanks" to each of the selectors. To find the selector to use for a certain content type, you should locate the DOM element inside the H5P iframe having the h5p-container class, which will also have an "h5p-<content-type>" class.
Change colors for buttons when a certain content type is included in a certain content type
In the previous example, we changed the button color to Fill in the blanks everywhere. I.e.: both standalone and also when used inside other content types (e.g. in Course Presentation). In this example, we're applying the same change, but only when Fill in the blanks is displayed within a Course Presentation:
.h5p-course-presentation .h5p-blanks .h5p-joubelui-button {
background-color: #000;
}
/* Hover and focus effect */
.h5p-course-presentation .h5p-blanks .h5p-joubelui-button:hover,
.h5p-course-presentation .h5p-blanks .h5p-joubelui-button:focus {
background-color: #444;
}
/* Click effect */
.h5p-course-presentation .h5p-blanks .h5p-joubelui-button:active {
background-color: #222;
box-shadow: none;
}
Change colors for buttons within a specific H5P content instance
In this example, we're gonna do the same as above, but we will make sure it applies to a specific Fill in the Blanks instance.
.h5p-content[data-content-id="1291454716549314006"] .h5p-joubelui-button {
background-color: #000;
}
/* Hover and focus effect */
.h5p-content[data-content-id="1291454716549314006"] .h5p-joubelui-button:hover,
.h5p-content[data-content-id="1291454716549314006"] .h5p-joubelui-button:focus {
background-color: #444;
}
/* Click effect */
.h5p-content[data-content-id="1291454716549314006"] .h5p-joubelui-button:active {
background-color: #222;
box-shadow: none;
}
As you can see above, we have added .h5p-content[data-content-id="1291454716549314006"] in the selector. The data-content-id is unique per content item and is the same value seen in the last part of the URL when viewing an H5P.
Using a Google Chrome extension to create the CSS
If doing major CSS customizations, it is a bit cumbersome to change the Custom CSS on h5p.com over and over again. Another option is to use the browser's capabilities to create the CSS locally. This has other benefits as well:
- The person creating the CSS doesn't need to have admin access to the h5p.com account.
- The CSS changes are reflected right away. I.e: you don't have to refresh the browser page.
- The CSS can be created while viewing the H5P from within the LMS.
There are several different ways of achieving this, but here we will look at using a Google Chrome extension named Stylus:
First, you need to install Stylus. Then, you should set it up like illustrated below:
Below is the result when visiting a Fill in the Blanks from within the Canvas LMS: