Preventing Scrollable Div Content from Displaying Behind a Fixed Div using CSS

Seeking help with a message_container division that contains a fixed scrollable message_header division and a scrollable message_body division. The issue I am facing is that the content of the message_body division is appearing behind the fixed div. How can this be resolved?

UPDATE: After some modifications, I now have an overflowing message_header division and a message_body division. While the message_header looks good, the message_body appears okay but still scrolls up behind the message_header. How can I address this? Just to clarify, this code is within a modal window, which I omitted initially as I didn't think it was necessary for solving my issue.

I have replicated the problem in this JSFiddle. I made the first content entry longer, so it appears behind the header division and becomes visible when you scroll. My goal is for the content to start below the fixed header division, not hidden behind it.

CSS Code Snippet:

.message_container {
    width=600px;
    height=395;
}

.message_header {
    padding-bottom: 5px;
    font-size: 14px;
    position: fixed;
    width:500px;
    height:50px;
    background-color: lightgrey;
    overflow-y:auto;
}

.message_body {
    overflow-y:auto;
    padding-top:50px;
}

HTML Code Snippet:

<div class="message_container">
    <div class="message_header">
        <p> test </p>
        <p> test </p>
        <p> test </p>
        <p> test </p>
    </div>
    <div class="message_body">
        <p>content is longer</p>
        <p>content </p>
              ... (repeated content)
    </div>
</div>

Edit: It's unfair when people downvote without providing a helpful answer. Constructive criticism would be more beneficial. I've extensively researched this issue and require assistance. Thank you.

Edit: Updated CSS classes to be less generic.

Solution:

Thanks to @Pete for the solution. Both the message_body and message_head divisions lacked defined widths and heights even though the message_container was later assigned specific dimensions. I revised my example, and here's the updated link : http://jsfiddle.net/kcfTc/59/

The revised CSS snippet includes:

.message_container {
    width=600px;
    height=395;
    position: relative; //new
}

.message_header {
    padding-bottom: 5px;
    font-size: 14px;
    position: fixed;
    width:600px;  //new
    height:50px;  //new
    background-color: white;  //modified
    overflow-y:auto;
}

.message_body {
    overflow-y:auto;
    top:55px;        //new
    bottom:0;        //new
    left:0; right:0; //new
    width: 100%;     //new
    position: absolute;//new
}

Answer №1

Is this what you're looking for?

http://jsfiddle.net/vdecree/kcfTc/10/

I believe I understand your issue, but I suggest reevaluating the structure for potential improvements. Adding some padding to the top of your body should suffice.

Edit*— in order to provide clarity and focus on the .content div rather than the body element, I have updated the black background. Does this meet your requirements: http://jsfiddle.net/vdecree/kcfTc/22/ - I'm eager to assist you further, but some aspects remain unclear.

css:

.body{
    overflow-y: visible;
    padding-top: 60px; /* push content down below fixed element */
}

Answer №2

To achieve the desired layout, simply assign a margin-top value to the .body class that is equal to the height of the fixed div. This will ensure that the content div appears below the fixed one.

Additionally, for the fixed div to always stay at the top, set top:0; for the .header class as well.

Answer №3

According to @Pete's suggestion, the dimensions of the message_body div and the message_head div were not specified despite the message_container having defined width and height values. I have made adjustments to my example and you can view it here:

http://jsfiddle.net/abc123/75/

The updated CSS code snippet is:

.message_container{
    width: 600px;
    height: 395px;
    position: relative; //new
}

.message_header{
    padding-bottom: 5px;
    font-size: 14px;
    position: fixed;
    width: 600px;  //new
    height: 50px;  //new
    background-color: white;  //modified
    overflow-y: auto;
}

.message_body{
    overflow-y: auto;
    top: 55px;        //new
    bottom: 0;        //new
    left: 0; right: 0; //new
    width: 100%;     //new
    position: absolute;//new
}

Answer №4

Referring to your initial jsfiddle link:

Modifications made to the CSS involve adding a top margin instead of relying solely on "top" and setting it to 65px for extra space, as well as specifying the height and width as previously suggested. I opted to include a border for better visibility when pasting this code into jsfiddle. Feel free to remove the border if unnecessary.

CSS

.message_body{
position: absolute;
margin-top:65px;
height: 100px;
overflow-y:auto;
width: 500px;
border: 2px solid darkgray;
}

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

Is there a way to automatically populate my second dynamic dropdown list on page load?

I am currently working on a dynamic drop down menu that consists of two lists. When the page initially loads, the second drop down list is empty. However, I would like both lists to be displayed by default. You can see an example of the issue I'm faci ...

Tips for ensuring math formulas display correctly in CKEditor

