首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >css粘性嵌套div容器

css粘性嵌套div容器
EN

Stack Overflow用户
提问于 2022-02-03 19:22:37
回答 3查看 74关注 0票数 2

我有一个简单的html结构,左侧菜单和内容在右边部分,我想左容器子菜单-2粘贴到顶部后滚动下来。我使用的位置:粘性和顶部:0px,但它不能工作。

为什么这个解决方案不起作用,我怎么才能解决这个问题?

代码语言:javascript
运行
复制
#big {}

#left {
  width: 20%;
  float: left;
  background: #f5f5f5;
}

#left .submenu-1 {
  border: 1px solid #8b8b8b;
}

#left .submenu-2 {
  border: 1px solid #8b8b8b;
  top: 0px;
  position: sticky;
}

#content {
  width: 70%;
  float: left;
}
代码语言:javascript
运行
复制
<div id="big">
  <div id="left">
    <div class="submenu-1">
      submenu 1<br/>
      <br/><br/><br/>
    </div>
    <div class="submenu-2">
      submenu 2<br/>
    </div>
  </div>
  <div id="content">
    What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
    specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
    with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it?
    <br/><br/> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using
    'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
    Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    <br/><br/> Where does it come from?
    <br/><br/> Where does it come from?
  </div>
</div>

EN

回答 3

Stack Overflow用户

发布于 2022-02-03 20:00:15

粘性与浮子不相容。使用flex代替。如果您想知道,flex比网格更好,下面是一个基准测试:https://techblog.smc.it/en/2020-08-03/grid-vs-flexbox-performance

代码语言:javascript
运行
复制
#big {
  display: flex;
}

#left {
  width: 20%;
  top: 0px;
  background: #f5f5f5;
}

#left .submenu-1 {
  border: 1px solid #8b8b8b;
}

.submenu-2 {
  border: 1px solid #8b8b8b;
  top: 0px;
  position: sticky;
}

#content {
  width: 70%;
}
代码语言:javascript
运行
复制
<div id="big">
  <div id="left">
    <div class="submenu-1">
      submenu 1<br/>
      <br/><br/><br/>
    </div>
    <div class="submenu-2">
      submenu 2<br/>
    </div>
  </div>
  <div id="content">
    What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
    specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
    with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it?
    <br/><br/> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using
    'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
    Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    <br/><br/> Where does it come from?
    <br/><br/> Where does it come from?
    <br/><br/> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using
    'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
    Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    <br/><br/> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using
    'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
    Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    <br/><br/> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using
    'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
    Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    <br/><br/> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using
    'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
    Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
  </div>
</div>

票数 2
EN

Stack Overflow用户

发布于 2022-02-03 20:01:49

我建议使用CSS网格和摆脱浮标。对于顶层布局,网格也比柔性盒要好得多,因为它更严格,对浏览器的计算更少(渲染性能稍好一些)。一切都会像预期的那样工作,您的CSS看起来不会像2005年那样;)。

代码语言:javascript
运行
复制
#big { 
  /* add this */
  display: grid;
  gap: 5%;
  grid-template-columns: 20% 70%;
}

#left {
  /* remove float and width */
  background: #f5f5f5;
}

#left .submenu-1 {
  border: 1px solid #8b8b8b;
}

#left .submenu-2 {
  border: 1px solid #8b8b8b;
  top: 0px;
  position: sticky;
}

#content {
  /* remove float and width */
}
代码语言:javascript
运行
复制
<div id="big">
  <div id="left">
    <div class="submenu-1">
      submenu 1<br/>
      <br/><br/><br/>
    </div>
    <div class="submenu-2">
      submenu 2<br/>
    </div>
  </div>
  <div id="content">
    What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
    specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
    with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it?
    <br/><br/> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using
    'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
    Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    <br/><br/> Where does it come from?
    <br/><br/> Where does it come from?
  </div>
</div>

票数 1
EN

Stack Overflow用户

发布于 2022-02-03 20:31:19

代码语言:javascript
运行
复制
Try this, have the main div #big as a flex box.

代码语言:javascript
运行
复制
#big {
    display: flex;
    justify-content: space-around;
    align-items: flex-start;
}

#left {
    width: 20%;
    float: left;
    background: #f5f5f5;
    position: sticky;
    top: 0;
}
代码语言:javascript
运行
复制
<div id="big">
  <div id="left">
    <div class="submenu-1">
      submenu 1<br/>
      <br/><br/><br/>
    </div>
    <div class="submenu-2">
      submenu 2<br/>
    </div>
  </div>
  <div id="content">
    What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
    specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
    with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it?
    <br/><br/> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using
    'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
    Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    <br/><br/> Where does it come from?
    <br/><br/> Where does it come from?
  </div>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70977116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档