遇到浮动内容时,粘性导航会中断吗?

问题描述 投票:1回答:1

出于教学目的,我正在使用最基本的CSS。我有一个导航栏设置为position: sticky工作得很好UNTIL它与页面下方的一组浮动列交互。

gif of site scrolling and breaking nav

我不确定这是因为浮动,还是关于布局简单性的一些不好的计算。现在,2列布局是:

#main-menu {
  width: 100%;
  height: auto;
  text-align: right;
  position: sticky;
  position: -webkit-sticky;
  top: 0px;
  z-index: 2;
}
.column {
    width: 50%;
    float: left;
}

使用内联块显示有效并且不会影响粘性菜单,但正如预期的那样,我无法将它们设置为50%宽度。修复将是伟大的,但也解释为什么我遇到这个。

谢谢!

#main-menu {
  width: 100%;
  height: auto;
  background-color: #222222;
  line-height: 2em;
  text-align: right;
  position: sticky;
  position: -webkit-sticky; /* Safari */
  top: 0px;
  z-index: 2;
}
.site-name {
  float: left;
  margin: auto 1em;
  padding: 0.5em 1em;
}
#main-menu a {
  color: #FFFFFF;
  text-decoration: none;
}
#main-menu .site-name a:hover {
  color: #00BBBB;
}
.menu-item {
  min-width: 2em;
  text-align: center;
  display: inline-block;
  padding: 0.5em 1em;
}
.menu-item:hover {
  background-color: #00BBBB;
}

#main {
  padding: 2em 4em;
  clear: both;
}
.column-lg {
  width: 50%;
  float: left;
  
}
<h1>Site Above Fold Content</h1>

<nav id="main-menu">
  <div class="site-name">
    <a href="#">Title</a>
  </div>
  <div class="menu-item">
    <a href="#">L1</a>
  </div>
  <div class="menu-item">
    <a href="#">L2</a>
  </div>
</nav>

<h2>Under Fold Content (Before Floated Columns)</h2>
<p>Gentrify woke irony +1 tote bag lo-fi drinking vinegar. Bushwick YOLO retro pinterest cloud bread skateboard. Small batch retro twee scenester roof party humblebrag celiac 8-bit direct trade franzen flannel cray. Kogi knausgaard godard selfies umami deep v, woke whatever 8-bit prism cred.</p>

<br><br>

<h3>BYE-BYE NAV!!</h3>

<div class="column-lg">
  <h2>Lorem Ipusm</h2>
  <div class="row">
    <h3>1 Title Impsum Amet</h3>
      <p>Lorem ipsum dolor amet bitters ethical microdosing, narwhal jean shorts venmo umami YOLO 90's trust fund activated charcoal lomo pok pok hammock. Man bun marfa blog narwhal letterpress food truck. Umami forage disrupt, snackwave DIY mlkshk aesthetic kogi bitters vice.</p>
  </div>
  
  <div class="row">
    <h3>2 Title Impsum Amet</h3>
    <p>2 Vegan williamsburg jianbing, gluten-free tote bag try-hard mixtape yuccie +1 everyday carry shabby chic umami vexillologist pop-up edison bulb. Whatever everyday carry listicle, coloring book hell of microdosing gastropub banh mi yuccie tumblr art party. Aesthetic hammock kitsch microdosing, viral biodiesel tumblr cliche beard readymade seitan. Copper mug chambray street art raclette shaman fam neutra.</p>
  </div>
</div>

<div class="column-lg">
  <h2>Lorem Ipusm</h2>
   <div class="row">
    <p>Gentrify woke irony +1 tote bag lo-fi drinking vinegar. Bushwick YOLO retro pinterest cloud bread skateboard. Small batch retro twee scenester roof party humblebrag celiac 8-bit direct trade franzen flannel cray. Kogi knausgaard godard selfies umami deep v, woke whatever 8-bit prism cred. Intelligentsia heirloom keytar, hot chicken synth tote bag vaporware williamsburg pok pok kickstarter 3 wolf moon selvage hoodie trust fund cronut. Occupy bicycle rights drinking vinegar small batch, vaporware taxidermy flannel live-edge marfa.</p>
  </div>
