Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do You Make Items Stay Centered on a Screen no Matter the Size?

I’m trying to make these boxes horizontally centered on the screen no matter what the area of the viewport is, but I just can’t seem to do it. If anyone could help with this, I would appreciate it.

        <div class = 'jobFields'>
            <div class = 'field-1'></div>
            <div class = 'field-2'></div>
            <div class = 'field-3'></div>
            <div class = 'field-4'></div>
            <div class = 'field-5'></div>
            <div class = 'field-6'></div>
            <div class = 'field-7'></div>
            <div class = 'field-8'></div>
            <style>
                .field-1 {
                    position: absolute;
                    width: 250px;
                    height: 150px;
                    top: 400px;
                    left: 5%;
                    box-shadow: 10px 10px;
                    background-color: #0f0f0f;
                    color: #5e00bc;
                    z-index: 2;
                }

                .field-2 {
                    position: absolute;
                    width: 250px;
                    height: 150px;
                    top: 400px;
                    left: 28.6%;
                    box-shadow: 10px 10px;
                    background-color: #0f0f0f;
                    color: #5e00bc;
                    z-index: 2;
                }

                .field-3 {
                    position: absolute;
                    width: 250px;
                    height: 150px;
                    top: 400px;
                    left: 52.2%;
                    box-shadow: 10px 10px;
                    background-color: #0f0f0f;
                    color: #5e00bc;
                    z-index: 2;
                }

                .field-4 {
                    position: absolute;
                    width: 250px;
                    height: 150px;
                    top: 400px;
                    left: 75.8%;
                    box-shadow: 10px 10px;
                    background-color: #0f0f0f;
                    color: #5e00bc;
                    z-index: 2;
                }

                .field-5 {
                    position: absolute;
                    width: 250px;
                    height: 150px;
                    top: 620px;
                    left: 5%;
                    box-shadow: 10px 10px;
                    background-color: #0f0f0f;
                    color: #5e00bc;
                    z-index: 2;
                }

                .field-6 {
                    position: absolute;
                    width: 250px;
                    height: 150px;
                    top: 620px;
                    left: 28.6%;
                    box-shadow: 10px 10px;
                    background-color: #0f0f0f;
                    color: #5e00bc;
                    z-index: 2;
                }

                .field-7 {
                    position: absolute;
                    width: 250px;
                    height: 150px;
                    top: 620px;
                    left: 52.2%;
                    box-shadow: 10px 10px;
                    background-color: #0f0f0f;
                    color: #5e00bc;
                    z-index: 2;
                }

                .field-8 {
                    position: absolute;
                    width: 250px;
                    height: 150px;
                    top: 620px;
                    left: 75.8%;
                    box-shadow: 10px 10px;
                    background-color: #0f0f0f;
                    color: #5e00bc;
                    z-index: 2;
                }
            </style>
            
        </div>
    </div>
</body>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

flex would do you just right

body, html {
    height: 100%;
    margin: 0;
    display: flex;
    gap: 10px;
    justify-content: center;
    align-items: center;
}
  div {
    background-color: black;
    height: 100px;
    width: 100px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body> 
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading