我已经设置了一个弹出框来显示照片,当我点击小照片时,会在弹出框中显示大照片。弹出框具有
position: fixed;
,而弹出框内容具有position: absolute;
我可以使用margin: auto; left: 0; right: 0;
将其居中,但是宽度会延伸到最左边和最右边,我希望弹出框的宽度与其中的照片或弹出框内容相同。
我的代码:
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding: 30px;
left: 0;
top: 0;
right: 0;
bottom: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
.modal-content {
background-color: #fefefe;
position: absolute;
top: 50px;
margin-bottom: 30px;
margin: auto;
border: 1px solid #888;
}
.modalimg {
position: relative;
text-align: center;
}
.modalimg img{
max-width: 100%;
max-height: 400px;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
position: relative;
box-shadow: 0 0 20px rgba(0,0,0,0.4);
}
现在可能有点混乱,但我已经尝试了很多不同的方法,却没有成功。
transform:translate()
。 - Carl Binalla