【发布时间】:2021-11-10 23:23:36
【问题描述】:
我正在尝试制作一个简单的居中弹出 div 以在其中显示图像。弹出 div 应该具有图像大小,但是当图像大于页面窗口时,它会溢出其父级。我想在不使用 JS 的情况下将图像最大大小设置为窗口大小。
我正在尝试这样做,但它不起作用,有什么想法吗?
body {
color: red;
}
.popup-window {
padding : 5px;
background-color: aquamarine;
display : inline-block;
position : absolute;
top : 50%;
left : 50%;
transform : translate(-50%, -50%);
max-width : 100%;
max-height : 100%;
}
.popup-window>img {
object-fit: contain;
}
<!doctype html>
<html>
<head>
<title>This is the title of the webpage!</title>
<link rel="stylesheet" href="mystyle.css">
</head>
<body>
<div class="popup-window">
<img src="https://conpeek.com/test/przod_auta.jpg">
<!-- <img src="hubspot.png"> -->
</div>
</body>
</html>
问题已解决
图像样式应如下所示:
.popup-window>img {
max-width: 100vw;
max-height: 100vh;
}
然后当图像小于视口时,它具有自然尺寸,但是当它大于视口大小时,它会正确地适合视口。
【问题讨论】:
标签: html css image dom popupwindow