I'm currently struggling to properly display the correct format of a math formula in ckeditor. I have made numerous attempts to find a solution, but so far, none have been successful. Here is an excerpt of my code: <html> <head> <scr ...

Close popup after submitting the form

On my website, I have a function to submit a form within a modal. Everything works well except for when I uncomment a certain line of code. When I uncomment that line, my test mysql insert on the page /index.php/trainings/add doesn't get executed. I a ...

The performance of the Ionic app is significantly hindered by lagging issues both on Google

After starting to work with the ionic framework, I noticed a significant change in performance when testing an android app on Chrome for the first time. It was fast and responsive until I added a button that led to a screen with navigation bars, side men ...

Having difficulty locating the identification number of an item within the HTML coding

I'm currently trying to locate the ID of the "proceed to checkout" button for a bot that I am developing for my mother on the Best Buy website. However, it seems like the button's ID is hidden and I'm unable to determine how to uncover it. A ...

Vacant area on the right side of the page

There seems to be an issue with the website where there is an empty space to the right, causing a horizontal scroll bar to appear at the bottom. Despite my efforts, I cannot seem to identify the root cause of this problem. While one solution would be to si ...

Issue with JQuery on("submit") event not triggered upon first click

I'm currently using JQuery to check the size of an input field after submitting a form: <!DOCTYPE html> <html lang="en> <head> <title>Page 1</title> <meta charset="utf-8"/> <script src="https://ajax.google ...

Unable to display "xyz" using console.log() function upon button click

Why isn't the JavaScript function being executed in this code snippet? <form> <select type="text" name="month" id="month"> <option value="01">January</option> <option value="02">February</option> ...

Failure occurred when attempting to link and display on the page container

I have created a simple app using jQuery Mobile. At some point in the app, I include the following code: <a href="test_es.html" data-role="button">Start!</a> This code snippet loads a new HTML file that contains several jQuery Mobile page ...

Designing a patterned rectangle filled with stylish text

I am relatively new to the world of HTML and CSS, and I am encountering a specific challenge with CSS. My goal is to design a section that looks like this: ---Image--- --Text-- ---Image--- --Text-- ---Image--- --Text-- ---Image--- --Text-- The ma ...

Adjust HTML table cell height to dynamically match content and align to top

I am currently working on creating a table layout where the left column adjusts its height based on content, while the right column needs to have a fixed height of 300. <table border="1" cellspacing="0" cellpadding="0"> <tr> <td>&a ...

Ways to ensure a div matches the size of the div above it within a Boostrap grid column

Hello there, this is my very first question on this platform. I'm still in the early stages of learning all these concepts, so please be patient with me ...

What are some ways to ensure that text can adapt to the size of a

I am looking to create dynamic text that adjusts based on the size of its parent container. As the parent container's size changes, I want the text to automatically adjust accordingly. Specifically, I want the text in a widget to resize when the widg ...

Froala text area is unexpectedly visible when I attempt to cover it with a partially see-through mask

The website I'm currently developing features a semi-transparent overlay that dims the screen with a light-colored message displayed on top. This overlay and message are shown whenever there is a background process running. Everything was running smoo ...

Angular 11 component encountering issues with accessing server-side data

Recently, I encountered an issue when trying to connect the PHP API with the Angular service to retrieve data from the database. I am uncertain whether the problem lies within the service, the component.ts file, or the HTML section. Therefore, I have share ...

Triggering a JavaScript event with every update to the DOM marked as "completed"

I've been having some trouble with a specific task. I'm working on a script where I need to execute some functions after DOM manipulation is complete. The challenge is that I can't make changes to any other scripts on the page, as they might ...

Elevation in design ui component

I am having an issue with the height of a theme-ui component I embedded. Even though the console shows it correctly, it is displaying at 100% height. <Embed src={url} sx={{width: '800px', height: '400px'}}/> This embed is contain ...

Continuously executing a series of JQuery functions or iterating through them repeatedly

Struggling with a jQuery animation loop issue that has me stuck. I need the animation to repeat continuously, but it's not working despite trying setInterval. Can anyone assist? Here's the fiddle link: https://jsfiddle.net/8v5feL9u/ $(document). ...

Step by Step Guide to Creating Vote Tallying Buttons with XHTML

I'm trying to create a voting system where users can choose between two images. However, all the tutorials I've found focus on text or check-box options. How can I make clickable buttons tally votes for each image and then display the results on ...

Issue with overriding font-family variable in Bootstrap 5 not resolving

I have configured Proxima+Nova font for my Angular application style.css @import url('https://fonts.googleapis.com/css?family=Proxima+Nova'); Attempting to change the root font using bootstrap variables as shown below $variable-prefix: ...