如何将固定div中的绝对div居中

Mat*_*sen 2 css

我为照片设置了一个模态,所以当我点击一张小照片时,我会在一个模态中获得一张更大的照片,该模态具有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); 

}
Run Code Online (Sandbox Code Playgroud)

它现在可能有点乱,但我尝试了很多不同的东西,没有运气..

Car*_*lla 6

这是我使用绝对定位元素时所使用的内容,这对我一直有效:

.absolute-center {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
Run Code Online (Sandbox Code Playgroud)


小智 1

.modal-content {
    background-color: #fefefe;
    position: absolute;
    top: 50px;
    left: 50px;
    right: 50px;
    bottom: 50px;
    border: 1px solid #888;
    }
Run Code Online (Sandbox Code Playgroud)