【问题标题】:How to add background image to a div in angular 8?如何将背景图像添加到角度 8 的 div?
【发布时间】:2020-09-22 20:44:23
【问题描述】:

我想将背景图像添加到 divrow1。我尝试过使用style.backgroundstyle.background-image

.html

<div class="row1"  [style.background-image]="'url('imgUrl') ' " >

.ts

imgUrl="assets/images/userImg.png"

【问题讨论】:

    标签: angular typescript angular8 ng-style


    【解决方案1】:

    imgUrl 是一个变量,但您正在使用它的名称。

    试试

    <div class="row1" [style.background-image]="'url(\'' + imgUrl + '\')'" >
    

    【讨论】:

    • 通过使用这个我得到多个图像
    【解决方案2】:

    使用这个:

    <div class="row1" [ngStyle]="{ 'background': 'url(' + imgUrl + ')' }"></div>
    

    <div class="row1" [style.background]="'url(' + imgUrl + ')'"></div>
    

    【讨论】:

    • 通过使用这个我得到多个图像
    • @naman 也试试添加这个 - 'background-repeat': 'no-repeat'
    猜你喜欢
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多