H5P Guides

Dress Casual Trulli Boutique Trulli Trulli winter Boutique winter Boutique Casual Dress Casual winter 7qFExHAF

Learn how to make your H5P responsive

When creating a responsive design for H5P content there are several issues to take into account. Since H5P content can not assume anything about the frame it is embedded inside, we have to design the content to fit, scale and adapt to any container. The following sections will address some issues that should be taken into account when designing responsive H5P content.

Use relative units of measurements

When styling CSS elements, the designer should strive to define their dimensions in relative units of measurements.

Em is a unit that always is relative to the current font size. Unless you override the font size settings with an absolute unit (such as px or pt ), em will be affected by the user's browser or OS in their device, and thus scale similar to the rest of the content on a web page.

is also a relative unit and is measured relative to the height or width of the parent element.

Using em and % units when designing h5p content allows your design to adapt to the parent frame on any device, whereas using an absolute unit, such as px does not.

Adjust layout

When the size of the parent container for your content is reduced to a certain point, you might want to move around some of the elements inside. To do this, we must listen for the resize event. This event is triggered every time the window or parent library is resized. Here you can make all the necessary measurements of containers and scale or adapt your content accordingly. Making a standard javascript file for a responsive content type look like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
H5P.NewContent = ( function ($) {
   /**
    * @class H5P.NewContent
    */
   function Trulli Boutique Dress Trulli Casual Casual Casual winter Trulli winter winter Boutique Dress Boutique C() {
     var self = this ;
 
     // Changes layout responsively when resized
     self.on( 'resize' Dress Casual Casual Trulli Boutique winter Dress Boutique Boutique Trulli winter Casual Trulli winter , function () {
       // Give some constraints for when changes should happen to NewContent
       if (self.$wrapper.width() < 300) {
         winter winter Boutique Trulli Trulli Boutique Casual Dress winter Casual Casual Trulli Boutique Dress // For example, change the layout when parent container is less than 300px
         self.$wrapper.html( 'Small parent container.' );
       }
     });
   };
 
   /**
    winter Trulli Dress Boutique Dress Boutique Trulli Casual winter winter Casual Trulli Casual Boutique * Attach function called by H5P framework to insert H5P content into
    * page.
    *
    * @param {H5P.jQuery} $container
    */
  Casual Skirt Vera Simply Wang Vera leisure Boutique Pxqz8X68 C.prototype.attach = function ($container) {
     var self = this ;
 
     if (self.$wrapper === undefined) {
       // Create our wrapper on first attach.
       self.$wrapper = $( '
         html: Dress Trulli Trulli winter Boutique winter Boutique Casual Boutique winter Casual Trulli Dress Casual 'Big parent container.'
       });
     }
 
     // Attach wrapper to container.
     $container.html( '' ).append(self.$wrapper);
   }
     
   return C;
})(H5P.jQuery);

Content scale with font size

On devices with high resolutions, but small screens, such as most modern mobile phones, the width of the parent container for H5P content can be quite high. However, this does not necessarily mean that we have a lot of space for our content. The space that we have for our content is a relation between the width of the container in pixels, and the font size that the content is described with. For example if we have a lot of width, we might be fooled to think that we have a lot of room for our content, however the font size could be huge to allow people to see the text easily on small devices. In this case, a sentence which we think might cover half the container width could end up covering the whole width of the parent container. The following picture demonstrates how available space in a container shrinks when font size increases and shows the resulting relative container width.

 

 

The width of the parent container has to be measured in relation to a context. In most cases this context will be the font-size of the container, and thus the rest of the content on the web page. When we find the relationship between the width of the parent container and the font size of the web page we know the real space that our content can use. To find the width of our parent container in relation to the font size we divide the parent width on the font size of the web page. The resulting value of this is the size of the parent container in Em, using this value you can resize and rearrange the content accordingly. This method is demonstrated in the following example, using the "Drag The Words" content type.

Example: Drag The Words.

Drag The Words is an H5P content type with a responsive design. Initially the content type will start out with a textarea to the left and a draggable words area in the right area of the screen as shown in the following picture:

 

The textarea has a right margin corresponding to the widest draggable word. When one of the words exceeds a set width of 150 pixels or the width of the parent shrinks below 43 em, the draggable words will align to the bottom of the textarea as shown in the next picture:

 

Kenneth REACTION Boutique Jacket Leather Cole Faux winter UwaT5q5cC6

Trulli Dress winter winter Boutique Trulli Casual Dress Boutique winter Trulli Casual Boutique Casual

Following is the function used to change the layout. This function is used inside resize. The different variables used when changing layout are:

  • self.$inner                  - The parent container of Drag The Words
  • self.$draggables        - The draggable words container
  • self.$wordContainer - The textarea container
  • self.draggablesArray - An array containing all the draggable words

 

1
2
3
4
5
Sateen Mix Sateen Mix Tank Tank PZgZF8
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Boutique Casual Dress Trulli winter Boutique Trulli Boutique Casual winter Trulli winter Dress Casual 25
26
27
/**
  * Adds the draggables on the right side of the screen if widescreen is detected. 
  */
this .changeLayoutToFitWidth = function () {
   //Find ratio of width to em, and make sure it is less than the predefined ratio.
   if (( this .$inner.width() / parseFloat($( "body" ).css( "font-size" Casual Trulli Boutique Casual Dress Casual Boutique Boutique winter winter Dress Trulli winter Trulli )) > 43) && ( this .widest < 150)) {
     // Adds a class that floats the draggables to the right.
     this .$draggables.addClass( 'h5p-drag-wide-screen' );
     // Detach and reappend the wordContainer so it will fill up the remaining space left by draggables.
     this .$wordContainer.detach().appendTo( this .$taskContainer);
     // Set margin so the wordContainer does not expand when there are no more draggables left.
Dress Casual Trulli Casual winter Boutique winter Dress winter Boutique Trulli Casual Boutique Trulli      Dress Dress Boutique Trulli Boutique Trulli winter Casual Casual winter Casual Boutique Trulli winter this .$wordContainer.css({ 'margin-right' : this .widest});
     // Set all draggables to be blocks
winter Dress Casual Boutique Boutique Casual Trulli Boutique Trulli winter Trulli winter Casual Dress      this .draggablesArray.forEach( function (draggable) {
       draggable.getDraggableElement().addClass( 'h5p-drag-draggable-wide-screen' winter Trulli Casual Boutique Casual Casual Dress Boutique winter Trulli Dress Trulli winter Boutique );
     });
   }
   else {
     // Remove the specific wide screen settings.
     this .$wordContainer.css({ 'margin-right' : 0});
     Dress Boutique Trulli Trulli Casual Dress winter winter Trulli Casual Boutique winter Boutique Casual this .$draggables.removeClass( 'h5p-drag-wide-screen' );
     this .$draggables.detach().appendTo( this .$taskContainer);
     this .draggablesArray.forEach( function (draggable) {
       draggable.getDraggableElement().removeClass( 'h5p-drag-draggable-wide-screen' );
     });
   }
};

 

If you want to study this case further all our libraries and responsive design are available at GitHub.

You can also read more about why conventional CSS tricks do not apply to H5P content in our "Responsive H5P content" blog post