【问题标题】:how to make a grid mobile responsive in react如何在反应中使网格移动响应
【发布时间】:2019-08-12 00:57:58
【问题描述】:

我必须让网格中的视频在移动设备(移动响应)中一个接一个地水平显示,但它们应该在桌面视图中保持垂直对齐。

我已经尝试为行和列自动赋值


    return (
          <div>
            <h1 className="text">trending in youtube</h1>

            <div className="grid-container">
              <div className="one">
                <YouTube
                  videoId="y6fThXQPT6I"
                  opts={opts}
                  onReady={this._onReady}
                />
              </div>
              <div className="two">
                <YouTube
                  videoId="bo_efYhYU2A"
                  opts={opts}
                  onReady={this._onReady}
                />
              </div>
              <div className="three">
                <YouTube
                  videoId="3AtDnEC4zak"
                  opts={opts}
                  onReady={this._onReady}
                />
              </div>
            </div>
          </div>
        );
      }

css 文件

      display: grid;
      grid-template-columns: repeat(auto, 1fr);
      grid-gap: 5px;
      grid-auto-rows: minmax(100px, auto);
    }

    .text {
      margin-top: 50px;
      margin-left: 40%;
    }
    .one {
      grid-column: auto;
      grid-row: 1;
    }
    .two {
      grid-column:auto;
      grid-row: 1;
    }
    .three {
      grid-column: auto;
      grid-row: 1;
    }

【问题讨论】:

    标签: css reactjs bootstrap-4


    【解决方案1】:

    当您使用bootstrap-4 标记您的帖子时,我假设您使用它。

    Bootstrap 有你需要的一切,看看网格系统:https://getbootstrap.com/docs/4.3/layout/grid/

    在您的情况下,您可以像这样使用它:

    return (
      <div className="container">
        <h1 className="text">trending in youtube</h1>
    
        <div className="row justify-content-center">
          <div className="col-xs-12 col-md-4">
            <YouTube
              videoId="y6fThXQPT6I"
              opts={opts}
              onReady={this._onReady}
            />
          </div>
          <div className="col-xs-12 col-md-4">
            <YouTube
              videoId="bo_efYhYU2A"
              opts={opts}
              onReady={this._onReady}
            />
          </div>
          <div className="col-xs-12 col-md-4">
            <YouTube
              videoId="3AtDnEC4zak"
              opts={opts}
              onReady={this._onReady}
            />
          </div>
        </div>
      </div>
    );
    

    不再需要 CSS ?

    【讨论】:

    • 不需要导入完整的库来实现它,它可以在 1 个 css 属性中完成
    【解决方案2】:

    你可以使用grid-auto-flow

       .grid-container {
          display: grid;
          grid-gap: 5px;
          grid-auto-flow: row; /* appear Vertically*/
          justify-items: center;
        }
        @media (max-width: 767px) { 
          .grid-container { 
            grid-auto-flow: column; /* appear Horizontally on mobile phones*/
          }
        }
    

    关于cssgrid-auto-flow属性的例子:当你运行sn-p它会水平显示,但是如果你全屏打开它会垂直显示。

       .grid-container {
          display: grid;
          grid-gap: 5px;
          grid-auto-flow: row;
          justify-items: center;
        }
        @media (max-width: 767px) { 
          .grid-container { 
            grid-auto-flow: column;
          }
        }
        .item {
          width: 250px;
          height: 100px;
        }
    
        .one {
          background-color: lightblue;
        }
    
        .two {
          background-color: lightpink;
        }
    
        .three {
          background-color: lightcoral;
        }
      <div class="grid-container">
          <div class="item one"></div>
          <div class="item two"></div>
          <div class="item three"></div>
        </div>

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 2018-10-17
      • 2016-06-01
      • 2018-07-15
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      相关资源
      最近更新 更多