适当的边距填充,用于更宽的宽度的响应式网格设计

时间:2019-07-04 19:46:47

标签: html css media-queries css-grid

我一直"working with one of the many frameworks or grid systems laying out sites on a 12-column flexible grid",自从使用Bootstrap数月以来,我既不是行家也不是学徒,我正在尝试仅使用网格和媒体查询来布局网站。下面的代码是简化的设计,它是通过精美的教程在刚刚引用的URL上提供的,还使用了Rachel Andrew启发性的https://gridbyexample.com的提示。

.box {
    background-color: #ddd; color: #000; padding: 20px;
}
.main-head { grid-area: hd; }
.content { grid-area: ct; }
.main-nav { grid-area: nv; }
.wrapper {
    display: grid;
    grid-gap: 2px;
    grid-template-areas: 
        "nv"
        "ct";
}
@media (min-width: 600px) {
    .wrapper {
        grid-template-columns: 1fr 3fr;
        grid-template-areas: 
            "nv nv"
            "ct ct";
    }
    nav ul {
        display: flex;
        justify-content: space-between;
    }
}        
@media (min-width: 1000px) {
    .wrapper {
        grid-template-columns: 1fr 4fr 1fr;
        grid-template-areas: 
            "nv ct ct"
            "nv ct ct";
    }
    nav ul {
        flex-direction: column;
    }
}
@media (min-width: 1200px) {
    .wrapper {
        grid-template-columns: 1fr 1fr 4fr 1fr 1fr;
        grid-template-areas: 
            ". nv ct ct ."
            ". nv ct ct .";
    }
    nav ul {
        flex-direction: column;
    }
}
<div class="wrapper">
    <nav class="box main-nav">
        <ul>
            <li><a href="">Nav 1</a></li>
            <li><a href="">Nav 2</a></li>
            <li><a href="">Nav 3</a></li>
        </ul>
    </nav>
    <article class="box content">
        <h1>Main</h1>
    </article> 
</div>

上面的代码尝试模拟当窗口超过某个(1200)宽度时插入两个侧边距,但是使用填充grid-template-area的时间段似乎是作弊行为。有没有更自然的方法来插入这些边距?

1 个答案:

答案 0 :(得分:0)

您可以像在其他任何地方一样使用margin,不是吗?当然,如果您希望两侧的空间恰好为1fr,则可以使用.

的空单元格方法

.box {
    background-color: #ddd; color: #000; padding: 20px;
}
.main-head { grid-area: hd; }
.content { grid-area: ct; }
.main-nav { grid-area: nv; }
.wrapper {
    display: grid;
    grid-gap: 2px;
    grid-template-areas: 
        "nv"
        "ct";
    transition: margin 0.5s ease-in-out;
}
@media (min-width: 600px) {
    .wrapper {
        grid-template-columns: 1fr 3fr;
        grid-template-areas: 
            "nv nv"
            "ct ct";
    }
    nav ul {
        display: flex;
        justify-content: space-between;
    }
}        
@media (min-width: 1000px) {
    .wrapper {
        grid-template-columns: 1fr 4fr 1fr;
        grid-template-areas: 
            "nv ct ct"
            "nv ct ct";
    }
    nav ul {
        flex-direction: column;
    }
}
@media (min-width: 1200px) {
    .wrapper {
        grid-template-columns: 1fr 4fr 1fr;
        grid-template-areas: 
            "nv ct ct"
            "nv ct ct";
        margin: 0 5%;
    }
    nav ul {
        flex-direction: column;
    }
}
<div class="wrapper">
    <nav class="box main-nav">
        <ul>
            <li><a href="">Nav 1</a></li>
            <li><a href="">Nav 2</a></li>
            <li><a href="">Nav 3</a></li>
        </ul>
    </nav>
    <article class="box content">
        <h1>Main</h1>
    </article> 
</div>