SharePoint 2013 WCM Advanced Cookbook
上QQ阅读APP看书,第一时间看更新

Creating a responsive mobile master page

In this recipe, we will modify the seattle.master master page to hide the header controls and float all of the page elements to make them appear vertically stacked. This will allow users to view the site on mobile devices and only require the user to scroll vertically rather than both vertically and horizontally.

This recipe will only cover the basic aspects of making a responsive design master page friendlier to mobile devices. Additional styling and design would be required to provide a more complete mobile user experience.

Responsive designs are usually flexible, allowing pages to render well in various web browsers and are not limited to mobile browsers. Creating responsive designs is a good practice for all web browsers. In this recipe, however, we will focus on a responsive design geared at mobile browsers.

Getting ready

For this recipe, we should have a device channel created to target our mobile browsers.

How to do it...

Follow these steps to create a responsive mobile master page:

  1. Open SharePoint Designer.
  2. Select Open Site. Enter the complete URL to the SharePoint site and select Open.
  3. From the Navigation pane, select Master Pages.
  4. In the list of files in the Master Pages library, make a copy of seattle.master (for our example, we have renamed it Seattle_Responsive.master).
  5. Check out the new Seattle_Responsive.master master page.
  6. Open the Seattle_Responsive.master master page.
  7. Locate the <head> element.
  8. Add the following CSS reference to the Responsive.css file that we created:
    <SharePoint:CssRegistration ID="customCssRegistration" Name="<% $SPUrl:~Site/_catalogs/masterpage/resources/Responsive.css %>" runat="server"></SharePoint:CssRegistration>
  9. Save the master page.
  10. Check in and publish the master page using the Check In and Publish options.
  11. Set the master page as the Site Master Page or assign the master page to the device channel that targets your mobile device.
  12. From the Navigation pane, select All Files.
  13. In the All Files content pane, navigate to _catalogs | masterpage | resources.
  14. From the New section on the ribbon, navigate to File | CSS.
  15. Name the new CSS file Responsive.css.
  16. Check out the new Responsive.css file.
  17. Open the Responsive.css file.
  18. Hide the header controls using the following code:
    #suiteBar, #s4-ribbonrow {
    display: none;
    }
  19. Set the title area to have an automatic width using the following code:
    #titleAreaRow, #s4-titlerow {
    height: auto;
    width: auto;
    }
  20. Set the content of the title and content areas to float using the following code:
    #titleAreaRow > div, #contentRow > div, #layoutsTable td {
    float: left;
    display: inline-block;
    }
  21. Set the page content to have a minimum width and set its left-hand side margin to override the default margin using the following code:
    #contentBox {
    min-width: 100px;
    margin-left: 20px;
    }
  22. Set the workspace container to have a forced automatic width using the following code:
    #s4-workspace {
    width: auto !important;
    }
  23. Save the CSS file.
  24. Check in and publish the CSS file using the Check In and Publish options.
  25. Navigate to the site in your preferred web browser or your mobile device to observe the results. The results will be similar to the following screenshot:
    How to do it...

How it works...

In this recipe, we have used CSS to hide controls and to make the remaining controls float and use automatic widths. By making these controls float and use automatic widths, we are making them appear stacked vertically to provide smaller screens with better visibility of the content. The content on the screen will be adjusted based on the width of the screen and so on.