div consistently positioned above fixed element

My goal is straightforward - I have a fixed div at the bottom of my page that always needs to be visible. Inside this div, there are two sub-divs; one small div should always be on top, and the other should be scrollable.

The issue lies with the small div - if I give it a position of fixed, it appears at the top of the window instead of on top of the fixed div itself, as demonstrated in this example.

Alternatively, positioning the small div as absolute places it on top of the fixed div, but when scrolled, it behaves erroneously, illustrated here: fiddle

Here's the HTML:

<div class="bottom">
 <div class="top"></div>
 <div class="content"></div>
</div>

And the CSS:

.bottom
{
    padding:20px;
    height: 253px; 
    position: fixed; 
    bottom:0px;
    width:100%;
    background-color: red;
    overflow-x: hidden;
    overflow-y: scroll;
}

.top
{
    height:50px;
    width:100%;
    background-color: yellow;
    position: fixed;
    top: 0px;
}

.content
{
    height: 1500px;
    background: linear-gradient(green, blue);
}

Is there a way to achieve this layout without relying on JavaScript to monitor scrolling? Can it be done through pure CSS?

Answer №1

If you want the content to scroll independently from a fixed position element, you can enclose the content in a <div> wrapper and add some CSS to achieve this effect:

Here is the HTML structure:

 <div class="bottom">
   <div class="top"></div>
      <div class='contentWrap'>
        <div class="content"></div>
      </div>
 </div>

And here is the corresponding CSS:

.contentWrap{
 height:100%;
 overflow-y:auto;
}

* {
  margin: 0;
  padding: 0;
}
.bottom {
  padding: 20px;
  height: 253px;
  position: fixed;
  bottom: 0px;
  width: 100%;
  background-color: red;
  overflow: hidden;
}
.top {
  height: 50px;
  width: 100%;
  background-color: yellow;
  position: absolute;
  top: 0px;
}
.contentWrap {
  height: 100%;
  padding-top: 30px; /* .top height - .bottom padding*/
  overflow-y: auto;
}
.content {
  height: 1500px;
  background: linear-gradient(green, blue);
}
<div class="bottom">
  <div class="top"></div>
  <div class='contentWrap'>
    <div class="content"></div>
  </div>
</div>

JSFiddle Demo

Answer №2

Your method of utilizing fixed -> absolute is spot-on as it allows you to position an element in an absolute manner but relative to its parent. However, the issue arises when the absolute .top always overlays .bottom - causing .top to move along with .bottom if it is scrolled.

One alternative solution could be implementing position:fixed; on .top, but making use of bottom instead of top:

.top {
    ....
    position:fixed;
    bottom:253px; /*you may need to experiment to achieve desired result*/
 }

Answer №3

Insert a div with the class name top inside another div that has the class name content. Also, remove the style rule top:0 from the .top class:

html

<div class="bottom">  
    <div class="content" >
        <div class="top"></div>
    </div>
<div>

css

.top
{
    height:50px;
    width:100%;
    background-color: yellow;
    position: fixed;
}

view the fiddle

Answer №4

If you're looking to maintain the layout of a scrollable div, consider using a frame container. Check out this solution that keeps everything in place within a scrollable area: JSFiddle

<div class="wrapper">
    <div class="header"></div>
    <div class="scroll-container">
        <div class="inner-content" ></div>
    </div>
<div>

.scroll-container 
{
    height: 203px;
    overflow-y: scroll;
}

To ensure proper functionality, don't forget to remove overflow-y: scroll; from the parent element's CSS as well!

Answer №5

Instead of dealing with fixed heights and positions, you can easily make the 'top' section fixed as well by simply adding the following CSS code:

.top
{
    height:50px;
    bottom:243px;
    width:100%;
    background-color: yellow;
    position: fixed;
}

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

Ways to implement a placeholder height for images on a webpage with varying heights using HTML

I'm facing an issue with an image on my website that has a dynamic width and height as defined by the following CSS: .sm_item_image { width:100%; height:auto; max-width:400px; } The problem arises when the image takes some time to load, ...

The function is invoked numerous times

My issue involves using jQuery to handle the mouseenter and mouseleave events. The problem arises when transitioning from one div to another, causing the events to trigger again. I am looking for a solution to only run these events once per mouse enter and ...

What could be causing the onclick function in the button tag to malfunction?

I have implemented a feature where clicking on a "Delete Photo" button would trigger the appearance of another delete button in the code. The code for this functionality is as follows: <button type="button" class="btn btn-danger" onc ...

Sudden slowdown due to Jquery error

