我有一个简单的CSS网格。
我希望在移动视图(800px宽度)上将#right
和main
元素放在彼此之下。
我想加上这个:
main,
#right {
grid-column: 2 / span 2;
}
会将main
和#right
元素放在彼此之下,但没有这样的运气。它们似乎都有100%的高度导致它们重叠。
所以我希望看到的是:
main {
margin-top: 90%;
height: 50%;
}
#right {
height: 50%;
}
但当然由CSS网格引起。
https://plnkr.co/edit/J0gZRP9LUak4n6eebnhh?p=preview
body,
html {
height: 100%;
}
#grid-wrapper {
height: 100vh;
display: grid;
grid-template: "header header header" 66px "subheader subheader subheader" 55px "left main right" auto / 18.75% auto 25%;
}
header {
grid-area: header;
display: grid;
grid-template: "logo searchbar hamburgermenu" auto / 18.75% auto 25%;
}
header #logo {
grid-area: logo;
background: red;
}
header #searchbar {
grid-area: searchbar;
background: yellow;
}
header #hamburgermenu {
grid-area: hamburgermenu;
background: teal;
}
.subheader {
grid-area: subheader;
display: grid;
grid-template: ". telephone-content ." auto / 18.75% auto 25%;
background: wheat;
}
.subheader .telephone-content {
grid-area: telephone-content;
background: lightcoral;
}
#left {
grid-area: left;
background: darkgray;
}
main {
grid-area: main;
background: lightskyblue;
}
#right {
grid-area: right;
background: lightsalmon;
}
/* TABLET VIEW */
@media screen and (max-width: 1024px) {
header {
grid-template: "logo searchbar hamburgermenu" auto / 6.75% auto 25%;
}
.subheader {
grid-template: ". telephone-content ." auto / 6.75% auto 25%;
}
#grid-wrapper {
grid-template: "header header header" 66px "subheader subheader subheader" 55px "left main right" auto / 6.75% auto 25%;
}
}
/* MOBILE VIEW */
@media screen and (max-width: 800px) {
header {
grid-template: "logo searchbar hamburgermenu" auto / 66px auto 66px;
}
.subheader {
grid-template: "telephone-content telephone-content telephone-content" auto / 66px auto 66px;
}
#grid-wrapper {
grid-template: "header header header" 66px "subheader subheader subheader" 55px "left main right" auto / 6.75% auto 25%;
}
#left {
/* grid-row: 1 / span 2; */
}
#right {
/* grid-column: 2 / 3; */
}
main,
#right {
grid-column: 2 / span 2;
}
}

<div id="grid-wrapper">
<header>
<div id="logo">
<h1>Logo</h1>
</div>
<div id="searchbar">
<p>Header stuff</p>
</div>
<div id="hamburgermenu">
<h1>...</h1>
</div>
</header>
<div class="subheader">
<p class="telephone-content">Dit is telefoon informatie</p>
</div>
<aside id="left">
<h1>Left</h1>
</aside>
<main>
<h1>Content</h1>
<p>Saaie boel</p>
</main>
<aside id="right">
<h1>Right</h1>
</aside>
</div>
</div>
&#13;
答案 0 :(得分:1)
这是您在移动视图中的内容:
@media screen and (max-width: 800px) {
#grid-wrapper {
grid-template: "header header header" 66px
"subheader subheader subheader" 55px
"left main right" auto / 6.75% auto 25%;
}
此布局产生三行三列。
但您希望right
显示在main
下方(反之亦然?)。所以试试这个:
@media screen and (max-width: 800px) {
#grid-wrapper {
grid-template: "header header header" 66px
"subheader subheader subheader" 55px
"left main main" auto /* adjustment */
"left right right" auto /* new row */
/ 6.75% auto 25%;
}