首页 > 解决方案 > 后备 CSS 网格不起作用

问题描述

建议我通过“@support”以下列方式组合 css-grid 和 css-table:支持网格的浏览器应该使用网格,不支持网格的浏览器使用默认的表格样式。

现在看来,我的网格布局无法建立,即使 ff 正在响应@support(例如,两个链接之一,谁应该在网格模式中更改)。有人知道我的代码有什么问题吗?

这是我仍然得到的

[默认表回退] ( https://i.stack.imgur.com/bfQNl.jpg )

这就是我想要的

[所需网格布局] ( https://i.stack.imgur.com/C5v4v.jpg )

标签: htmlcss

解决方案


.wrapper-of-wrapper { display: none;}在开始之前添加@supports。我认为这会帮助你:

html {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  display: table;
  table-layout: fixed;
  position: relative;
}

body {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

@media only screen and (max-width: 600px) {
  /*some definitions */
}

.grid-G-and-H {
  display: none;
  max-height: 0;
  overflow: hidden;
  mso-hide: all;
}

.wrapper-of-wrapper {
  display: none;
}

/*- - - - - - - - @supports - - - - - - - - - - - - */

@supports (display: grid) {
  html {
    box-sizing: border-box;
  }
  .wrapper-of-wrapper {
    position: fixed;
    width: 100%;
    height: 100%;
    display: flex;
    flex-flow: column nowrap;
    justify-content: center;
    bottom: 0;
  }
  .wrapper {
    height: auto;
    display: grid;
    -ms-grid;
    -ms-grid-columns: 16% 16% 16%;
    -ms-grid-rows: 10% 10% 10%;
    flex: 0;
    grid-column-gap: 7px;
    grid-row-gap: 7px;
    grid-template-columns: 16% 16% 16%;
    justify-content: center;
    /*horizontal*/
    align-content: center;
    /*vertical*/
    grid-auto-flow: row dense;
  }
  .box {
    justify-content: center;
    align-items: center;
    background: grey;
  }
  img {
    max-width: 90%;
    height: auto;
    margin: 5px;
  }
  .a {
    grid-column: 1 / span 2;
    grid-row: 1 / span 2;
    border: 5px solid red;
  }
  .b {
    grid-column: 3;
    grid-row: 1;
  }
  .c {
    grid-column: 3;
    grid-row: 2;
  }
  .d {
    grid-column: 1;
    grid-row: 3;
  }
  .e {
    grid-column: 2;
    grid-row: 3;
  }
  .f {
    grid-column: 3;
    grid-row: 3;
  }
  .grid-G-and-H {
    display: block;
  }
  @media only screen and (max-width: 600px) {
    /*some definitions*/
  }
}
<div id="G">
  <!--absolute positioned-->
  <a href="link.html"><img style="margin: auto; width: 23px; height: 27px; left: 20px; position: absolute; top: 0pt; bottom: 0pt;" src="http://via.placeholder.com/23x27"></a>
</div>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<div>
  <a href="link.html"><img style="max-width: 600px; max-hight: 400px; width: 65%; height: auto; border: 5px solid red" src="http://via.placeholder.com/600x400"></a>
</div>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<div id="H">
  <!--absolute positioned-->
  <a href="link.html"><img style="margin: auto; width: 23px; height: 27px; right: 20px; position: absolute; top: 0pt; bottom: 0pt;" src="http://via.placeholder.com/23x27"></a>
</div>
<!-- ## END OF DIVS FOR TABLE###### -->
<!-- ## BEGINNING OF DIVS FOR GRID ## -->
<div class="grid-G-and-H">
  <div id="G_grid">
    <a href="changed-link.html"><img style="margin: auto; width: 23px; height: 27px; left: 20px; position: absolute; top: 0pt; bottom: 0pt;" src="http://via.placeholder.com/23x27"></a>
  </div>
</div>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<div class="wrapper-of-wrapper">
  <div class="wrapper">
    <div class="box a" id="A">
      <img style="color: red" src="http://via.placeholder.com/350x200">
    </div>
    <div class="box b" id="B">
      <img src="http://via.placeholder.com/170x100">
    </div>
    <div class="box c" id="C">
      <img src="http://via.placeholder.com/170x100">
    </div>
    <div class="box d" id="D">
      <img src="http://via.placeholder.com/170x100">
    </div>
    <div class="box e" id="E">
      <img src="http://via.placeholder.com/170x100">
    </div>
    <div class="box f" id="F">
      <img src="http://via.placeholder.com/170x100">
    </div>
  </div>
</div>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<div class="grid-G-and-H">
  <div id="H_grid">
    <a href="changed-link.html"><img style="margin: auto; width: 23px; height: 27px; left: 20px; position: absolute; top: 0pt; bottom: 0pt;" src="http://via.placeholder.com/23x27"></a>
  </div>
</div>


推荐阅读