How to add border-radius to ion-img in Ionic 4

痴心易碎 提交于 2020-05-16 07:51:29

问题


I need to add border-radius to ion-img but it seems shadow DOM won't let me modify.

HTML

<ion-img [src]="img-url" [alt]="alt"></ion-img>

CSS

ion-img  {
    border-radius: 10px !important;
}

回答1:


HTML

<ion-img [src]="img-url" class="your-img"></ion-img>

CSS

.your-img {
    border-radius: 10px !important;
    overflow: hidden;
}



回答2:


It's tricky to encapsulate style with Shadow DOM, but there are ways.

In this case, does it work for you with an inline style attribute?

<ion-img [src]="img-url" [alt]="alt" [style]="border-radius: 10px;"></ion-img>

Otherwise you might try adding a <style></style> tag to the inner HTML of the Shadow DOM with the border radius.

A modified example from the attached link:

const host = document.getElementById('shadow-host');

host.shadowRoot.innerHTML = `
<style>
  img {
    border-radius: 10px;
  }
</style>
`


来源:https://stackoverflow.com/questions/58640790/how-to-add-border-radius-to-ion-img-in-ionic-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!