ensure placement within the div

I have a back-to-top arrow image that is fixed on the page when clicked. I want this arrow to be positioned within its parent div.

Below is the code snippet:

.relative {
  position: relative;
}
.up {
  float: right;
  max-width: 3%;
  display: block;
  position: fixed;
  top: 40%;
  bottom: 30%;
  right: 2%;
}
<section class="elements relative" id="about">
  <a href="">
    <img src="img/arrow-up.png" class="up" id="up-about" alt="" />
  </a>
  <h2>Who am I?</h2>
  <p>Lots of text</p>
</section>

The current code makes the white arrow stay fixed on the screen as I scroll up or down the page. However, I only want it to remain within the "relative" section and not extend beyond. Can anyone suggest a simple solution for this issue? I hope my query is clear :)

Answer №1

To ensure your arrow moves with the content as you scroll, it's recommended to use position:absolute; instead of position:fixed;. Here is an example:

.relative {
  position: relative;
}
.up {
  max-width: 3%;
  display: block;
  position: absolute;
  top: 40%;
  bottom: 30%;
  right: 2%;
}
<section class="elements relative" id="about">
  <a href="">
    <img src="img/arrow-up.png" class="up" id="up-about" alt="" />
  </a>
  <h2>About Me</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</section>

Answer №2

To achieve the desired effect, apply position: absolute to the element with the class .up, without using float properties.

.up {
  max-width: 3%;
  display: block;
  position: absolute;
  top: 40%;
  bottom: 30%;
  right: 2%;
}

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

Utilizing query parameters in Next.js

I've been working on a unique Next.js application that incorporates both infinite scroll and a search input feature. The infinite scroll functionality loads 6 additional items whenever the user reaches the bottom of the page. On the other hand, the s ...

Challenges encountered with the css dropdown menu when hovering over li elements

Occasionally, I encounter a problem with my menu. Hovering over the "produkter" menu item should trigger a submenu to appear. Most of the time, I am able to navigate down the submenu with my mouse cursor. However, on some occasions, the submenu disappears ...

Failure to initiate JavaScript function with onClick event

I've been struggling with a simple issue related to calling a javascript function. After spending several hours trying to debug it on my own, I'm reaching out for help in the hopes that a fresh perspective can assist me. Here is the snippet of c ...

How can you use mouseover events to display a popover and manipulate the div id?

In my scenario, I have multiple div elements and I want to display separate popover for each div. Initially, the popover is not shown on the first mouseover event but works perfectly on subsequent attempts. Below is the code snippet: $(document).read ...

Checking for valid zip code using a regular expression in JavaScript

Thank you for the suggestions, everyone. I have made some modifications to the script based on your advice and it appears to be functioning correctly now. <script src="jquery-1.4.2.min.js" type="text/javascript"></script> <script> $ ...

The height of the li element is not properly aligning with the height of

I am having some trouble with creating a menu panel. I have set the height for both the li and menu div to be the same, but there appears to be a margin at the bottom of the li. I would like the li to completely fill the menu height. Any help is greatly a ...

Build a PHP-driven form for data collection

Previously, I posted this question but didn't receive a clear answer. Here's what I'm confused about: Below is the HTML snippet: <body onload="showcontent()"> <!-- onload optional --> <div id="content"><img src ...

Something seems off with the way WebGL is rendering shapes

I am encountering an issue with my code that is supposed to display parsed radar data on a Mapbox GL JS map using WebGL. While I have successfully managed to render the general radar shape on the map, the rendering is incorrect. Instead of drawing rectangl ...

Is there a way to combine multiple images into one PDF document using Html2Canvas and JsPDF?

Trying to understand the HTML2Canvas API has been quite challenging due to the limited documentation available. My goal is to place multiple div elements, each representing a different page, into a single PDF file. It might sound simple at first, but in re ...

The ever-changing world of list items with dynamic editions

One of my tasks involves displaying a list of elements through the ng-repeat directive. Each element is contained within its own div block. My goal now is to enable editing for each block, so that when users click on an edit button, the content of the div ...

Is it necessary to utilize process.env in node.js?

In my project, there is a .env file containing the following: ABC='abc' While in my app.js I can access the value abc using process.env.ABC, how can I require it to be used in my models' files? Attempting to use process.env.ABC in my model ...

Tips for incorporating a callBack function when the screen changes in react-native:

Is there a way to implement a callback function that executes whenever the screen changes, such as moving from the home screen to the about screen? Situation I would like to create a global stack that triggers a callback function whenever any navigation o ...

transferring data from grails to javascript via a map

I am facing an issue with passing a Map object from my Grails controller to JavaScript. The code snippet in my controller is as follows: def scoreValue = new HashMap<String,String>(); scoreValue.put("0","poor"); scoreValue.put("1","good"); ... retu ...

Guide to changing the route in Node.js/Express?

When creating posts and comments, I am looking to redirect to the previous route after deleting a comment on a post. Specifically, I want to redirect without specifying a particular route. router.route('/post/comment/destroy/:postroot').post(fun ...

Steps to toggle text color upon clicking and switch the color of another text

As a beginner with no Js knowledge, I am trying to achieve a specific function. I want to be able to change the color of text when it is clicked, but also have the previously clicked text return to its original color. <div class="mt-5 fi ...

Laravel: JavaScript Integration Problem (Current Status Concern)

I encountered an issue with the update status feature. The status is being stored correctly in the database, but the corresponding status icon does not change as expected. Upon refreshing the page, the status icon reverts to its previous position, even tho ...

Getting information from a JSON file with several variables: A guide for using node.js, express, and jQuery

In order to minimize the number of Ajax calls, I am attempting to send three variables in a single Ajax response. However, I am facing difficulties when trying to process the data on the client side. Let me elaborate on my issue and if sending multiple var ...

Having trouble implementing an ajax login/logout script on a WordPress website

I'm encountering issues with the login/logout script on my ajax-driven website. Here's the situation: The website should operate fully through ajax. Only the content and menu should change on ajax requests. This functions well for regular pages ...

Customizing the appearance of elements based on their designated identifier

There is a division named top nav, like this: <div id="topnav"> I want to apply styling to all the A elements within this division only, excluding those outside it. Is there a way to achieve that using CSS? ...

Styling nested divs in CSS

I am experiencing an issue where the child divs within parent divs are overflowing outside of the parent divs. To get a better understanding of the problem, please try running the code below in a browser: My goal is to align the innermost divs horizontall ...