如何使用CSS网格定位元素

发布于 2025-02-01 16:14:52 字数 2948 浏览 1 评论 0原文

我是CSS网格的新手,我只是了解将网格元素定位的确切定位。我不明白的是如何在此网格元素中放置一个正在调整大小的项目。 我将这样解释:

这是我的网站的布局

,这是我的代码:

    <div class="container">
        <div class="header">Header</div>
        <div class="content1">Content1</div>
        <div class="content2">Content2</div>
        <div class="footer">footer</div>

    </div>
.container {
    width: 100vw;
    height: 100vh;

    display: grid;

    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: 80px 1fr 1fr 120px; 
    
    gap: 10px;
    padding: 10px;
    box-sizing: border-box;
}

.container div {
    padding: 10px;
    border: 1px solid #000000;
}

.header {
    grid-column: 1 / 5;
}

.content1 {
    row-gap: 200px;
    grid-column: 1 / 5;
    grid-row: 2 / 3;
}

所以现在现在我想做的是在该标头盒中一个实际的导航菜单,但我希望以某种方式仅在此盒子内,并在更改该盒子的大小时自动调整大小。现在,这就是我所做的: 第二张图片

代码:

<div class="header">
            <nav>
                <ul>
                    <li> <a text-white asp-area="" asp-page="/main">Home</a></li>
                    <li><a text-white asp-area="" asp-page="/pc">Games</a>
                       <ul>
                       <li><a text-white asp-area="" asp-page="/pc">PC</a></li>
                       <li><a text-white asp-area="" asp-page="/xbox">XBOX</a></li>
                       <li><a text-white asp-area="" asp-page="/ps">PS</a></li>
                       </ul>
                    </li>
                    <li><a text-white asp-area="" asp-page="/devices">Devices</a></li>
                    <li><a text-white asp-area="" asp-page="/pcparts">PC Parts</a></li>
                </ul>
            </nav>
        </div>
.header {
    grid-column: 1 / 5;
}


nav p {
    float: left;
    color: white;
    font-size: 20px;
    line-height: 40px;
}

nav ul {
    float: left;
    background-color: #222;
    z-index: 998;
}

nav ul li {
    float: left;
    list-style: none;
    padding: 0px 15.50px;
}

nav ul li a {
    display: block;
    font-family: arial;
    background-color: #222;
    color: #fff;
    font-size: 16px;
    padding: 26px 14px;
    text-decoration: none;
}

nav ul li ul {
    display: none;
    position: absolute;
    background-color: #222;
    outline-color: #f92525;
    padding: 0px 2px;
    padding-left: 0vh;
    left: 2px;
    width: 35px;
    border-radius: repeat (5, 4px);
}

这是错误的,我真的不知道它如何应该是。很抱歉,如果这个问题存在,但是我试图找到解决方案,而我无法通过我从互联网上得到的任何东西来解决问题,也无法理解应该如何解决问题。

I am new to css grid and I just understood how exactly positioning the grid elements works. What I don't understand is how to put an item that's resizing inside this grid element.
I will explain it this way:

This is my site's layout

and this is my code:

    <div class="container">
        <div class="header">Header</div>
        <div class="content1">Content1</div>
        <div class="content2">Content2</div>
        <div class="footer">footer</div>

    </div>
.container {
    width: 100vw;
    height: 100vh;

    display: grid;

    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: 80px 1fr 1fr 120px; 
    
    gap: 10px;
    padding: 10px;
    box-sizing: border-box;
}

.container div {
    padding: 10px;
    border: 1px solid #000000;
}

.header {
    grid-column: 1 / 5;
}

.content1 {
    row-gap: 200px;
    grid-column: 1 / 5;
    grid-row: 2 / 3;
}

So now what I would love to do is in that header box an actual navigation menu but I want somehow to be only inside this box and resize automatically when I change the size of that box. Right now this is what I have done:
2nd picture

code:

<div class="header">
            <nav>
                <ul>
                    <li> <a text-white asp-area="" asp-page="/main">Home</a></li>
                    <li><a text-white asp-area="" asp-page="/pc">Games</a>
                       <ul>
                       <li><a text-white asp-area="" asp-page="/pc">PC</a></li>
                       <li><a text-white asp-area="" asp-page="/xbox">XBOX</a></li>
                       <li><a text-white asp-area="" asp-page="/ps">PS</a></li>
                       </ul>
                    </li>
                    <li><a text-white asp-area="" asp-page="/devices">Devices</a></li>
                    <li><a text-white asp-area="" asp-page="/pcparts">PC Parts</a></li>
                </ul>
            </nav>
        </div>
.header {
    grid-column: 1 / 5;
}


nav p {
    float: left;
    color: white;
    font-size: 20px;
    line-height: 40px;
}

nav ul {
    float: left;
    background-color: #222;
    z-index: 998;
}

nav ul li {
    float: left;
    list-style: none;
    padding: 0px 15.50px;
}

nav ul li a {
    display: block;
    font-family: arial;
    background-color: #222;
    color: #fff;
    font-size: 16px;
    padding: 26px 14px;
    text-decoration: none;
}

nav ul li ul {
    display: none;
    position: absolute;
    background-color: #222;
    outline-color: #f92525;
    padding: 0px 2px;
    padding-left: 0vh;
    left: 2px;
    width: 35px;
    border-radius: repeat (5, 4px);
}

and this is so wrong that I don't really have an idea how it should be. I am sorry if this question exists but I tried to find a solution and I could not resolve the problem with anything I got from the Internet, nor understood how it should be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅窗月明清似水 2025-02-08 16:14:52

一些一般提示:

  1. 不要使用网格模板行网格模板列来定义您的站点布局。
  • 相反:如果您有侧边栏,请使用grid-template-columns,如果您已经堆叠了内容(像您一样),只需使用display:grid;彼此堆叠。
  1. 不要为标头和页脚定义静态高度
  • :处理内部元素的间距(rus>padding)以使您的容器响应。
  1. 熟悉Flexbox和更多的网格,以便轻松地开始建模您的网站,例如间隔菜单项等等。

这是这些技巧的一个示例:

* {
 margin: 0;
 padding: 0;
 box-sizing: border-box;
}

body {
 padding: 1rem;
}

.main {
 display: grid;
}


.nav, .content {
 display: flex;
 justify-content: center;
 align-items: center;
 border: 2px solid black;
}

.nav {
 padding: 2rem;
}

.content {
 padding: 5rem;
}
<div id="main">
  <header class="nav">Header</header>
  <div class="content">Content 1</div>
  <div class="content">Content 1</div>
  <footer class="nav">Footer</footer>
</div>

Some general tips:

  1. Don't define your site layout using both grid template rows and grid template columns.
  • Instead: If you have Sidebar, use grid-template-columns, if you have stacked content (like you), just use display:grid; This will force the content to stack ontop of each other.
  1. Don't define static heights for your header and footer
  • Instead: Work with the spacing of the elements inside (margin and padding) to make your container responsive.
  1. Get familiar with Flexbox and Grid a bit more to easily get started modelling your site, for example spacing out your menu items and much more.

Here is an example of those tricks:

* {
 margin: 0;
 padding: 0;
 box-sizing: border-box;
}

body {
 padding: 1rem;
}

.main {
 display: grid;
}


.nav, .content {
 display: flex;
 justify-content: center;
 align-items: center;
 border: 2px solid black;
}

.nav {
 padding: 2rem;
}

.content {
 padding: 5rem;
}
<div id="main">
  <header class="nav">Header</header>
  <div class="content">Content 1</div>
  <div class="content">Content 1</div>
  <footer class="nav">Footer</footer>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文