【问题标题】:How to centre and make responsive a row of divs如何使一排 div 居中并做出响应
【发布时间】:2014-12-14 06:00:20
【问题描述】:

http://jsfiddle.net/raLmo8kd/

您好,我有一个页面,我想让方块垂直和水平地出现在页面中间。我有一个容器,我希望大约 80% 的页面在两边留出 20% 的空间。我想知道如何才能使所有浏览器和设备都完全响应。

HTML

<div class="splashcontainer">
    <h1 class="splashheader">Title</h1>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

CSS

.splashcontainer{
margin: 0 auto;
width: 80%;
height: 100%;
min-height: 100%;
}

.splashheader{
text-align: center;
font-size: 62px;
font-family: font-family: 'Montserrat', sans-serif;
}

.splashcontainer div{
width: 20%;
margin: 0px 2% 0 2%;
padding-bottom: 20%;
background-color: deepskyblue;
display: inline-block;
}

谢谢,

【问题讨论】:

    标签: html css responsive-design


    【解决方案1】:

    您可以使用 3 行 CSS vertical align 框。

    .splashcontainer {
      position: relative;
      top: 50%;
      transform: translateY(-50%);
    }
    

    接下来确保 &lt;html&gt;&lt;body&gt; 元素的高度为 100%。

    最后,您需要使用 &lt;!-- --&gt; 处理 div 之间的空格。

    See the fiddle

    HTML

    <div class="splashcontainer">
        <h1 class="splashheader">Title</h1>
        <div></div><!--
     --><div></div><!--
     --><div></div><!--
    --><div></div>
    </div>
    

    CSS

    html {
        height: 100%;
    }
    body {
        min-height: 100%;
        height: 1px;
        position: relative;
        margin: 0;
    }
    
    .splashcontainer{
        position: relative;
        top: 50%;
        -webkit-transform: translateY(-50%);
        -ms-transform: translateY(-50%);
        transform: translateY(-50%);
        margin: 0 auto;
        width: 80%;
    }
    
    .splashheader{
        text-align: center;
        font-size: 62px;
        font-family: font-family: 'Montserrat', sans-serif;
        margin-top: 0;
    }
    
    .splashcontainer div{
        width: 20%;
        margin: 0px 2% 0 2%;
        padding-bottom: 20%;
        background-color: deepskyblue;
        display: inline-block;
    }
    

    【讨论】:

      【解决方案2】:

      您还可以使用生成的元素,vertical-aligndisplay:inline-block;

      DEMO

      body:before , html, body {
          height:100%;
          margin:0;
      }
      body {
          text-align:center;
      }
      body:before {
          content:'';
          display:inline-block;
          vertical-align:middle;
      }
      .splashcontainer{
          display:inline-block;
          vertical-align:middle;
          width: 80%;
          background:gray;
      }
      
      .splashheader{
          text-align: center;
          font-size: 62px;
          font-family: font-family: 'Montserrat', sans-serif;
      }
      
      .splashcontainer div{
          width: 20%;
          margin: 0px 2% 0 2%;
          padding-bottom: 20%;
          background-color: deepskyblue;
          display: inline-block;
      }
      

      display:flex;heightmargin:auto;

      DEMO 2

      html, body {
          height:100%;
      }
      body {
          display:flex;
      }
      
      .splashcontainer{
          margin:auto;
          width: 80%;
          background:gray;
      }
      
      .splashheader{
          text-align: center;
          font-size: 62px;
          font-family: font-family: 'Montserrat', sans-serif;
      }
      
      .splashcontainer div{
          width: 20%;
          margin: 0px 2% 0 2%;
          padding-bottom: 20%;
          background-color: deepskyblue;
      

      显示:内联块; }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-25
        • 2013-10-06
        • 2021-12-14
        • 1970-01-01
        • 2021-11-29
        • 2017-07-22
        • 2018-05-03
        • 2017-04-01
        相关资源
        最近更新 更多