Creating a horizontal slider in Ionic 4

Creating a horizontal slider in Ionic 4

  • Share on Pinterest

In this blog post I will show how to make a horizontal slider for a magazine/blog style app.

I think the slider ui component are really great for displaying highlighted content in an app. e.g. Featured blog post, most liked, recommend, top stories etc.

The final result will look like this:

First, make sure you are running the latest Node version

Then create a new Ionic 4 project.

$ ionic start myApp blank --type=angular

For this example I will just use the blank template. *Note the –type=angular for ionic 4 project.

$ cd myApp
$ ionic serve

For the horizontal slider feature, we will use the <ion-slides> component.

Navigate to the under src/app/home

<!-- -->
      Blog Stories

    <ion-slides pager="true">
            <h1>Slide 1</h1>
            <h1>Slide 2</h1>
            <h1>Slide 3</h1>


This will get us something like this. Now lets add some content to it.

Normally I will use the HttpClient, to fetch some kind of content from a web api to display.

In this example I will just use some hardcoded data. So I created topStories which contains an array of hashes each with a title:, author:, body: and picture:


import { Component } from '@angular/core';

  selector: 'app-home',
  templateUrl: '',
  styleUrls: [''],
export class HomePage {

  topStories: any;


    this.topStories = [
      {title: "Exploring San Francisco", author: "Rea Ramsey", body: "", picture: ""},
      {title: "Coffee the right way", author: "Ellesha Hartley", body: "", picture: ""},
      {title: "Best Hiking In Yosemite", author: "Vinnie Alexander", body: "", picture: ""},
      {title: "Astro Photography Guide", author: "Greg Rakozy", body: "", picture: ""}




Now it’s just a matter of looping through the array with *ngFor and display the data in the template.

I’m using the <ion-img> component for displaying the images. ion-img only loads images visible in the DOM. This gives you some performance enhancements when loading the app.

<!-- -->
    <ion-slides class="slide-padding" pager="false">
        <ion-slide *ngFor="let story of topStories">
            <div class="container">
                <ion-img src="{{story.picture}}"></ion-img>
                <h2 class="slidetitle">
                    <span class="block">{{story.title}}</span>
                <p class="author">By {{}}, 2h ago</p>

    <div class="sub-headers">
        <p>Latest News</p>



Time for some styling to make things look prettier.

     position: relative;
     width: 100%;

.slidetitle { 
    position: absolute; 
    bottom: 60px;
    left: 18px;
    color: white;
    text-align: left;
    font-family: 'Libre Baskerville', serif;

    background-color:rgba(0, 0, 0, 0.5);
    padding: 4px;

 .author {
    position: absolute; 
    bottom: 25px;
    left: 20px;
    color: rgb(240, 240, 240);
    font-size: 15px !important;
    font-weight: 300;
    text-shadow: 1px 1px 3px #000000;

    padding-left: 20px;
    text-align: center;
    font-weight: bold;


  • Hina Malik
    Hina Malik Hina Malik

    Images size are not get equal height. Can you help please?

Leave a Reply to Hina Malik
Cancel Reply