gpt4 book ai didi

html - CSS 网格和语义 HTML

转载 作者:行者123 更新时间:2023-12-02 03:07:55 24 4
gpt4 key购买 nike

我正在学习 CSS 网格,在尝试布局并使用语义 html 的同时遇到了一些问题

https://codepen.io/oscarryz/pen/oNNBKyd

所以基本上我将网格设置为 3x3,左侧和右侧为空白,中间为内容

.grid-container {
display: grid;
grid-template-columns: 1fr 40em 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas:
" . header . "
" . content . "
" . footer . ";
}

.header {
grid-area: header;
}

.content {
grid-area: content;
}

.footer {
grid-area: footer;
}

.header, .content, .footer {
border: 1px solid red;
}
<div class="grid-container">
<header>
<div class="header">header</div>
</header>
<main>
<div class="content">content</div>
</main>
<footer>
<div class="footer">footer</div>
</footer>
</div>

从上面的代码笔中可以看出,这是行不通的。如果我remove the semantic tags it works ,显然必须有一个正确的方法来做到这一点

最佳答案

网格模板适用于直系后代。

语义元素应该通过标记名引用,而不是类:

/* changed from .header, which is a _child_ of header */
header {
grid-area: header;
}

/* changed from .content, which is a _child_ of main */
main {
grid-area: content;
}

/* changed from .footer, which is a _child_ of footer */
footer {
grid-area: footer;
}

更正代码笔:https://codepen.io/c_bergh/pen/eYYvOmG

关于html - CSS 网格和语义 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58508959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com