首页 > 解决方案 > 为什么 CSS 伪元素在图像中不起作用

问题描述

首先,我尝试用类似的问题找到堆栈溢出的答案,但结果仍然相同,它没有显示我的意思。

所以这是我的html结构:

<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 以及伪 ::before

&-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;

            }
        }

我想要这样,在此处输入图像描述但实际上什么也没发生在此处输入图像描述

标签: htmlcsspseudo-element

解决方案


这是我将如何解决它。首先,您需要给父级 aposition: relative以便pdiv

然后给伪元素位置 absolute 以在p标签周围移动它。

.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";
}

在此处输入图像描述

并以不同的方式。我个人推荐这个。以上示例中的绝对值。但我不知道哪个更适合你。所以我两个都加了

.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";
}
<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你就可以开始了。

.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");
}
<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>

在此处输入图像描述

您也可以将其添加为 abackground-image而不是URLcontent. 没关系。

.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;
}
<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>

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

那没有帮助?请告诉我。我会尽力提供帮助。


推荐阅读