Resize an avatar and have it top of an image

Hey guys!

My first post here :slight_smile:

I am trying to create the following view in my app (sketch on the right).

  1. I would like the avatar to be a little bigger and overlap on the right with the picture.
  2. Have a heart icon on top right of the image
  3. Have good overall spacing between the avatar, title and bottom icons

image

I have the following code at the moment.

<ion-item>
    <img src="image.url"/>
    <ion-item-content>
        <h2 class="card-title">
            Nine Inch Nails Live
        </h2>
        <ion-avatar class="profilepic" item-right>
            <img src="image.url"/>
        </ion-avatar>
    </ion-item-content>
    <ion-row no-padding>
        <ion-col>
            <button clear small danger>
                <ion-icon name='star'></ion-icon>
                <label text-capitalize>Favorite</label>
            </button>
        </ion-col>
        <ion-col text-center>
            <button clear small danger>
                <ion-icon name='musical-notes'></ion-icon>
                <label text-capitalize>Listen</label>
            </button>
        </ion-col>
        <ion-col text-right>
            <button clear small danger>
                <ion-icon name='share-alt'></ion-icon>
                <label text-capitalize>Share</label>
            </button>
        </ion-col>
    </ion-row>
</ion-item>

Can someone help me with this?

Thanks ^^

Ok guys. This is the progress I have made so far.

  1. Avatar is resized with css but itā€™s no longer a circle. Is it possible to have a circle?
  2. Avatar is on the left side but image goes on top of it. z-index doesnā€™t seem to work on this.

Is there a better way to implement this? Thanks.

image

Hereā€™s the html code

<ion-item>
    <div class="fav-icon1">
        <img src="image_url"/>
        <button clear danger large class="fav-icon2">
                <ion-icon ios="ios-heart-outline" md="md-heart-outline"></ion-icon>
        </button>
    </div>
    <ion-item>
        <h2>
            Nine Inch Nails Live
        </h2>
        <ion-avatar item-right class="profilepicposition">
            
                <img class="profilepicsize" src="image_url"/>
            
        </ion-avatar>
    </ion-item>

    <ion-row no-padding>
        <ion-col>
            <button clear small danger>
                <ion-icon name='star'></ion-icon>
                <label text-capitalize>Favorite</label>
            </button>
        </ion-col>
        <ion-col text-center>
            <button clear small danger>
                <ion-icon name='musical-notes'></ion-icon>
                <label text-capitalize>Listen</label>
            </button>
        </ion-col>
        <ion-col text-right>
            <button clear small danger>
                <ion-icon name='share-alt'></ion-icon>
                <label text-capitalize>Share</label>
            </button>
        </ion-col>
    </ion-row>
</ion-item>

And the css code

.profilepicposition  {
    position: absolute;
    top: -80%;
    right: 0%;
    z-index: 89;
}


.profilepicsize  {
    width: 100% !important;  
    height : 100% !important;  
    max-width: 50px !important;  //any size
    max-height: 50px !important; //any size 
}

.fav-icon1 {
    position: relative;
}

.fav-icon2 {
    position: absolute;
    top: 6%;
    right: 4%;
}

did you try to put a z-index to both item not only one. And thereā€™s no need to put 89 as a value. 0 and 1 should do the job. Unless thereā€™s something specific for ion element Iā€™m not aware of.

Just tried. Iā€™ve put a z-index value on the image and on the imageā€™s div but no result. It stays the same. Image is overlapping the avatar.

For z-index value, Iā€™ve put a high value just to try it out.

z-index should work , just check in the debug tools what classes have what values of z-index.

and I would recommend using fixed sizes for .profilepicsize class rather than % values and use border-radius as half the value of height and width.

This should work

1 Like

Thank you very much!! This has solved the shape problem and Iā€™ve got a circle now :grinning:

.profilepicposition has position:absolute value and by adding position:static to the ā€˜ion-itemā€™ containing the avatar, it solved the problem. Avatar is on top of the image ^^

