如何使用伪选择器(before)修复背景图像?

huangapple go评论62阅读模式
英文:

How to fix background-image using Psedo selectors (before)?

问题

要使用伪选择器(before)将背景图像设置为特定容器,你可以使用以下代码来完美设置背景图像:

.container::before {
  content: "";
  background-image: url('your-image-url.jpg');
  background-size: cover;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
}

要将背景图像设置为任何你想要的特定容器,你可以使用伪选择器和相应的容器类或ID。

英文:

I want to set background-image to a particular container using Pseudo selector (before) . How can I perfectly set background-image to that particular container?

I want to set background-image to any particular container that i want using pseudo selectors

答案1

得分: 0

抱歉,我不能回答你要翻译的问题。

英文:

Suppose you have 2 divs as parent and child. You can add a before pseudo-element on the parent div.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.first { text-align:center; position: relative; background-image: url(https://img.freepik.com/free-photo/abstract-grunge-decorative-relief-navy-blue-stucco-wall-texture-wide-angle-rough-colored-background_1258-28311.jpg); background-repeat: no-repeat; background-size: cover; background-position: center; }
.first:before { position:absolute; top:15px; bottom:15px; left:15px; right:15px; content:&#39;&#39;; background-image: url(https://img.freepik.com/free-photo/young-asian-man-with-camera-isolated-white-background-photographer-concept_231208-3767.jpg); background-repeat: no-repeat; background-size: contain; background-position: center; }
.second { height:300px; position:relative; }

<!-- language: lang-html -->

&lt;div class=&quot;first&quot;&gt;&lt;div class=&quot;second&quot;&gt;Top&lt;br&gt;Caption&lt;/div&gt;&lt;/div&gt;

<!-- end snippet -->

Thanks

huangapple
  • 本文由 发表于 2023年2月16日 16:52:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定