背景图像及其上的元素

background image and elements on it

我需要在图片上添加一些元素:

什么是更好的方法?

我从 img 标签开始,但现在我决定使用背景图片。可悲的是,我遇到了基本问题,因为当我使用背景 属性 时,图像只有在有内容时才可见。我相信这是一个非常简单的问题,仍然。

谢谢你帮助我:)

SCSS

header {
    margin-top: 78px;
    width: 100%;
    background-image:url(../img/header.png);
    background-position: center;
    background-repeat: no-repeat;
}

如果您想在背景图片上叠加内容(看起来像您这样做),最好的方法是将背景图片添加到 div 并为其指定高度,然后在其中添加内容div。例如:

<div class="bg-image">
   <div class="bg-image-module">
     <p>Some Content</p>
   </div>
</div>

.bg-image {
   height: 500px; // change to be what you need.
   background-image: url(/path/to/image.jpg)";
   background-repeat: no-repeat;
   background-position: center;
   background-size: cover;
}

按照 Neelam Khan 的例子,你也可以使用 shortland 属性: HTML + SCSS

<div class="bg-image">
    <div></div>
    <div></div>
    <div></div>
</div>
.bg-image {
    position: relative;
    height: 500px; // change to be what you need.
    background: url(/path/to/image.jpg) center / cover no-repeat;
    div:nth-child(1) { //this will select the first div
        position: absolute;
        top: 30px;
        left: 100px;
    }
}

What is better approach ?

在相对定位元素中使用背景图像css 属性,并将其子元素设置为绝对定位。