CSS sticky inner container within a parent div

I am currently facing an issue with my HTML layout where I have a left menu and content in the right section. I want to make the submenu-2 within the left container stick to the top after scrolling down. I tried using position:sticky and top:0px but it doesn't seem to be working.

Why isn't this solution working and how can I fix it?

#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 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>

Answer №1

When it comes to designing layouts with sticky elements, it's important to note that using floats can lead to compatibility issues. Instead, consider using flex for a more reliable result. And if you're curious about performance comparisons between flex and grid, check out this benchmark test:

#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 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>

Answer №2

I highly recommend utilizing CSS grids instead of relying on floats. Grids offer a more structured approach and are superior to flexbox for top-level layouts as they require less browser calculation, resulting in slightly better rendering performance. By following this method, your layout will function as intended without appearing outdated like it's stuck in the early 2000s ;).

#big { 
  /* include the following */
  display: grid;
  gap: 5%;
  grid-template-columns: 20% 70%;
}

#left {
  /* delete float and width properties */
  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 settings */
}
<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>

Answer №3

One suggestion is to use 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 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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for utilizing the div tag in a similar manner as a table

How can I position the div tag to resemble a table in the correct order? <html> <head><title> .</title> <link rel="stylesheet" type="text/css" href="style/main.css"/> <script type="text/javascript" src="script/ajax.js"& ...

What is the method for automatically scrolling a WebView HTML document?

I am seeking a way to implement autoscroll on my webpage. Here is the solution I have come up with: public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCon ...

CSS fails to function properly on websites hosted on nginx servers

During the process of creating a static website, I utilized VS Code and Live Server on my Windows machine. The site appeared to function properly. However, upon transferring the files to my CentOS machine to run the site through nginx, the site loaded cor ...

Adjust the position of the background when hovering with the mouse to create a right

Currently, I am implementing a background perspective change with the use of mousemove. However, an issue arises with the right margin of the image after moving it. Everything looks fine initially. Any suggestions on how to prevent this problem? https:/ ...

Text encoded in UTF-8 emerges from the surrounding frame

As someone who is relatively new to frontend development, I recently created a blog using a template that utilizes Next.js, Tailwind, and React. However, when I input UTF-8 text in Korean, the text overflows outside of the parent frame on mobile devices. ...

Tips for designing a tilted CSS layout from scratch

I am looking to design a stylish interface using HTML & CSS. Here is the style I want to achieve: Currently, my interface looks like this: Although these are just reference images, you can see that my design does not have the same sleekness as the one in ...

Testing with Selenium to confirm the presence of a specific CSS attribute within the style attribute

I am looking to create a Selenium IDE test that will confirm the value of the bottom attribute in the "style" section below. How can I go about writing a test for this considering: The Xpath to the element is: xpath=/html/body/div/div[3]/div[2]/div/div/di ...

What is the best way to alter the text color based on certain conditions within a table column using PHP

I am faced with multiple conditions involving if-else statements, where the variable "Remark" changes based on these conditions. if($income==($paid + $deduction)){ $remark="Paid"; } else if($paid > 0){ $remark="Have Due"; } else{ $remark="N ...

Utilizing Java to Store HTML Content in MySQL

Currently, I am immersed in a project that requires storing webpages in a database. To accomplish this task, I am utilizing crawler4j for crawling, along with Proxool and MySQL Java Connector to establish connections with my database. During testing, an e ...

Is there a way to retrieve the content of a WordPress page minus the HTML tags?

$content = fetch_content(); var_dump($content); <figure class="wp-block-gallery columns-1 is-cropped"><ul class="blocks-gallery-grid"><li class="blocks-gallery-item"><figure><img src="http://www ...

Ensuring DIV Fills without Compromising Aspect Ratio

I am currently attempting to fill a div with an image in both width and height. However, I have only been able to control the width while the height respects the aspect ratio without filling the div completely. I have tried various combinations of backgrou ...

Utilize the div element to segment a webpage into four different sections

Within a div tag, I have used 4 other div tags to create equal areas. Is there an alternative method to achieve this division or improve it? <div style="width: 689px; margin-left: 215px; margin-top: 0px; float: none; height: 502px;"> ...

The random sorting algorithm in the query_posts function is disrupting the formatting of the CSS

<?php query_posts($query_string . '&orderby=rand') ?> Hello everyone, I recently added the code snippet above to my portfolio page template.php. You can view it at However, I've noticed that sometimes the CSS breaks when you rel ...

Secure access to an API using a certificate within a Vue.js application running on localhost

My vue.js app is built using vue-cli. The application is hosted at dev.example.com and the REST API can be found at dev.example.com/api/v1/. The backend has added an SSL certificate for security on the development environment. However, when I try to make a ...

Could someone explain why Bootstrap columns are positioned in the same row but stacked on top of each other?

In the HTML structure I have provided below, you can see that there is a nested row inside another row, and within that inner row, there are two more columns (trying to split col-9 into more columns). However, the issue is that instead of appearing next t ...

Is using CSS as an HTML attribute considered poor practice?

I've been noticing an increasing trend of combining HTML, CSS, and JavaScript together in web development. However, I was taught to keep them separate with HTML linking to the sources/scripts. While using the style attribute in HTML is sometimes acce ...

Develop a custom dropdown menu using JavaScript

I've been working on creating a dropdown menu that appears after selecting an option from another dropdown menu. Here's the HTML code I'm using: <br> <select id ="select-container" onchange="addSelect('select-container') ...

Creating a form with required fields in Angular and using the ngIf directive

Update: modified the sample code to incorporate TypeScript for better clarity I have a form with various buttons for users to choose from. The submit button is initially disabled until a user selects a button. However, there's a unique requirement wh ...

The CSS styling does not seem to be affecting the text button's appearance

I am currently creating a website using Bootstrap 5, and I'm facing an issue with making the header image responsive on mobile devices. While it looks great on desktops, laptops, and tablets, on mobile screens, the text and button alignment is off. I& ...

Difficulty arises when trying to engage with the addition span within a span using jQuery

Looking for some assistance here! Currently in the process of developing a mail system and focusing on creating a list of receivers. I am working on adding user names to a span element, with the goal of being able to remove users by clicking their name. & ...