Home > database >  css sticky nested div container
css sticky nested div container

Time:02-04

I have simple html structure with left menu and content in right section and I want to left container submenu-2 sticked to the top after scroll to down. I use position:sticky and top:0px but it not working

Why this solutions is not working, how I can fix this?

#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;
}
<div id="big">
  <div id="left">
    <div >
      submenu 1<br/>
      <br/><br/><br/>
    </div>
    <div >
      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>

CodePudding user response:

Sticky is incompatible with floats. Use flex instead. And just in case you were wondering, flex perorms better than grid, here's a benchmark test: https://techblog.smc.it/en/2020-08-03/grid-vs-flexbox-performance

#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%;
}
<div id="big">
  <div id="left">
    <div >
      submenu 1<br/>
      <br/><br/><br/>
    </div>
    <div >
      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>

CodePudding user response:

I recommend using CSS grids and getting rid of the floats. Grids are also much better than flexbox for top-level layout as it's more rigid requires less calculation for the browser (slightly better rendering performance). Everything will work as expected, and your CSS won't look like it came from 2005 ;).

#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 */
}
<div id="big">
  <div id="left">
    <div >
      submenu 1<br/>
      <br/><br/><br/>
    </div>
    <div >
      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>

CodePudding user response:

Try this, have the main div #big as a flex box.

#big {
    display: flex;
    justify-content: space-around;
    align-items: flex-start;
}

#left {
    width: 20%;
    float: left;
    background: #f5f5f5;
    position: sticky;
    top: 0;
}
<div id="big">
  <div id="left">
    <div >
      submenu 1<br/>
      <br/><br/><br/>
    </div>
    <div >
      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>

  •  Tags:  
  • Related