【问题标题】:how to add backgroud image Pseudo-element 'after' content with Angular 2?如何使用 Angular 2 添加背景图像伪元素“后”内容?
【发布时间】:2018-12-31 18:01:24
【问题描述】:

我正在尝试将 URL 图像从 Angular 中的 html 传递给我的 CSS,但我不能这样做:

我的 CSS 是:

card-profile_visual {
      height: $visual-height;
      overflow: hidden;
      position: relative;
      background: linear-gradient(to bottom, darken(#545559, 10%), saturate(darken(#3A4A7B, 2%), 20%), saturate(darken(#3A4A7B, 15%), 20%));
      &:before,
      &:after {
        display: block;
        content: '';
        width: 100%;
        height: 100%;
        position: absolute;
        z-index: 0;       
        background: url(myUrl) no-repeat center center/cover;
        opacity: 0.5;
        mix-blend-mode: lighten;
      }

    }

我的 html 是:

<div class="card-profile_visual" [style.background-image]="'url('+offer.photo_url+')'"></div>

如何实现这一点,以将 offer.photo_url 传递给 CSS 中:

背景:url(myUrl) 不重复中心中心/封面;

非常感谢你

【问题讨论】:

标签: javascript css angular sass


【解决方案1】:

您可以在content 属性中使用url

content: url(imageUrl);

【讨论】:

    【解决方案2】:

    使用ngStyleCSS Variables 如下所示:

    代替:

    CSS:

    card-profile_visual {
          height: $visual-height;
          overflow: hidden;
          position: relative;
          background: linear-gradient(to bottom, darken(#545559, 10%), saturate(darken(#3A4A7B, 2%), 20%), saturate(darken(#3A4A7B, 15%), 20%));
          &:before,
          &:after {
            display: block;
            content: '';
            width: 100%;
            height: 100%;
            position: absolute;
            z-index: 0;       
            background: url(myUrl) no-repeat center center/cover;
            opacity: 0.5;
            mix-blend-mode: lighten;
          }
    
        }
    

    HTML:

    <div class="card-profile_visual" [style.background-image]="'url('+offer.photo_url+')'"></div>
    

    使用这个:

    CSS:

    card-profile_visual {
          height: $visual-height;
          overflow: hidden;
          position: relative;
          background: linear-gradient(to bottom, darken(#545559, 10%), saturate(darken(#3A4A7B, 2%), 20%), saturate(darken(#3A4A7B, 15%), 20%));
          &:before,
          &:after {
            display: block;
            content: '';
            width: 100%;
            height: 100%;
            position: absolute;
            z-index: 0;       
            background: var(--my-image) no-repeat center center/cover;
            opacity: 0.5;
            mix-blend-mode: lighten;
          }
        }
    

    HTML:

    <div class="card-profile_visual" [ngStyle]="{'--my-image': ' + offer.photo_url + '}"></div>
    

    希望对你有帮助。

    【讨论】:

    • 嗨,VicJordan ...我已经这样做了,当我检查代码时,我的 html 是:
      和我的 css : background: var(--my-image) no-repeat center center/cover;
    【解决方案3】:

    试试这个

    <div class="card-profile_visual" [style.backgroundImage]="'url('+offer.photo_url+')'"></div>
    

    要在css中的伪元素中添加背景图片,你可以这样做

    .card-profile_visual:after{
      background: url(myUrl) no-repeat center center/cover;
    }
    

    【讨论】:

    • 检查我是如何写背景图片的
    • 我想在 :after background: url(myUrl) no-repeat center center/cover 中传递 url;
    • 我们不能使用ngStyle或者style.backgroundImage来设置after或者before
    • 您需要为此使用 css。我还发现在上面的css中你没有在类名之前使用点
    猜你喜欢
    • 2018-07-21
    • 2021-01-01
    • 2022-01-05
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 2023-03-09
    • 2015-03-23
    • 2020-05-28
    相关资源
    最近更新 更多