首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无论大小如何,如何使项目保持在屏幕的中心位置?

无论大小如何,如何使项目保持在屏幕的中心位置?
EN

Stack Overflow用户
提问于 2022-07-10 16:34:16
回答 1查看 37关注 0票数 -1

我试图使这些框水平中心在屏幕上,无论是什么区域的视口,但我只是不能这样做。如果有人能帮忙,我会很感激的。

代码语言:javascript
复制
        <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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-10 16:42:49

flex会对你有好处的

代码语言:javascript
复制
body, html {
    height: 100%;
    margin: 0;
    display: flex;
    gap: 10px;
    justify-content: center;
    align-items: center;
}
  div {
    background-color: black;
    height: 100px;
    width: 100px;
}
代码语言:javascript
复制
<!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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72930144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档