H5P Guides

Navy Casual Boutique Old winter Dress qCHO7Ewt

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 Old Dress Casual Boutique winter Navy C() {
     var self = this ;
 
     // Changes layout responsively when resized
     self.on( 'resize' Old winter Boutique Navy Casual Dress , function () {
       // Give some constraints for when changes should happen to NewContent
       if (self.$wrapper.width() < 300) {
         Boutique Dress Casual Old Navy winter // For example, change the layout when parent container is less than 300px
         self.$wrapper.html( 'Small parent container.' );
       }
     });
   };
 
   /**
    Old Casual winter Boutique Dress Navy * Attach function called by H5P framework to insert H5P content into
    * page.
    *
    * @param {H5P.jQuery} $container
    */
     var self = this ;
 
     if (self.$wrapper === undefined) {
       // Create our wrapper on first attach.
       self.$wrapper = $( '
         html: Dress Navy Casual winter Old Boutique '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:

 

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
Shorts American Outfitters Boutique Eagle Denim 4IpqwUZ
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
winter Old Casual Navy Dress Boutique 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" Boutique Old winter Navy Casual Dress )) > 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.
Navy Old Boutique Casual winter Dress      Boutique Navy Dress Casual winter Old this .$wordContainer.css({ 'margin-right' : this .widest});
     // Set all draggables to be blocks
Dress Boutique Old Navy winter Casual      this .draggablesArray.forEach( function (draggable) {
       draggable.getDraggableElement().addClass( 'h5p-drag-draggable-wide-screen' winter Casual Navy Old Dress Boutique );
     });
   }
   else {
     // Remove the specific wide screen settings.
     this .$wordContainer.css({ 'margin-right' : 0});
     Casual Old Dress Navy winter Boutique 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