How does css make page elements scale in proportion to the size of the screen and keep them centered vertically all the time?

problem description

you want to place a picture in the vertical center of the page, with some text on top of the picture. If you want pictures and text, you can scale according to the size of the screen.

do you have any good ideas for implementation?

Mar.31,2021

place your content inside div

.div {
    position: absolute;
    top:30%;
    right: 30%;
    bottom: 30%;
    left: 30%;
}

html, body {
    width: 100%;
    height: 100%;
}
div {
    width: 20%;
    height: 20%;
    margin-top: 50%;
    transform: translateY(-50%);
}

1. The size of the picture and text can be set as a percentage or rem, and will be scaled according to the screen size.
2, is centered horizontally and vertically. For more information, please see


parent container display:flex;justify-content:center;align-content:center;


div {
    position: absolute;
    top: 0; bottom:0; left: 0; right:0;
    width: 50%; height: 50%;
    margin: auto;
    background: red;
}

good compatibility and simple style

Menu