简体   繁体   English

相对于特定图像点的元素,其背景大小为:cover

[英]Element relative to specific Image point which is background-size : cover

I have a scenario like below: 我有一个类似下面的场景:

 .cover-image { background : no-repeat url('https://thumb9.shutterstock.com/display_pic_with_logo/536575/164027042/stock-photo-simple-and-stylish-office-environment-164027042.jpg') center center / cover; height : 100vh; width : 100vw; position : absolute; z-index : 1; left : 0; } body{margin : 0; padding : 0; } .data{ position:absolute; position: absolute; z-index: 10; height: 30px; width: 40px; top: 61vh; left: 11vw; background: red; } .container { position : absolute; height : 100vh; width : 100vw; } 
 <div class="container"> <div class='cover-image'></div> <div class='data'>chair</div> </div> 

Here the Data box should always be at left top of chair, If we resize the window. 如果我们调整窗口大小,这里的数据框应始终位于椅子的左上方。

How can we achieve this? 我们怎样才能做到这一点?

There are better ways to accomplish that, but taking your existing code and make it work, the following seems to be the solution for your scenario. 有更好的方法可以实现此目的,但是采用现有代码并使之正常工作,以下似乎是您的方案的解决方案。 Note that you will have to play around with the correct sizing, this is just the idea. 请注意,您将不得不使用正确的大小,这只是想法。 (You can see a DEMO here). (您可以在此处看到一个DEMO )。

HTML HTML

<div class="container">
   <div class="cover-image">
   </div>

   <div class="data">Chair</div>
</div>

CSS CSS

body{
   margin: 0 auto;
}
.container{
   width: 1000px;
}

.cover-image { 
   background : no-repeat url('https://thumb9.shutterstock.com/display_pic_with_logo/536575/164027042/stock-photo-simple-and-stylish-office-environment-164027042.jpg') center center / cover;  
   float: left;
   height: 942px;
   width: 1920px;  
}

.data { 
   margin-right: 20%;
   background-color: red;
   float: right;
   height: 30px;
   width: 40px;
   margin-top:580px;
   margin-left: 170px;
   position: absolute;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM