html - 相对 DIV 宽度和高度

标签 html css image svg width

我想要一个 DIV,最大 90% 高度或 90% 宽度。我只想将图片作为背景放入 DIV 中,并且完整的 svg-image 应该是可见的。在移动设备和桌面设备上。

CSS:

.camera {
    position:absolute;
    background-image: url(../img/svg-bild.svg);
    background-repeat: no-repeat;
    background-position: center;
    max-height: 90%;
    max-width: 90%;
}

感谢您的帮助。

ps: 我用谷歌但找不到有用的东西。我知道它会在某个地方,但我只需要更少的时间来搜索 2 小时。

最佳答案

如果您希望背景图像不被裁剪并始终适合容器,您可以使用 background-size: contain; ( more info on MDN )

您还需要将 max-width/max-height 更改为 height/width 否则您的元素将具有 0 高度/宽度,因为它不包含任何内容:

.camera {
    position:absolute;
    background-image: url(../img/svg-bild.svg);
    background-repeat: no-repeat;
    background-position: center;
    background-size:contain; /** add this **/
    height: 90%;  /** change this **/
    width: 90%;  /** change this **/
}

关于html - 相对 DIV 宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26887001/

相关文章:

android - 试图将图片保存到尚未创建的特定图片文件夹

css - Bootstrap 4 带图像背景的垂直图像菜单

php - 搜索我的 sql 数据库不起作用,它返回了所有结果

html - 出现的文本和粗体选择的链接 CSS

css - 使用 span 在 H1 标签的文本旁边放置一个图标

javascript - 我怎样才能让这个 flexbox 扩展它的 flex 元素?

javascript - 为什么在 JQuery 中删除类后我的导航不调整大小?

javascript - 在js中移动到同一页面中的新部分时进行动画处理

html - 继续 div 元素越过水平溢出

php - imagepng() 和 imagegif() 中的图像透明度