我必须使网格中的视频在移动设备(移动设备响应)中观看时,一个接一个地水平显示,但在桌面视图中应保持垂直对齐。
我尝试自动给行和列赋值
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;
}
答案 0 :(得分: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 :(得分:0)
您可以使用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*/
}
}
关于css grid-auto-flow
属性的示例:运行该代码段时,该代码段将水平显示,但如果以全屏模式打开,则该代码段将垂直显示。
.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>