代码之家  ›  专栏  ›  技术社区  ›  Aakash Dave

div不在另一个div的中心

  •  0
  • Aakash Dave  · 技术社区  · 6 年前

    我有一个非常简单的问题。我有嵌套的div。我试图将div(.gridcontainer)放在div(.main)的中心。如果您运行下面的代码片段,您可以看到div在主div中的中心位置是完全的,它使两边的空间在水平和垂直方向上不均匀。

    我已经尝试了很多修复,甚至尝试通过js动态地将它们集中在负载上。但是,不知怎的,它并没有达到效果。

    我只希望它的大小与父对象的大小相同,但两边的间距要均匀。

    body {
        margin: 0;
        padding: 0;
    }
    
    .tops {
        position: fixed;
        background: rgba(0,0,0,0.70);
        margin: 0px;
        padding: 0px;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        min-height: 100vh;
        min-width: 100vw;
        height: auto;
        width: auto;
        overflow: hidden;
    }
    
    .top_menu {
        position: relative;
        top: 0;
        left: 0;
        right: 0;
        width: 100%;
        height: 70px;
        background-color: green;
        overflow: hidden;
    }
    
    .welcome {
        position: relative;
        top: 0;
        bottom: 0;
        left: 0;
        min-height: 100vh;
        min-width: 50%;
        height: auto;
        width: auto;
    }
    
    .welcome_message {
        font-family: 'Palanquin Dark', sans-serif;
        font-size: calc(2.5vw + 3vh);
        position: relative;
        top: 30%;
        left: 50%;
        margin: 0;
        float: left;
        color: white;
        display: inline-block;
        overflow: hidden;
        transform: translate(-50%, -30%);
    }
    
    .app_name {
        font-size: calc(1.5vw + 2vh);;
    }
    
    .gradientText {
        background: -webkit-linear-gradient(#FFA07A, #8B0000);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
    }
    
    .downloadButton{
        position: absolute;
        top: 75%;
        left:50%;
        background-color: white;
        width: 225px;
        height: 50px;
        border-radius: 25px;
        border-width: 0px;
        display: inline-block;
        overflow: hidden;
        transform: translate(-50%, -75%);
        transition: all .2s ease-in-out;
    
    }
    
    .downloadButton:hover
    {
        -webkit-transform: scale(1.1);
        -ms-transform: scale(1.1);
        transform: translate(-50%, -75%) scale(1.1);
    }
    
    
    .downloadText {
        position: relative;
        text-align: center;
        top: 50%;
        transform: translateY(-50%);
        font-size: 1.4em;
        font-weight: bold;
        color: black;
    }
    
    
    .iphone {
        position: relative;
        top: 0;
        bottom: 0;
        right: 0;
        min-height: 100vh;
        min-width: 50%;
        height: auto;
        width: auto;
    }
    
    .iXFrame {
        position: absolute;
        height: 700px;
        width: 350px;
        top: 45%;
        left: 50%;
        transform: translate(-50%, -45%);
    }
    
    .main {
        position: absolute;
        display: block;
        margin-top: 75px;
        height: auto;
        overflow: hidden;
        width: auto;
        min-width: 100vw;
        min-height: 50vh;
    
    }
    
    .gridcont{
        position : absolute;
        /*height: auto;*/
        width: auto;
        /*min-height: 95vh;*/
        min-width: 95vw;
        margin: 0;
        border-radius: 20px;
        display: inline-block;
        background-color: whitesmoke;
        /*overflow: hidden;*/
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css">
        <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/hover.css/2.3.1/css/hover-min.css">
        <link href="https://fonts.googleapis.com/css?family=Changa+One|Montserrat|Palanquin+Dark|Paytone+One|Rubik|Rubik+Mono+One" rel="stylesheet">
        <link href="https://afeld.github.io/emoji-css/emoji.css" rel="stylesheet">
    
        <section class="mains ui">
            <div
                class="tops"></div>
                <!--.top_menu-->
                <div class="ui grid marg-reset">
                    <div class="doubling two column row">
                        <div class="ui column welcome">
                            <h1 class="welcome_message"><span class="app_name">Example</span><br>we will<br>rock <br>you<br><span class="gradientText">Text</span></h1>
                            <a class="downloadButton" type="button" href="/app">
                                <div class="downloadText">Download Spotmi</div>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="main">
                    <div class="ui grid gridcont">
                        <div class="doubling two column row">
                            <div class="ui column show_text">
                                <h1 class="emotion_message">Hello world</h1>
                            </div>
                            <div class="ui column emotion_show"></div>
                        </div>
                    </div>
                </div>
        </section>

    这也对网站的响应性提出了问题。下面是两张截图。一个是hidpi笔记本电脑屏幕,另一个是iphone x模拟器。这甚至是一个问题。

    iphonex大小屏幕 . iPhoneX size screen

    笔记本电脑屏幕 . Laptop Screen

    我到底该怎么解决?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Travis Unangst    6 年前

    您是否尝试过将给定的内部div设置为css属性的中心:

    position: relative;
    top: 50%;
    left 50%;
    transform: translate(-50%, -50%);
    

    这通常解决了我的平衡问题xd