The problem of vertical centering of pictures of unknown height?

how to set a picture to center vertically in the browser and scroll up and down when the picture is out of the screen

 <div class="file-wrapper">
       <img class="file-position" src="1.jpg" alt="">
 </div>
.file-wrapper{
        width: 100%;
        height: 100%;
        background: -sharp0e0e0e;
        position: relative;
    }
.file-position{
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
    }

now it can be centered vertically, but when the picture is too large, it will be intercepted and cannot scroll up and down automatically

Mar.30,2022

just add overflow:auto to wrapper:

.file-wrapper{
    ...
    overflow: auto;
}

add relative alignment to the parent,
picture on the following style
position: absolute;
left: 50%;
top: 50%;
transform: translate (- 50% lemon 50%);


flex


  

Scheme:
1, use flex layout
2, add onload event to the picture, calculate the picture height after the picture is loaded successfully, determine whether it is larger than the outer container height, if it is larger, remove display: flex; / p >

Menu