Bonus question: Is it possible to have a small white border on the avatar?

sure ā€¦ use normal border style like
border: 3.5px solid #fff;
and it should work

1 Like

@piyukore problem solved :slight_smile:

Iā€™m new to Ionic and programming in general. Thank you for your help.

keep coding and it will be easy with time :slight_smile:

1 Like

Simil problem, but canā€™t solve.

The icon is half visible. How to fix?
(Itā€™s a list of card, and user can reorder it)

.html

<ion-content class="cards-bg">
    <ion-list no-lines reorder="true" (ionItemReorder)="reorderItems($event)">
        <ion-item *ngFor="let lista of elencoListe">
            <ion-card>
                <div class="item">
                    <ion-card-title>
                        {{lista.titolo}}
                    </ion-card-title>
                    <ion-avatar item-right class="imgListaPosition">
                        <img src="{{lista.userImg}}" class="imgListaSize" />
                    </ion-avatar>
                </div> 
                <ion-card-content>
                    Frequenza: {{lista.frequenza}}
                    <p>
                    Descrizione: {{lista.descrizione}}
                    </p>
                </ion-card-content>                               
            </ion-card>
        </ion-item>
    </ion-list>
</ion-content>

.scss

.cards-bg {
    background-color: #f4f4f7;
    ion-item {
        background-color: #f4f4f7;
    }
}

.imgListaPosition {
    position: absolute;
    z-index: 5;
    top: 20%;
    left: 80%;
}

.imgListaSize {
    width: 100px;  
    height : 100px;  
    max-width: 100px;
    max-height: 100px;
    border-radius: 50px;
}
1 Like

Can you put your .ts file code?

Solved btw :slight_smile:
And the rounded icon can also be non-square image:

.scss

ion-card img {
    margin: auto;
    width: 15vw;
    height: 15vw;
    border-radius: 50%; // half for rounding */
    object-fit: cover; /* Scale the image - Center and crop image*/
    object-position: center; /* Center the image within the element - Center and crop image*/
}

.ts

<ion-item no-lines>
    <ion-card>
        <ion-card-content (click)="takePhoto()">
            <img class="imgModal" [src]="givenImg" *ngIf="givenImg" />
        </ion-card-content>
    </ion-card>
</ion-item>

credit: http://jonathannicol.com/blog/2014/06/16/centre-crop-thumbnails-with-css/

2 Likes

Thanks for the link. Really interesting :slight_smile:

I tried your previous code, it worked by taking out the slash at the end of the image tag

<img src=ā€œ{{lista.userImg}}ā€ class=ā€œimgListaSizeā€>

1 Like

Yeah, I donā€™t know whether to use it or not. :neutral_face:

On stackoverflow, people had recommended to use pixel instead of vw.

I am not sure of the advantages and the reason behind this.

As long as it works, you can keep on building. ^^

Well, pixel would be a problem if i run my app on tablet (maybe 2000x1500) or on an old phone (640x480).
Iā€™m wrong?

There is a problem in rendering vw in iOS. I donā€™t know how this translates with ionic.

Best would be to try and test it on device or emulators and see what happens.

1 Like

Hi guys,

Many thanks for the discussion! I solved also my problem using different answers from here. Hereā€™s my approach. Just in case someone also needs. Regards.

.html

  <ion-card>
    <div>
      <img src="assets/images/landscape.jpg" (tap)="openGallery()">
      <ion-item class="foo01">
        <ion-avatar item-right>
          <img class="foo02" src="assets/images/landscape.jpg">
        </ion-avatar>
      </ion-item>
    </div>  
    <ion-card-content>      
      <ion-card-title>you</ion-card-title>
    </ion-card-content>
  </ion-card>

.scss

   .cards-bg {
        background-color: #f4f4f7;
    }

    .foo01 {
        margin-top: -15%;
        margin-bottom: -2em;
        background: transparent !important;
    }

    .foo02 {
        margin: auto;
        width: 20vw !important;
        height: 20vw !important;
        border: 1vw solid #fff;
    }