Angular Material
Angular Material is a UI Component framework and a reference implementation of Google's Material Design Specification. Further information about Angular Material can be located here.
Angular Material and Card or Tile Based Interface
I have been a great fan of Google Now and the way all the information is presented in a card based interface. So I wanted to implement a similar UI. While researching for card based interfaces, I came across Angular Material. Card is one of the components from Angular Material, which can help us in building card-based User Interfaces.
Before I get into the details, here is how card interface looks,
Building Card Interface
Angular Material provides us various types of cards. These cards can be used based on the type of information we would like to display,
Now let's follow the below steps for our interface which will display our information in card format,
- Create an index.html by using the below code,
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>Angular Material Card</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel='stylesheet' href='angular-material.min.css'>
- <style>
- .card-media {
- background-color: #CACACA;
- }
- </style>
- </head>
- <body ng-app="MyApp">
- <div ng-controller="appController" ng-cloak>
- <md-content class="md-padding" layout-xs="column" layout="row">
- <div flex-xs flex-gt-xs="30" layout="column">
- <md-card>
- <md-card-title>
- <md-card-title-text>
- <span class="md-headline">Large Card</span>
- <span class="md-subhead">Subheading<span>
- </md-card-title-text>
- <md-card-title-media>
- <div class="md-media-lg card-media"></div>
- </md-card-title-media>
- </md-card-title>
- <md-card-actions layout="row" layout-align="end center">
- <md-button>Action 1</md-button>
- <md-button>Action 2</md-button>
- </md-card-actions>
- </md-card>
- <md-card md-theme="default" md-theme-watch="" class="_md md-default-theme">
- <md-card-title>
- <md-card-title-text>
- <span class="md-headline">Small Card</span>
- <span class="md-subhead">Subheading</span>
- </md-card-title-text>
- <md-card-title-media>
- <div class="md-media-sm card-media"></div>
- </md-card-title-media>
- </md-card-title>
- <md-card-actions layout="row" layout-align="end center" class="layout-align-end-center layout-row">
- <button class="md-button md-default-theme md-ink-ripple" type="button" ng-transclude="" aria-label="Action 1"><span class="ng-scope">Action 1</span></button>
- <button class="md-button md-default-theme md-ink-ripple" type="button" ng-transclude="" aria-label="Action 2"><span class="ng-scope">Action 2</span></button>
- </md-card-actions>
- </md-card>
- </div>
- </md-content>
- </div>
- <script src='angular.min.js'></script>
- <script src='angular-animate.min.js'></script>
- <script src='angular-route.min.js'></script>
- <script src='angular-aria.min.js'></script>
- <script src='angular-messages.min.js'></script>
- <script src='angular-material.min.js'></script>
- <script src='app.js'></script>
- </body>
- </html>
Some of the directives from the above code can be explained as below,
Directives |
Description |
layout="row" |
Sets layout of a container is row manner |
layout-xs="column" |
Defines breakpoints for responsiveness |
flex-xs flex-gt-xs="30" |
Child elements within container and their breakpoints. This can be imagined like bootstrap’s grid system |
md-card |
Directive for Card based on Google’s Machine Design |
- Create a new app.js and copy below code to it
- var myapp= angular.module('MyApp', ['ngMaterial']);
-
- myapp.controller('appController', function($scope) {
-
- })
Pl. Note: I have copied all the required JS and CSS files to my local folder while creating this exercise. You can install the Angular Material library (and its dependent libraries) in your local directory using NPM or Bower.
- Open index.html in browser and see how the cards are looking,
- Now let’s add another card and let’s verify how it becomes responsive. For that in the above code, just locate </md-content> tag before our <script> tags at the end of index.html. Copy below code snippet,
- <div flex-xs flex-gt-xs="30" layout="column">
- <md-card class="_md">
- <md-card-header>
- <md-card-header-text>
- <span class="md-title">Right Information</span>
- <span class="md-subhead">At right time</span>
- </md-card-header-text>
- </md-card-header>
- <img src="http://placeholdit.imgix.net/~text?w=430&h=220"/>
- <md-card-title>
- <md-card-title-text>
- <span class="md-headline">In-card actions</span>
- <span class="md-subhead">Description</span>
- </md-card-title-text>
- </md-card-title>
- <md-card-actions layout="row" layout-align="start center" class="layout-align-start-center layout-row">
- <button class="md-button md-ink-ripple" type="button" ng-transclude="" aria-label="Action 1"><span class="ng-scope">Action 1</span></button>
- <button class="md-button md-ink-ripple" type="button" ng-transclude="" aria-label="Action 2"><span class="ng-scope">Action 2</span></button>
- </md-card-actions>
- <md-card-content>
- <p>
- Discovered here how UI implementation for Cards is done.
- Use cards to present right information at right time and at right place.
- </p>
- </md-card-content>
- </md-card>
- </div>
- Save index.html and open in a browser and see how the cards are looking,
Now let’s resize the window and see how cards become responsive too. I am using Developer Tools for testing responsiveness of these cards.
This way we can generate a simple card for showing information in a very elegant way. In the next article, we will actually show some information. Just to show the right information at the right time at the right place.