Li在移动设备上不会叠放在一起。

huangapple go评论55阅读模式
英文:

Li will not stack above each other on mobile

问题

以下是您要翻译的内容:

"所以我正在创建我的应用程序的移动版本,而这个移动版本的难点在于响应性。我将发布我想要的图片以及我得到的图片。

我想要的:
Li在移动设备上不会叠放在一起。

我得到的:
Li在移动设备上不会叠放在一起。

正如您所见,这是 ul(无序列表):
Li在移动设备上不会叠放在一起。

正如您所见,这些是 li(列表项)(它们都在同一个位置):
Li在移动设备上不会叠放在一起。

我将展示我的代码,希望有人可以告诉我发生了什么。

HTML:

<body>
  <header class="header">
    <nav class="navigation">
      <div class="logo">
        <img src="Images/logo.svg">
      </div>
      <ul class="nav-items">
        <li class="nav-items nav-li"><a href=#>Home</a></li>
        <li class="nav-items nav-li"><a href=#>About</a></li>
        <li class="nav-items nav-li"><a href=#>Contact</a></li>
        <li class="nav-items nav-li"><a href=#>Blog</a></li>
        <li class="nav-items nav-li"><a href=#>Careers</a></li>
      </ul>
      <a id="invite" class="btn-nav" href="#">
        <button class="btn">Request Invite</button>
      </a>
    </nav>
  </header>
  <!-- 我有其他代码,但我认为它们不重要。 -->
</body>

CSS:(我认为重要的部分)

body {
    width: 100%;
    height: 100%;
    position: relative;
    height: 100vh;
    width: 100vw;
}
.mobileNav {
    position: absolute;
    color: rgb(150, 152, 166, 0.85);
    bottom: -5rem;
    left: 0%;
    display: block;
    width: 100%;
    min-height: 100%
}
.nav-items-mobile {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    height: 100%;
}
.nav-li {
    padding: 0 1rem 0 1rem;
    height: 100%;
    cursor: pointer;
}

我确实有 JavaScript 代码,但我认为它不需要,因为这些只是 CSS 类。我认为如果我能正确堆叠这些 li,则问题将得到解决。

谢谢您!

英文:

So I am creating a mobile version of my app, and the tricky part about this mobile is the responsiveness. I'll post a picture of what I want and a picture of what I am getting.

What I want:

Li在移动设备上不会叠放在一起。

What I am getting:
Li在移动设备上不会叠放在一起。

As you can see, this is the ul
Li在移动设备上不会叠放在一起。

As you can see, these are the lis (they're all in the same place)
Li在移动设备上不会叠放在一起。

I'll show you my code, and hopefully someone can tell me what is going on.

HTML:

&lt;body&gt;
&lt;header class=&quot;header&quot;&gt;
  &lt;nav class=&quot;navigation&quot;&gt;
    &lt;div class=&quot;logo&quot;&gt;
      &lt;img src=&quot;Images/logo.svg&quot;&gt;
    &lt;/div&gt;
    &lt;ul class=&quot;nav-items&quot;&gt;
      &lt;li class=&quot;nav-items nav-li&quot;&gt;&lt;a href=#&gt;Home&lt;/a&gt;&lt;/li&gt;
      &lt;li class=&quot;nav-items nav-li&quot;&gt;&lt;a href=#&gt;About&lt;/a&gt;&lt;/li&gt;
      &lt;li class=&quot;nav-items nav-li&quot;&gt;&lt;a href=#&gt;Contact&lt;/a&gt;&lt;/li&gt;
      &lt;li class=&quot;nav-items nav-li&quot;&gt;&lt;a href=#&gt;Blog&lt;/a&gt;&lt;/li&gt;
      &lt;li class=&quot;nav-items nav-li&quot;&gt;&lt;a href=#&gt;Careers&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;a id=&quot;invite&quot; class=&quot;btn-nav&quot; href=&quot;#&quot;&gt;
      &lt;button class=&quot;btn&quot;&gt;Request Invite&lt;/button&gt;
    &lt;/a&gt;
  &lt;/nav&gt;
&lt;/header&gt;
&lt;!-- I have other code below that I don&#39;t think is important. --&gt;
&lt;/body&gt;

CSS: (What I think is important)

body {
    width: 100%;
    height: 100%;
    position: relative;
    height: 100vh;
    width: 100vw;
}
.mobileNav {
    position: absolute;
    color: rgb(150, 152, 166, 0.85);
    bottom: -5rem;
    left: 0%;
    display: block;
    width: 100%;
    min-height: 100%
}
.nav-items-mobile {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    height: 100%;
}
.nav-li {
    padding: 0 1rem 0 1rem;
    height: 100%;
    cursor: pointer;
}

I do have JS code, but I believe it's not needed because these are just CSS classes. I believe if I can just stack the lis properly, then the problem will be solved.

Thank you

答案1

得分: 1

你的 position 属性在这个上下文中没有意义。

来自 mdn web docs

absolute

元素从正常文档流中移除,页面布局中不会为元素创建空间。元素相对于其最近的包含块进行定位。其最终位置由 top、right、bottom 和 left 的值确定。

当 z-index 的值不为 auto 时,此值会创建一个新的堆叠上下文。绝对定位框的外边距不会与其他外边距合并。

因此,你的 li 元素会堆叠在彼此之上,因为它们相对于父元素定位,忽略了前后的元素。

你可能还想重新考虑你的 min-height 属性。将其设置为 100% 意味着你的 &lt;li&gt; 的高度至少与父元素一样高,这在你有多个 &lt;li&gt; 的情况下是没有意义的。对于 height 属性也是一样。

在没有这些属性的情况下,这是你的页面:
Li在移动设备上不会叠放在一起。

英文:

Your position property doesn't make sense in this context.

From mdn web docs
> absolute
>
> The element is removed from the normal document flow, and no space is created for the element in the page layout. The element is positioned relative to its closest containing block. Its final position is determined by the values of top, right, bottom, and left.
>
> This value creates a new stacking context when the value of z-index is not auto. The margins of absolutely positioned boxes do not collapse with other margins.

So your li elements stack on top of each other because they are positioned with respect to the parent element, ignoring the elements before and after.

You might also want to reconsider your min-height property. Setting it to 100% means that your &lt;li&gt; will have height at least as high as the parent element, which doesn't make sense since you have multiple &lt;li&gt;. Same goes for your height property.

Here's your page without these properties:
Li在移动设备上不会叠放在一起。

huangapple
  • 本文由 发表于 2023年7月11日 09:46:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定