This article by Sandeep Kumar Patel, author of Responsive Web Design with AngularJS we will explore the role of AngularJS for responsive web development. Before going into AngularJS, you will learn about responsive "web development in general. Responsive" web development can be performed "in two ways:
(For more resources related to this topic, see here.)
When we view" web pages through our browser, the browser sends a user agent string to the server. This string" provides information like browser and device details. Reading these details, the browser can be redirected to the appropriate view. This method of reading client details is known as browser sniffing.
The browser string has a lot of different information about the source from where the request is generated. The following diagram shows the information shared by the user string:
Details of the parameters" present in the user agent string are as follows:
A different browser string is generated based on the combination of the device and type of browser used while accessing a web page. The following table shows examples of browser "strings:
Browser |
Device |
User agent string |
Firefox |
Windows desktop |
Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0 |
Chrome |
OS X 10 desktop |
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 |
Opera |
Windows desktop |
Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14 |
Safari |
OS X 10 desktop |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2 |
Internet Explorer |
Windows desktop |
Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0 |
AngularJS has features like providers or services which can be most useful for this browser user-agent sniffing and a redirection approach. An AngularJS provider can be created that can be used in the configuration in the routing module. This provider can have reusable properties and reusable methods that can be used to identify the device and route the specific request to the appropriate template view.
To discover more about user agent strings on various browser and device combinations, visit http://www.useragentstring.com/pages/Browserlist/.
CSS3 brings a "new horizon to web application development. One of the key features" is media queries to develop a responsive web application. Media queries uses media types and features as "deciding parameters to apply the style to the current web page.
CSS3 media queries" provide rules for media types to have different styles applied to a web page. In the media queries specification, media types that should be supported by the implemented browser are listed. These media types are as follows:
A media rule can be declared using the @media keyword with the specific type for the targeted media. The following code shows an example of the media rule usage, where the background body color" is black and text is white for the screen type media, and background body color is white and text is black for the printer media type:
@media screen { body { background:black; color:white; } } @media print{ body { background:white; color:black; } }
An external style "sheet can be downloaded and applied to the current page based on the media type with the HTML link tag. The following code uses the link type in conjunction with media type:
<link rel='stylesheet' media='screen' href='<fileName.css>' />
To learn more about" different media types,visit https://developer.mozilla.org/en-US/docs/Web/CSS/@media#Media_types.
Conditional styles can be "applied to a page based on different features of a device. The features that are "supported by CSS3 media queries to apply styles are as follows:
The following" code shows some examples" of CSS3 media queries using different device features for conditional styles used:
//for screen devices with a minimum aspect ratio 0.5 @media screen and (min-aspect-ratio: 1/2) { img { height: 70px; width: 70px; } } //for all device in portrait viewport @media all and (orientation: portrait) { img { height: 100px; width: 200px; } } //For printer devices with a minimum resolution of 300dpi pixel density @media print and (min-resolution: 300dpi) { img { height: 600px; width: 400px; } }
To learn more" about different media features, visit https://developer.mozilla.org/en-US/docs/Web/CSS/@media#Media_features.
In this chapter, you learned about responsive design and the SPA architecture. "You now understand the role of the AngularJS library when developing a responsive application. We quickly went through all the important features of AngularJS with the coded syntax. In the next chapter, you will set up your AngularJS application and learn to create dynamic routing-based on the devices.
Further resources on this subject: