首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么CSS伪元素在图像中不起作用

为什么CSS伪元素在图像中不起作用
EN

Stack Overflow用户
提问于 2020-09-11 11:52:35
回答 3查看 169关注 0票数 0

首先,我试图用一个类似的问题在堆栈溢出上寻找答案,但结果仍然是一样的,它没有显示我所指的图像。

这就是我的html结构:

代码语言:javascript
复制
<div class="header__shape">
            <img src="img/rose.png" alt="rose couples" class="header__shape-img">
            <p class="header__shape-quote">
                Being someone’s first love may be great, <br>
                but to be their last is beyond perfect.
            </p>
</div>

这是为我的css和伪::之前

代码语言:javascript
复制
&-quote{
            position   : absolute;
            font-size  : $font-size-2;
            font-family: $font-secondary;
            color      : $color-font;
            line-height: 1.3;
            right      : 45rem;
            top        : 45rem;

            &::before{
                content         : "";
                background-image: url(../../img/quote.png);
                height          : 50px;
                display         : block;
                width           : 50px;

            }
        }

我想要的是这样的

但实际上什么都没发生

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-09-11 12:40:13

下面是我将如何解决这个问题。首先,您需要为父级提供一个position: relative,以便在div中移动p标记

然后将伪元素的位置设为绝对位置,以便在p标记周围移动它。

代码语言:javascript
复制
.header__shape {
    position: relative;
}

.header__shape-quote {
    line-height: 1.3;
    font-size: 3rem;
    color: red;
    position: absolute;
    right: 30rem;
    top: 40rem;

}

.header__shape-quote::before {
    font-size: 4rem;
    position: absolute;
    top: -50px;
    left: -60px;
    /* must have for fontawesome */
    display: inline-block;
    font-style: normal;
    font-variant: normal;
    text-rendering: auto;
    -webkit-font-smoothing: antialiased;
    font-family: "Font Awesome 5 Free";
    font-weight: 900;
    content: "\f10e";
}

以一种不同的方式。我个人推荐这个。而不是上面例子中的绝对数。但我不知道哪个对你更好。所以我把两个都加进去了

代码语言:javascript
复制
.header__shape {
  position: relative;
}

.header__shape-quote {
  line-height: 1.3;
  font-size: 3rem;
  color: red;
  margin-left: 5rem;
  margin-top: 5rem;
  position: relative;
}

.header__shape-quote::before {
  font-size: 4rem;
  position: absolute;
  top: -50px;
  left: -60px;
  /* must have for fontawesome */
  display: inline-block;
  font-style: normal;
  font-variant: normal;
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
  font-family: "Font Awesome 5 Free";
  font-weight: 900;
  content: "\f10e";
}
代码语言:javascript
复制
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" integrity="sha512-1PKOgIY59xJ8Co8+NE6FZ+LOAZKjy+KY8iq0G4B3CyeY6wYHN3yt9PW0XpSriVlkMXe40PTKnXrLnZ9+fkDaog==" crossorigin="anonymous" />

<div class="header__shape">
  <img src="https://picsum.photos/600" rose couples class="header__shape-img">
  <p class="header__shape-quote">
    Being someone’s first love may be great, <br> but to be their last is beyond perfect.
  </p>
</div>

编辑

使用你的资产是很正常的。只需在URL中添加路径,并赋予其heightwidth,您就应该可以运行了。

代码语言:javascript
复制
.header__shape {
  position: relative;
}

.header__shape-quote {
  line-height: 1.3;
  font-size: 3rem;
  color: red;
  margin-left: 5rem;
  margin-top: 5rem;
  position: relative;
}

.header__shape-quote::before {
  font-size: 4rem;
  position: absolute;
  top: -50px;
  left: -60px;
  /* must have for fontawesome */
  display: inline-block;
  /* content: url('../images/caret-down-solid.svg'); */
  content: url("https://picsum.photos/40");
}
代码语言:javascript
复制
<div class="header__shape">
  <img src="https://picsum.photos/600" rose couples class="header__shape-img">
  <p class="header__shape-quote">
    Being someone’s first love may be great, <br> but to be their last is beyond perfect.
  </p>
</div>

您还可以将其添加为background-image,而不是在content中添加URL。这完全没问题。

代码语言:javascript
复制
.header__shape {
  position: relative;
}

.header__shape-quote {
  line-height: 1.3;
  font-size: 3rem;
  color: red;
  margin-left: 5rem;
  margin-top: 5rem;
  position: relative;
}

.header__shape-quote::before {
  font-size: 4rem;
  position: absolute;
  top: -50px;
  left: -60px;
  /* must have for fontawesome */
  display: inline-block;
  content: "";
  height: 40px;
  width: 40px;
  /*  background: url('../images/caret-down-solid.svg') no-repeat;  that works fine i'm just using external link  */
  background: url('https://picsum.photos/40') no-repeat;
}
代码语言:javascript
复制
<div class="header__shape">
  <img src="https://picsum.photos/600" rose couples class="header__shape-img">
  <p class="header__shape-quote">
    Being someone’s first love may be great, <br> but to be their last is beyond perfect.
  </p>
</div>

请随意选择最适合您的方式。

这不管用吗?请让我知道。我会尽我所能帮助你。

票数 1
EN

Stack Overflow用户

发布于 2020-09-11 12:28:10

你可以尝试添加<q>标签,这样如果你想为<q>标签添加CSS,你可以添加你的CSS类

代码语言:javascript
复制
<div class="header__shape">
            <img src="pic_trulli.jpg" alt="rose couples" class="header__shape-img">
            <p><q>
                Being someone’s first love may be great, <br>
                but to be their last is beyond perfect.</q>
            </p>
</div>
票数 0
EN

Stack Overflow用户

发布于 2020-09-11 13:01:07

将浮动、背景大小属性添加到伪元素的样式中,如下所示:

代码语言:javascript
复制
&-quote {
        position   : absolute;
        font-size  : $font-size-2;
        font-family: $font-secondary;
        color      : $color-font;
        line-height: 1.3;
        right      : 45rem;
        top        : 45rem;

        &::before {
            content         : "";
            background-image: url(../../img/quote.png);                
            height          : 50px;
            width           : 50px;
            /* new properties */
            float: left;
            background-size: contain;
        }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63840561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档