css - 添加背景图像到一串动态文本的末尾,在文本后面

标签 css css-selectors pseudo-element

我需要在我的 h1 文本后面(或前面 - 这无关紧要)放置一张图片,并将其放置在文本末尾的右侧,如下所示:

我似乎无法让背景图片显示在文本之上或之后。我错过了什么?

h1 {
  text-align: center;
}

h1:after {
  content: "";
  background: url("https://static1.squarespace.com/static/50f111c8e4b02b3b2218af91/t/5d9fa26b176671739c726240/1570742891482/CRMC-2020-Measure-h1-1a.png") no-repeat;
  background-position: -85px 12px;
  background-size: 32%;
  width: 400px;
  height: 50px;
  position: absolute;
}
<h1>Dynamic Headline</h1>

使用 :after,我无法让图像显示在文本的后面或上面。

最佳答案

为了将背景移动到显示在文本之上,而不是 background-position 您正在寻找 margin-left。但是请注意,您不能将 margin-bottom 应用于绝对定位的元素,因此您仍然需要使用 background-position 来调整垂直偏移。在以下示例中,我已将其更改为 4px:

h1 {
  text-align: center;
}

h1:after {
  content: "";
  background: url("https://static1.squarespace.com/static/50f111c8e4b02b3b2218af91/t/5d9fa26b176671739c726240/1570742891482/CRMC-2020-Measure-h1-1a.png") no-repeat;
  background-size: 32%;
  width: 400px;
  height: 50px;
  position: absolute;
  margin-left: -85px;
  background-position: 0 4px;
}
<h1>Dynamic Headline</h1>

关于css - 添加背景图像到一串动态文本的末尾,在文本后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58332411/

相关文章:

html - <br> 和::之后或::之前

css - 使用 webpack 将 css 移动到 Angular 4 组件中的外部文件

html - 将图像推到卡外后,只显示一半

javascript - 将鼠标悬停在按钮上时显示图像

多个子元素的 CSS 选择器

html - 为什么:before pseudo tag not add color overlay to this figure

javascript - 如何从 wordpress 页面上传 pdf 文件,文件必须保存在 wp-content 上传目录文件夹中

html - css nth-of-type 长列表中的每个第一,第二,第三

python - 在 Beautifulsoup 中使用 select 函数返回 None 值

CSS3 :after disappear if his parent has overflow:hidden;