What is the best way to ensure my paragraph remains fixed in position and doesn't scroll down? The solution lies in nesting scrolls

I am trying to create a main layout with a fixed header that will scroll its main content when necessary. However, I want to have control over how the main content scrolls itself. My goal is to keep the header and paragraph at the top, while allowing only the list of scrollable items to be scrolled. How can I achieve this without removing the div.mywrap element?

The header should remain in place as desired, but I am facing difficulties inside the main tag where the paragraph should stay on top and the rest of the content should scroll. Here is my code snippet:

*{
  box-sizing:border-box;
}

header{
  background-color: red;
  flex: 1 0 auto;
}
p{
  background-color: blue;
}

.mywrap{
    display: flex;
  flex-direction: column;
  max-height: 400px;
}

main{
    display: block;
  flex-grow: 1;
  overflow: scroll;
}

.scrollable{
  overflow: scroll;
}
<div class="mywrap">
<header>
  IM a HEADER and I ALWAYS want to be ON TOP
</header>
<main>
  <section class="aot">
    <p>
       I want this paragraph to NOT TO SCROLL DOWN and be on top as well
    </p>
  </section>
  <section class='scrollable'>
   <!-- List of scrollable items -->
  </section>
</main>
</div>

For a complete example, please refer to the code and demo here: https://jsfiddle.net/Lwgd26vt/23/

Answer №1

This is an illustrative example I have prepared:

Here, the header and paragraph are fixed at the top while only the scrollable section scrolls. The use of overflow: hidden on the main element ensures that overflow is initially hidden, while applying overflow: auto to the scrollable section allows scrolling when necessary. Additionally, setting flex-grow: 1 on the scrollable part enables it to occupy the remaining space.

* {
      box-sizing: border-box;
      padding: 0;
      margin: 0;
    }

    .mywrap {
      display: flex;
      flex-direction: column;
      height: 100vh;
    }

    header {
      background-color: red;
      flex: 0 0 auto;
      padding: 20px;
      color: #fff;
    }

    main {
      display: flex;
      flex-direction: column;
      flex-grow: 1;
      overflow: hidden;
    }

    .aot p {
      background-color: blue;
      flex: 0 0 auto;
      margin: 0;
      padding: 10px;
      color: #fff;
    }

    .scrollable {
      overflow: auto;
      flex-grow: 1;
    }

    .scrollable p {
      padding: 10px;
      border-bottom: 1px solid #ccc;
    }
<div class="mywrap">
    <header>
      I AM THE HEADER AND MY POSITION IS FIXED AT THE TOP
    </header>
    <main>
      <section class="aot">
        <p>
          This paragraph should STAY VISIBLE ON TOP WITHOUT SCROLLING
        </p>
      </section>
      <section class="scrollable">

        <p>scrollable item</p>
        <p>scrollable item</p>
        <p>scrollable item</p>
        <p>scrollable item</p>
        <p>scrollable item</p>
        <p>scrollable item</p>
        <p>scrollable item</p>
        <p>scrollable item</p>
        [Remaining scrollable items omitted for brevity]
      </section>
    </main>
  </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

What causes the odd gaps at the bottom of boxes in a Pure CSS Masonry layout?

Issue with implementing Pure CSS Masonry layout. Using position: relative for the boxes and position: absolute for the content inside each box is causing gaps below each box. Struggling to find a solution, view the code on this codepen: http://codepen.io/k ...

Arrange the current div elements by utilizing jQuery sortable within an HTML document

Here is the HTML file I am working with: <div id="sortable"><div id="div1">Item 1</div> <div id="div2">Item 2 </div></div> I have successfully applied jQuery sortable to the div with id "sortable" and was able to sort ...

To incorporate node_modules CSS into a Vue.js application that is utilizing SCSS, follow these steps

After deploying my application to AWS, I noticed that the application was rendering correctly except for the Syncfusion controls, which were not rendering correctly. Surprisingly, there were no errors shown in the Google Chrome console. On my local machine ...

