display:flex在IE8中不支持

时间:2016-06-30 05:42:22

标签: css internet-explorer-8 flexbox

我在IE8 browser中有显示问题,因为它在整行中显示我的块。它应该像Chrome browser中那样浮动。为了在IE8中工作,需要修复什么。

这是我的代码:`                            IE                                       

    </head>

    <body>

    <div class="grid">
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
    </div>

    </body>
    </html>

     `

和css部分是`

     *, *:before, *:after {
      box-sizing: border-box;
    }

    html, body {
      width: 100%;
      height: 100%;
      font-family: Helvetica;
    }

    body {
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-pack: center;
      -webkit-justify-content: center;
          -ms-flex-pack: center;
              justify-content: center;
      -webkit-box-align: center;
      -webkit-align-items: center;
          -ms-flex-align: center;
              align-items: center;
    }

    img {
      max-width: 100%;
      height: auto;
    }

    .grid {
      width: 1024px;
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      -webkit-flex-flow: row wrap;
          -ms-flex-flow: row wrap;
              flex-flow: row wrap;
      padding: 32px;
      background-color: #ddd;
    }
    .grid:after {
      content: "";
      -webkit-box-flex: 1;
      -webkit-flex: auto;
          -ms-flex: auto;
              flex: auto;
      margin-left: -1%;
    }
    .grid .item {
      -webkit-box-flex: 1;
      -webkit-flex: 1 0 24.25%;
          -ms-flex: 1 0 24.25%;
              flex: 1 0 24.25%;
      max-width: 24.25%;
      margin-bottom: 10px;
      text-align: center;
      background-color: #bbb;
    }
    .grid .item:nth-child(4n+2), .grid .item:nth-child(4n+3), .grid 
     .item:nth-child(4n+4) {
      margin-left: 1%;
    }
    .grid .item:nth-child(4n+1):nth-last-child(-n+4), .grid .item:nth-

     child(4n+1):nth-last-child(-n+4) ~ .item {
      margin-bottom: 0;
    }
    `

1 个答案:

答案 0 :(得分:0)

我发生了,因为 ie-8 不支持flex属性,但没问题我们已经解决了这个问题。

首先使用即特定样式表 as,

<!--[if lte IE 9]>
    <link rel="stylesheet" type="text/css" href="path to file/ie.css" />
<![endif]-->

然后在 ie.css 文件中包含跟随css,

.grid {
  display:block;
  margin-left:auto;
  margin-right:auto;
}

.grid .item {
  float:left;
  width: 24.25%;
 }  

.clearfix:before,
.clearfix:after {
    content: " ";
    display: table;
}

.clearfix:after {
    clear: both; 
}    

确保在最后一个“项目”之后添加一个带有“。 clearfix ”的div,例如

<div class="grid">
  <div class="item">
    <h3>Item</h3>
  </div>
  <div class="item">
    <h3>Item</h3>
  </div>
  <div class="item">
    <h3>Item</h3>
  </div>
  <div class="item">
    <h3>Item</h3>
  </div>
  <div class="item">
    <h3>Item</h3>
  </div>
  <div class="item">
    <h3>Item</h3>
  </div>
  <div class="item">
    <h3>Item</h3>
  </div>
  <div class="item">
    <h3>Item</h3>
  </div>
  <div class="item">
    <h3>Item</h3>
  </div>
  <div class="item">
    <h3>Item</h3>
  </div>

  <div class="clearfix"></div>

</div>

它也适用于

您的完整HTML页面将是这样的,

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
*, *:before, *:after {
      box-sizing: border-box;
    }

    html, body {
      width: 100%;
      height: 100%;
      font-family: Helvetica;
    }

    body {
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-pack: center;
      -webkit-justify-content: center;
          -ms-flex-pack: center;
              justify-content: center;
      -webkit-box-align: center;
      -webkit-align-items: center;
          -ms-flex-align: center;
              align-items: center;
    }

    img {
      max-width: 100%;
      height: auto;
    }

    .grid {
      width: 1024px;
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      -webkit-flex-flow: row wrap;
          -ms-flex-flow: row wrap;
              flex-flow: row wrap;
      padding: 32px;
      background-color: #ddd;
    }
    .grid:after {
      content: "";
      -webkit-box-flex: 1;
      -webkit-flex: auto;
          -ms-flex: auto;
              flex: auto;
      margin-left: -1%;
    }
    .grid .item {
      -webkit-box-flex: 1;
      -webkit-flex: 1 0 24.25%;
          -ms-flex: 1 0 24.25%;
              flex: 1 0 24.25%;
      max-width: 24.25%;
      margin-bottom: 10px;
      text-align: center;
      background-color: #bbb;
    }
    .grid .item:nth-child(4n+2), .grid .item:nth-child(4n+3), .grid 
     .item:nth-child(4n+4) {
      margin-left: 1%;
    }
    .grid .item:nth-child(4n+1):nth-last-child(-n+4), .grid .item:nth-

     child(4n+1):nth-last-child(-n+4) ~ .item {
      margin-bottom: 0;
    }
</style>

<!--[if lte IE 9]>
    <link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->
</head>



<body>

<div class="grid">
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>
      <div class="item">
        <h3>Item</h3>
      </div>

      <div class="clearfix"></div>

    </div>
</body>
</html>