html - "Cover"图像不知道 block 的宽度和高度

标签 html css background size

下面是我的代码:

.background{
    margin: 0;
    padding: 0;
    width:100%;
    height:100%;
    position: absolute;
    background: url('https://dummyimage.com/600x400/000/fff') no-repeat center bottom / cover;
}

 #subimage{
    background: url("https://dummyimage.com/600x400/000/fff") no-repeat bottom / cover;

  position: absolute;
  left: 24.5%;
  top: 28.3%;
  width:??
  height: ??
} 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="css/main.css">
</head>
<body>
    <header>
            <div class="background">
                    <div id="subimage">
                    </div>
                </div>
     </header>
 </body>

我不知道图像的大小,所以我不知道 block 的大小(如果可能的话,我可以使用 img 标签,但 block 更适合我的任务)。 需要像封面一样获取“原生”图像大小,但不知道 block 的宽度和高度;

最佳答案

也许这对你有帮助。 我使用背景作为基础,没有给它任何高度,但是用 padding-bottom 做了。然后巧妙地将子图像放在它上面,是的,它在所有屏幕上都是响应式的

.background{
margin: 0;
    padding: 0;
    width: 100%;
    padding-bottom: 85%;
    position: relative;
    background: url(https://i.ibb.co/bX8rPqr/mac-computer-screen-png-apple-mac-computer-screen-png-1122.png) no-repeat center bottom / cover;
    background-size: contain;
}

 #subimage{
background: url(https://dummyimage.com/600x400/000/fff) no-repeat bottom / cover;
    position: absolute;
    background-size: cover;
    z-index: 12;
    width: 92%;
    height: 61.9%;
    left: 4.1%;
    bottom: 28.6%;
} 
    <header>
            <div class="background">
                    <div id="subimage">
                    </div>
                </div>
     </header>

关于html - "Cover"图像不知道 block 的宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57854115/

相关文章:

swift - 如何让计时器在后台运行?

html - Div 不会在 wordpress 主题中居中

javascript - html 无法加载 javascript?

google-chrome - CSS 显示 :block not working on google-chrome on windows 7

html - Chrome 现在为表格单元格的背景图像添加了右边距

css - Div 背景图像没有出现?图片来源已多次检查

javascript - CSS:是否有可能使::after 选择器表现得像一个按钮?

javascript - 显示正在加载图像直到 div 完全加载

html - CSSOM 和 DOM 创建是异步的吗?

android - Android Andengine场景添加背景