Trouble arises when attempting to animate the movement of two distinct objects consecutively using jQuery

I am facing an issue with combining animations of two different objects so that the second one starts when the first one ends. I attempted to use a callback function, but it seems I have made some syntax errors that are causing jQuery to crash or behave un ...

Click to position custom text on image

Is there a way to adjust the position of text on an image after it has been clicked? I have included an image below to demonstrate how I would like it to appear: https://i.stack.imgur.com/unmoD.png function revealContent(obj) { var hiddenText = obj. ...

Guide to displaying nine images in a table format with three images in each row using PHP

How can I display nine images in a table with three images in each row using PHP? ...

What is the best way to create an index for a user-provided value in a textbox

Looking for guidance on how to set an index to user-provided values in a textbox, append them to a table in a new row, and display that index in the first column of every row. I am unfamiliar with this type of functionality. $(document).ready(function() ...

Is there a way to create a diagonal movement for an image by clicking on another HTML element?

<!DOCTYPE html> <html> <head> <meta charset="UTF-9"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(function() { $(document).re ...

Is it possible to define a multiplication margin using css?

I am trying to place a box in the center of a container, but I am unable to set a static height for the parent element as it may change. This is causing issues with aligning the box perfectly in the middle of the page. Any assistance would be greatly app ...

Incorporate Videos into HTML Dynamically

Is there a way to dynamically embed videos from a source into an HTML page? My goal is to make it easy to add new videos to the page by simply placing them in a folder and having them automatically embedded. I am wondering if this can be achieved using J ...

Tips for updating a portion of your code using new variables in JavaScript

I'm currently working on implementing a commenting feature for my website. The goal is to display new comments above the existing comment section when users submit their feedback. However, I'm facing challenges in dynamically updating the HTML wi ...

Is there a way for me to acquire the Amazon Fire TV Series?

I'm currently working on developing a web app for Amazon Fire TV, and I require individual authorization for each app. My plan is to utilize the Amazon Fire TV serial number for this purpose. However, I am unsure how to retrieve this serial using Jav ...

Shifting hues of dots within a grid according to the passing of time

As a newcomer to the world of coding, I have conceptualized an idea for a visually appealing clock. My goal is to visually represent the passage of time throughout the day. To achieve this, I have devised a grid system where each dot on the grid represents ...

Issue with mobile flipcard: Initial tap is unresponsive

Currently debugging the mobile version of a site built with Next.js. Encountering an issue where the first flip-card tapped on mobile refuses to flip properly. Instead, it selects the text on the opposite side of the card when tapped multiple times. The ...

Utilizing two visuals within the <picture> srcset

Is there a way to include two images on the desktop version (one on the right and one on the left) within the picture tag that replaces the mobile version image? I attempted it but it didn't work as expected. <picture> <source medi ...

ConnectionError: 404 Page Not Located

Why am I receiving downvotes? This is the second time I've been downvoted. Please, at least tell me the reason so that I can improve. I am currently using the jQuery get method to send data from HTML to PHP (both are hosted on localhost/Site1). Edit ...

How to retrieve the content/value from a textfield and checkbox using HTML

I'm encountering an issue with my HTML code where I am unable to extract data from the HTML file to TS. My goal is to store all the information and send it to my database. Here is a snippet of the HTML: <h3>Part 1 : General Information</h3 ...

Tips for restricting the preloader display to once per session

My website features a preloader on every page. <div id="preloader" > <div id="status"></div> <div id="statustext">some text</div> </div> This preloader is activated using the following jQuery code: //<![CDATA[ var ...

Limit of PHP string variable characters

I am working on an HTML survey where the answers are saved in variables and need to be inserted into a database using PHP. Here is an example of how the questions (part of a table) that can be filled out look like in HTML: <td style="padding-top: 10px ...

Using a video as a background in HTML

My code has an issue where I have set overflow: hidden in the CSS but the video is still not displaying below the text as intended. The relevant CSS snippets are: fullscreen-bg { top: 0; right: 0; bottom: 0; left: 0; overflow:hidden ...