37

i have tried in global.scss as

ion-content{
    background-image: url('assets/images/cover.jpg');
    -webkit-background-image: url('assets/images/cover.jpg');
    background-repeat: no-repeat;
    background-size:cover;
  }

but this does not render the image. tried path as ./assets/images/cover.jpg as well.

if i put the same image as img tag on the page that shows up ruling out invalid image possibility.

I also tried to put in the homeage.scss as

.myview {
    background-image: url('../../assets/images/cover.jpg');
    background-repeat: no-repeat;
    background-size:cover;
}

and using class="myview" in home.html no luck

Sampath
  • 63,341
  • 64
  • 307
  • 441
Vik
  • 8,721
  • 27
  • 83
  • 168

5 Answers5

91

You can use the CSS-Variable --background to change the background of ion-content.

Example:

ion-content{
    --background: #fff url('../../assets/images/cover.jpg') no-repeat center center / cover;
}

Put this into your components, pages or global scss.

For reference see: https://beta.ionicframework.com/docs/api/content#css-custom-properties

Gary Großgarten
  • 1,522
  • 9
  • 8
32

I solved the problem doing the following:

ion-content {
    --background: none;
    background-image: url('/assets/imgs/page_bg.jpg');
    background-position: center top;
    background-repeat: no-repeat;
    background-size: cover;
}

This will disabled background and then set the correct one.

8

I have encountered the similar situation, having ionic background css property leads to flickering issues on IOS

try the following if you have encountered flickering issue

:host {
  ion-content {
    --background:none;
    background:url(''../../assets/images/cover.jpg');
    background-size: cover;
    background-position: center center;
  }
}

for anyone having keyboard issues (background resizes on keyboard show) install Keyboard plugin

https://ionicframework.com/docs/native/keyboard/

and add the following code on your config.json

<preference name="keyboardResize" value="false" />
<preference name="keyboardResizeMode" value="native" />

Note: this may hide item beneath the keyboard when its shown (I have tested only on iOS)

kishorekumaru
  • 1,500
  • 2
  • 19
  • 33
0

I tried answer from @rchau, but it did not work in my case. Instead, I put this code in my project and it has done the right thing:

ion-content{
    --background:url('../../assets/images/cover.jpg') 0 0/100% 100% no-repeat;
}
Heikki
  • 2,214
  • 19
  • 34
0

This is the working solution for me.

global.scss

ion-content {
    --background: url('../assets/img/background/background.png') no-repeat 100% 100%;
}
Sampath
  • 63,341
  • 64
  • 307
  • 441