Sitemap
Tim Kendall

Tim Kendall

Designer, developer, and college student in O.C.

Ionic.js Platform Styles

Platform specific styles with the Ionic Framework

1 min readJul 1, 2014

Ran into this issue the other day and figured I’d share my solution. The beauty of hybrid mobile apps is being able to share 90% of your codebase between platforms. However if you’ve worked on a larger project you may have run into the need for platform specific styles.

Get Tim Kendall’s stories in your inbox

Join Medium for free to get updates from this writer.

In my solution I wanted to use SASS and also keep the platform specific styles in separate files. The solution is in two parts:

  1. Edit the included gulpfile.js to watch multiple stylesheets
gulp.task(‘sass’, function(done) {
gulp.src([‘./scss/ionic.android.app.scss’,‘./scss/ionic.ios.app.scss’])
.pipe(sass())
.pipe(gulp.dest(‘./www/css/’))
.pipe(minifyCss({
keepSpecialComments: 0
}))
.pipe(rename({ extname: ‘.min.css’ }))
.pipe(gulp.dest(‘./www/css/’))
.on(‘end’, done);
});

2. Include some logic in index.html to load the correct stylesheet (note: make sure this script is placed after ionic.bundle.js; shown below)

<!— ionic/angularjs js →
<script src=”lib/ionic/js/ionic.bundle.js”></script>
<!— Load platform specific styles →
<script>
// Utilize Ionic’s methods for determining platform
if (ionic.Platform.isAndroid()) {
// Android styles
document.write(‘<link href=”css/ionic.android.app.css” rel=”stylesheet”>’);
} else if (ionic.Platform.isIOS()) {
// iOS Styles
document.write(‘<link href=”css/ionic.ios.app.css” rel=”stylesheet”>’);
} else {
// Browser development styles (use Android)
document.write(‘<link href=”css/ionic.android.app.css” rel=”stylesheet”>’);
}
</script>

And that’s it! I’m sure there are probably more elegant ways to do this but hopefully you find it useful.

--

--

Tim Kendall
Tim Kendall

Written by Tim Kendall

Designer, developer, and college student in O.C.