</div>
html css css-float css-position sticky
1个回答
0
投票

浮动元素不再处于DOM的正常流动中,从而降低了身体的整体高度。使用检查器,您可以看到这个(蓝色代表身体的高度):

enter image description here

因此,当您遇到花车时,相对于身体的position: sticky将会显示为滚动。

“修复”是清除你的花车。我将以下clearfix应用于身体:

body:after {
    content: "."; 
    display: block; 
    height: 0; 
    clear: both; 
    visibility: hidden;
}

这是带演示的snippet

body:after {
    content: "."; 
    display: block; 
    height: 0; 
    clear: both; 
    visibility: hidden;
}
#main-menu {
  width: 100%;
  height: auto;
  background-color: #222222;
  line-height: 2em;
  text-align: right;
  position: sticky;
  position: -webkit-sticky; /* Safari */
  top: 0px;
  z-index: 2;
}
.site-name {
  float: left;
  margin: auto 1em;
  padding: 0.5em 1em;
}
#main-menu a {
  color: #FFFFFF;
  text-decoration: none;
}
#main-menu .site-name a:hover {
  color: #00BBBB;
}
.menu-item {
  min-width: 2em;
  text-align: center;
  display: inline-block;
  padding: 0.5em 1em;
}
.menu-item:hover {
  background-color: #00BBBB;
}

#main {
  padding: 2em 4em;
  clear: both;
}
.column-lg {
  width: 50%;
  float: left;
}
<h1>Site Above Fold Content</h1>

<nav id="main-menu">
  <div class="site-name">
    <a href="#">Title</a>
  </div>
  <div class="menu-item">
    <a href="#">L1</a>
  </div>
  <div class="menu-item">
    <a href="#">L2</a>
  </div>
</nav>

<h2>Under Fold Content (Before Floated Columns)</h2>
<p>Gentrify woke irony +1 tote bag lo-fi drinking vinegar. Bushwick YOLO retro pinterest cloud bread skateboard. Small batch retro twee scenester roof party humblebrag celiac 8-bit direct trade franzen flannel cray. Kogi knausgaard godard selfies umami deep v, woke whatever 8-bit prism cred.</p>

<br><br>

<h3>BYE-BYE NAV!!</h3>

<div class="column-lg">
  <h2>Lorem Ipusm</h2>
  <div class="row">
    <h3>1 Title Impsum Amet</h3>
      <p>Lorem ipsum dolor amet bitters ethical microdosing, narwhal jean shorts venmo umami YOLO 90's trust fund activated charcoal lomo pok pok hammock. Man bun marfa blog narwhal letterpress food truck. Umami forage disrupt, snackwave DIY mlkshk aesthetic kogi bitters vice.</p>
  </div>
  
  <div class="row">
    <h3>2 Title Impsum Amet</h3>
    <p>2 Vegan williamsburg jianbing, gluten-free tote bag try-hard mixtape yuccie +1 everyday carry shabby chic umami vexillologist pop-up edison bulb. Whatever everyday carry listicle, coloring book hell of microdosing gastropub banh mi yuccie tumblr art party. Aesthetic hammock kitsch microdosing, viral biodiesel tumblr cliche beard readymade seitan. Copper mug chambray street art raclette shaman fam neutra.</p>
  </div>
</div>

<div class="column-lg">
  <h2>Lorem Ipusm</h2>
   <div class="row">
    <p>Gentrify woke irony +1 tote bag lo-fi drinking vinegar. Bushwick YOLO retro pinterest cloud bread skateboard. Small batch retro twee scenester roof party humblebrag celiac 8-bit direct trade franzen flannel cray. Kogi knausgaard godard selfies umami deep v, woke whatever 8-bit prism cred. Intelligentsia heirloom keytar, hot chicken synth tote bag vaporware williamsburg pok pok kickstarter 3 wolf moon selvage hoodie trust fund cronut. Occupy bicycle rights drinking vinegar small batch, vaporware taxidermy flannel live-edge marfa.</p>
  </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.