The element is anchored within a div, but its position is dependent on a JavaScript script

I am dealing with a situation where I have a div named "Main_Card" containing text and an icon. When the icon is clicked, it moves the "Main_Card" along with everything inside of it. The challenge arises when I need to set the icon's position as either relative or absolute while also ensuring that Main_Card has overflow-y set to auto for scrolling purposes on smaller screens.

The issue occurs when Main_Card's content needs to be scrolled - not only does the text scroll properly, but the icon also scrolls along with it. Setting the icon's position to fixed is not a viable solution as it would fix the icon to the entire page rather than just within Main_Card.

Is there a way to make the icon unscrollable in this scenario? Any suggestions would be greatly appreciated. Thank you and have a wonderful day!

Answer №1

To provide a clearer understanding, it would be beneficial to include the code along with the question.

From what I comprehend, setting the overflow property on the text container should address the issue.

body, html {
  height: 100%
}
.card {
  position: relative;
  background: orange;
  width: 200px;
  height: 100%;
  
}

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

.card-icon {
  position: absolute;
  bottom: 20px;
  right: 20px;
  width: 50px;
  height: 50px;
  border-radius: 25px;
  background: green;
  color: white;
}
<div class="card">
  <div class="card-icon">
   </div>
  <p class="card-text">
    Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text.
     Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text.
     Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text.
    
  </p>
</div>

Answer №2

Using CSS to Keep Element Fixed Relative to Parent:

div{
  position:relative;
  overflow-y:auto;
  height:100px;
  width:200px;
  background:#eee;
  margin: 0 auto;
}
div i{
  position:fixed;
  /*Avoid assigning top, left, right, bottom properties! */
  margin: 10px;
  background: red;
  padding: 20px;
}
div p{height:400px;border:4px dashed blue;} /*creating scrollbars*/
<div>
  <i></i>  
  <p></p>
</div>

position fixed, if you add left, right, top, bottom, the element will relate to the viewport instead of the parent.
By not adjusting those properties, you can make the browser behave as you intend.

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

Obtaining a subset of data from firebase

I am currently working on retrieving a sub-collection from the Firestore database using Angular. In my database, I have a collection called 'Company' which contains fields for 'Name' and 'Id', as well as a sub-collection named ...

three.js fur effect not appearing on screen

I have been working on understanding and implementing fur in three.js. I came across an example at which I used as a reference to comprehend the code. The model loads successfully, but the issue arises when the fur texture doesn't load. I have check ...

Step-by-Step Guide for Uploading an Entire Folder and Its Contents

I have been working on a code to upload multiple files, but now I am facing the challenge of uploading an entire folder with multiple files and possibly subfolders containing even more files. Currently, I am utilizing JavaScript for obtaining the files and ...

A step-by-step guide on implementing a callback function

I am eager to incorporate a callback into this script - specifically the third callback onSlideChangeStart(swiper) found at http://idangero.us/swiper/api/#.V9CMp5grJlY. Since I have never worked with callbacks before, I am unsure of where to begin. In es ...

Incorporate a Bootstrap panel featuring a stylish border design and perfectly centered text

I am attempting to design two panels in Bootstrap with centered text and different fonts. Similar to the layout shown in the image below https://i.sstatic.net/aVGwV.png Here is my code: <div class="row"> <div class="col-lg-6&q ...

Creating a "Container" component in Vue.js step by step

As a newcomer to Vue, I am facing a challenge in implementing a wrapper component similar to React's 'Wrapper' component. Specifically, I want to create a reusable datagrid component using a 3rd-party some-table component and a pagination co ...

Swap the text within the curly braces with the div element containing the specified text

I have an input and a textarea. Using Vue, I am currently setting the textarea's text to match what's in the input field. However, now I want to be able to change the color of specific text by typing something like {#123123}text{/#}. At this poin ...

One method for routing pages in React Router is to have a shared component that is displayed on multiple pages, as well as a

My app.js consists of multiple pages with the navbar and sidebar as components, but my login page is different and does not include these elements. How can I use react-router to create separate routes for the login page without the sidebar and navbar, whil ...

Choosing the right framework for a web application

Currently, I am at a crossroads when it comes to deciding on the architecture of the web application I will be developing. As part of a small team, I am tasked with working on this project solo while my colleagues focus on other tasks. The front-end of th ...

What is the best way to adjust the spacing based on the width of the screen?

Seeking assistance with coding for a website, I have encountered an issue where I need the background to be relative to the user's screen width. However, I am struggling to find the right approach. I have attempted using relative units and media quer ...

Fade out effect in jQuery upon reloading the page

Is there anyone who can assist me with this issue? The following script snippet allows a user to delete records from a table connected to a mySQL database. <script type="text/javascript> $(document).ready(function(){ $('form.delete ...

Iterate through an array to dynamically assign values to variables using string elements

I'm facing a challenge here. I need to generate 4 new elements with the same class but different IDs without repeating code. Unfortunately, my loop doesn't seem to be working as expected. I've spent the last 2 hours trying to crack this puz ...

Should I utilize a single form or one for each table row in asp.net mvc?

While working on a project, I caught myself in a coding dilemma: <table> <tr> <th>Code</th> <th>Enabled</th> <th>Percentage</th> <th>Amount</th> <th>Start&l ...

Tips for effectively managing index positions within a dual ngFor loop in Angular

I'm working on a feedback form that includes multiple questions with the same set of multiple choice answers. Here's how I've set it up: options: string[] = ['Excellent', 'Fair', 'Good', 'Poor']; q ...

Using CSS to create various h1 headings with distinct colors and sizes

I am currently working with CSS and HTML, trying to replicate the design shown in this image: https://i.stack.imgur.com/Uyczp.png Initially, I attempted to achieve this by using multiple h1 tags, but after researching further, I realized that it is gener ...

Press the button to modify the titles of the table

One of the tasks I'm working on involves a table with column titles. In this table, there's an edit button that, when clicked, should convert the title into an input field with the current title as its initial value. The edit button will then cha ...

Prevent request timeouts in Express.js

I am currently experimenting with methods to terminate client requests that are prolonging excessively, thus depleting the server's resources. After reviewing various sources (referenced below), I attempted a solution similar to the one suggested here ...

Node.js offers a variety of routing options and URL endpoints

app.use('/api', require('./api')); app.use('/', require('./cms')); The initial path is designated for my public API, while the latter is intended for the CMS dashboard. However, the setup is flawed as localhost:80/a ...

Revamping Footer Text

Hey there, I'm feeling a bit inexperienced with this question, so after struggling on my own for a while I decided to turn to stackoverflow for help. So, I have a website located at , and at the very bottom of the page there's some copyright inf ...

Click to enlarge font size across the entire project

I'm working on a project for elderly users and my client wants a feature where they can easily adjust the font size throughout the application with just one click of a button. What is the best approach for implementing this functionality? My initial ...