I am encountering an issue with the Jquery registration validation. I am struggling to display the error message if a name is not filled in. jQuery(document).ready(function () { $('#register').submit(function () { var action = $(thi ...

Can JavaScript be Utilized to Create Dynamic Layouts Successfully?

Are you curious about the viability of using Javascript to create fluid website layouts? While it is possible, how does it compare in terms of performance and complexity when compared to other methods like CSS3/HTML5? Take a look at the function below that ...

What could be the reason for my Vue application failing to load, even though the mounted event is being triggered

Here's an interesting scenario. The code functions correctly in CodePen and even in Stack Overflow's code renderer, but it fails to work on my GitHub Pages site. No errors are triggered, and the console logs for the created and mounted events ex ...

Leveraging Mongodb's aggregate feature to calculate the overall quantity of a specific product that has been ordered across all

Recently delving into the realm of javascript, I am currently tackling a dashboard project that requires displaying the total quantity of each product ordered. Although I have successfully defined a variable 'qty' to represent the quantity of an ...

The issue of misplaced data and nested v-for loops

I received some rather messy data from a backend API in JSON format and I am struggling to display it correctly within an HTML table. The list items are not aligned properly within the table. For instance, as shown in the screenshot, items like 2.1, 2.2, ...

Raphael and jQuery/JavaScript for user-selected array intersections

Hello everyone! This is my first time posting here, and I must say that I'm still quite new to JavaScript/jQuery/Raphael. Please forgive me if I make any mistakes or ask basic questions. :) I've been searching high and low for answers to my quer ...

I am experiencing an issue where the submit button in my HTML form is unresponsive to clicks

Welcome to my HTML world! <form action="petDetails.php" id="animalInput"> <ul> <li> <label for="dogName">Enter Dog's Name:</label><input id="dogName" type="text" name="dogName" /> </l ...

Navigate to a different page in NextJs without causing a page refresh or altering the current state of the application

Recently, I encountered a challenge in my NextJS application while attempting to incorporate dynamic routes as endpoints on my server. Specifically, when accessing localhost:3000/register, the register.tsx file is loaded successfully. However, within one ...

a fixed width layout with two columns aligned at the top

I've been struggling to design a two-column, top-aligned layout with fixed width for data that will be inserted into text in a WebView for an iPhone app. The first column needs to have a set width so the items in the second column can align perfectly. ...

How can we stop the Replace function from replacing spaces as well?

When I trigger a paste event in an input field, I have a method that replaces all special characters. However, it is also removing empty spaces between words. How can I prevent this from happening? checkSpecialCharacters(){ let value = this.form.get(&q ...

Execute function upon changing the title of a list item using jQuery

Hey there, I've got a question for you. Is it possible to trigger an event when the title of a list element is changed? For example: <ul> <li id="li_1" title="40">40</li> </ul> So when the title attribute changes (coming ...

Creating a dropdown navigation menu using CSS from href divs

I am working on a script that includes divs with images and href functions for clicking on them. <div class="home"><a href="index.php?pagina=home"></a></div> <div class="events"><a href="index.php?pagina=events"></a& ...

Exploring the capabilities of JavaScript on the iOS platform

Here is the code I've written for my iOS application: webView = [[UIWebView alloc] init]; webView.delegate = self; [webView loadHTMLString:@"<script type=\"text/javascript\" src=\"myFile.js\"></script& ...

Implement Dynamic Filters in a Vue Component

How can filters be programmatically applied to select values? During each iteration of records and columns, I am checking if the column ID starts with 'd', indicating it's a datetime field. In such cases, I want to apply the humanize filter ...

What could be causing my hamburger icon to malfunction?

https://codepen.io/anon/pen/QNqgMo Curious why the top line of the animation isn't moving. Any code wizards out there who can spot the mistake? Appreciate it! #hamburger-icon.active .line-1 { transform: translateY(25px) translateX(0) rotate(45deg); ...

Tips for structuring classes in an Angular project

What is the best approach for handling API responses using classes in various programming languages like TypeScript, JavaScript, Java, or others? Consider an application that requires three resources: Account (API: /account/:id) Car (API: /account/:id/c ...

Algorithm to identify the highest sum of two numbers in a disorganized array of whole numbers

I am looking to create a function that can identify the largest pair sum in an unordered sequence of numbers. largestPairSum([10, 14, 2, 23, 19]) --> 42 (sum of 23 and 19) largestPairSum([99, 2, 2, 23, 19]) --> 122 (sum of 99 and 23) largestPairSum ...