Can a child div be contained within a fixed parent div that is specific to the page?

I find myself in a challenging situation with an existing website that requires some minor adjustments. While I understand that what I am about to ask may not be best practice, I'm still curious if it's technically achievable.

Imagine having a parent DIV with a CSS property of position: fixed; and top: 0;, now within this container, is it feasible to have a child element that behaves differently? Specifically, can this child DIV scroll along with the rest of the page while the parent remains fixed in its position?

Despite my efforts in trying out various position properties on the child element, I haven't been successful in breaking it free from its parent's constraints.

Take a look at the current code example here.

body {
  margin: 0;
  height: 2000px;
}

* {
  color: white;
}

.parent {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
}

.child1 {
  background-color: darkgreen;
  height: 100px;
  width: 100%; 
}

.child2 {
  background-color: green;
  height: 100px;
  width: 100%; 
}
<DIV class="parent">

  <DIV class="child1">
    child1 should stay at top during scroll
  </DIV>
  
  <DIV class="child2">
    child2 should scroll with page (begin to disappear once the page is scrolled down). What (if anything) can I apply to this child to get this behavior?
  </DIV>
  
</DIV>

Ideally, I would like to achieve this effect using only HTML & CSS, but I am open to considering alternative methods as well.

Answer №1

Modification required in HTML and CSS codes. Execute the code snippet to observe the expected behavior.

body {
  margin: 0;
  height: 2000px;
}

* {
  color: white;
}

.parent {
  width: 100%;
  height: 100px;
}

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

.child2 {
  background-color: green;
  height: 100px;
  width: 100%;
}
<DIV class="parent">
  <DIV class="child1">
    child1 should remain at the top while scrolling
  </DIV>
</DIV>
<DIV class="child2">
  child2 should scroll along with the page (start fading out as the page is scrolled down). What changes can be made to achieve this behavior for child2?
</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

Hidden Navigation Bar in AngularJS

Is there a way to hide the navBar when the route is "/"? I managed to successfully hide/show the navbar, but the issue arises when the navbar is hidden. The "app-body" div then has 50px of padding top (where the navbar should be). Here is my HT ...

Step-by-step guide on accessing values from a JavaScript object

I have a dictionary variable declared within a script tag on an HTML page. My goal is to retrieve the values associated with the "Roads" and "Intersections" keys, which change each time the page is refreshed. By capturing these values, I can then use JavaS ...

Require a selection from the menu on the right side needed

I am looking to have the logout link displayed on the far right when the other menu items are hidden. Currently, it is appearing in the center. Is there a way I can make it show up on the right? <div class="nav-content container"> &l ...

Unable to display and conceal div elements

<ol class="novice"> <li> <p class="glava">HTML</p> <div class="vsebina"> <p>Hyper Text Markup Language (slovensko jezik za označevanje nadbesedila...</p> </div> </li> <li> ...

Is it possible to hide the iOS Safari navbar by simply scrolling the body?

One of the challenges I am facing with my website layout is the grid structure that consists of a fixed header, scrollable content, and a fixed footer. The issue arises when using iOS Safari because the bottom navbar and URL bar never go away even when scr ...

Having issues getting the single div to refresh with `.#div.load(scriptpage.php)` command

I've been attempting to update the content of a specific div (in-game chat) every X seconds or minutes using this simple code. The issue I'm facing is that the content never refreshes or loads the new page. I've searched through various cod ...

Combining Files in Angular 2 for Optimal Production Deployment

What is the best method for packaging and combining an Angular 2 application for production? My goal is to have an index.html file and a single app.min.js file. Although the Angular 2 documentation mentions using webpack, I found it to be overly complex f ...

The <body> tag is not located at the top of the webpage

Recently, I've come across an interesting issue on my website. The scrollTo() method in jQuery seems to be working fine in Firefox, but it doesn't scroll to the top in Chrome. I suspect that the placement of the <body> tag might be causing ...

Tips for creating CSS styles for a selected input field

I seem to be stuck in a situation where my screen looks similar to the screenshot provided. There are four input elements that I would like to have bordered just like in the image, complete with a circled tick mark. I've managed to create these four i ...

Positioning two divs side by side, with two more below in the following row

I am attempting to align multiple div elements with a width relative to the screen size. However, when I try to float them next to each other, they shift to the right and display under each other. I have experimented with applying display: inline-block; a ...

"The combination of Windows, @font-face, and Cyrillic characters presents a

I am encountering an issue with font-face and Cyrillic characters. The fonts display properly in any browser on OS X, but fail to render on a Windows 7 machine (chrome, ie etc). Even after putting the fonts through Font Squirrel and testing the demo files ...

What is the best way to navigate between elements using AJAX?

My experience in Ajax is minimal. I've managed to create two <li> elements with <a> elements inside, each linking to different HTML files. My goal is for clicking on a link to automatically load the corresponding HTML file without refreshi ...

Rearrange the array in PHP/HTML code

I am working with an array that contains ID values, such as [2,34,7,46,16,19 ...] Each ID corresponds to additional data which is displayed using HTML like this: <ul> <li> content from 2 content from 34 content from 7 </li& ...

Displaying iFrame Border in React Native WebView

When attempting to dynamically add YouTube videos to my React Native app, I decided to utilize a combination of WebView and iFrame due to the incompatibility of the current react-native-youtube component with RN 16+. Although this solution works, the ifram ...

Is it possible to center Div/Img elements both vertically and horizontally over a background of a cover size?

^Read my jsfiddle link^ I have a small jq script that adjusts background image size based on viewport dimensions. My issue is with a div (.png) that needs to be centered vertically and horizontally on the background, no matter the screen size. I've t ...

Troubleshooting: Issue with changing the CSS of a div from display:none to display:block using jQuery

I have a scenario where the content of a div is dynamically changed based on checkbox values. For most options, the div displays simple text, but for one specific option, it should display another div (referred to as childDiv). To achieve this functionali ...

Validation needed for data list option

Here are all the details <form action="order.php" method="post" name="myForm" onsubmit="return(validate());"> <input type="text" list="From" name="From" autocomplete="off" placeholder="From Place"> <datalist id="From"> <option valu ...

Slider with a progress bar

Have you ever wondered if it's possible to create a progress bar using an input range? Imagine someone entering a number and the bar moves to the right, changing its background color on the left side of the thumb based on the size of the number. Check ...

Creating a Dynamic Soundtrack: How to Embed an Audio

Success! I finally figured it out, with a little help from everyone. I've been diving into the world of Audio Playlists in HTML and attempted to follow a tutorial on the topic found here: https://www.youtube.com/watch?v=vtZCMTtP-0Y. However, I encoun ...

Building a website using Bootstrap: A step-by-step guide

Wondering how I can incorporate all the cool Bootstrap components into a webpage. Is there a tool that offers live preview? Back in the day (web 1.0), Dreamweaver was my go-to for creating HTML and basic CSS, but what are some current tools where I can si ...