Element that remains fixed within an element that cannot be scrolled

I am facing a challenge where I have a button nested inside a section that needs to maintain a fixed position within the parent container ('.container') while scrolling through the page. The catch is that the section itself does not scroll and can vary in height. How can I achieve this for the 'a.button-primary' element?

<body>
<header class="section page-header">
</header>
<section class="breadcrumbs-custom-inset">
</section>
<section class="section section-sm bg-default">
    <div class="container">
        <a class="button-primary button" href="">
            <span class="icon fa fa-plus"></span>
            Add New Item
        </a>
        <div class="row-sm row-40 row-md-50 justify-content-center">
        <!--List of Items-->
        </div>
  </div>
</section>
</body>

I attempted using Sticky Position but encountered issues with it. I also experimented with fixed positioning, but it anchored the button based on the window rather than the '.container'.

Below is the CSS I applied:

.container{
position: relative
}

.button{
position: sticky;
top: 0;
right: 0;
}

This solution worked when I added

height: 100px; overflow-y: scroll
to '.container', but I prefer not to set a specific height.

Answer №1

If you want to make use of a different ancestor besides the body to relate position: fixed to, simply apply a zero transformation to the desired parent element:

.container { transform: translate(0); }

Keep in mind that this approach may not yield the intended outcome if the .container itself ends up scrolling out of view.

fixed
When an element is set to position: fixed, it is taken out of the normal document flow and does not contribute to the page layout. The element will be positioned relative to the initial containing block as determined by the viewport, unless one of its ancestors has a transform, perspective, or filter property assigned with a value other than none (refer to CSS Transforms Spec), in which case that specific ancestor will act as the new containing block.

Source: MDN

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 show or conceal content using only CSS

Looking for help with switching content using pure CSS. I'm not very skilled with CSS and would prefer to avoid using JavaScript if possible. Any assistance would be greatly appreciated. Below is a snippet of the code: If A is clicked, toggle-on-con ...

Refinement of website footer alignment

As a web designer, I am experiencing difficulty in adjusting the footer on my website. I would like the footer to be fixed at a specific height and remain in the same position unless the content increases, in which case it should move down accordingly. Can ...

How to override an event in JavaScript when a specific value is entered

I am looking to implement a system with complex conditions. The goal is to have an alert appear when a value is inputted and the button is clicked. First, a confirmation message Have you input ? should be displayed, followed by clicked with input. If no va ...

Creating a custom file input field with Bootstrap 4

I've been attempting to incorporate the custom-file-input feature using bootstrap4 alpha 6, but I'm encountering a blank area after adding it, like this: <label class="custom-file"> <input type="file" id="file" class="custom-file-in ...

Adding 'foobar' to innerHTML using JQuery

I want to add new content instead of removing the existing one. I have been using the element.innerHTML += 'foobar' method for this purpose, but now I am trying to learn how to use jQuery. I came across the $().html('') function, but it ...

Storing multiple values in local storage

Hey there! I have a text box where I input grades, and they get saved in local storage. I can see them below the text box. Now, I want to add more text boxes for additional grades. How can I achieve this and ensure that the grades are saved in a separate a ...

The -webkit-box-reflect effect vanishes during the transition

I need help with this issue: The image loses its reflection during the transition until it returns at the end. Any suggestions are appreciated! Thank you! .specials-box { width: 330px; margin: 50px 0; height: 500px; position: relative; ...

Tips for generating a numeric whole number input field with Vuetify

After researching the topic on Vuetify's specific number input component, I am attempting to create a numeric input field. The value for both input and output is currently unknown, meaning it could be either undefined or null in order to allow cleari ...

The table header row mysteriously vanished after implementing the "stacked" property

A few days back, all of my <b-table> elements that were utilizing the stacked="md" property suddenly had the headers row disappear. Removing the property brings back the headers row, but I really need this property for responsive stacking on smaller ...

How to center an item in a Bootstrap navbar without affecting the alignment of other right-aligned items

Currently, I am in the process of designing a website and have implemented a Bootstrap navbar. The navbar consists of three icons aligned to the right, and I am looking to center another element. However, when I use mx-auto, the element does not center per ...

Design element: Text overlaying background with a touch of transparency

Is there a way to create a background image with an opacity level of 0.5, while maintaining the text on top at full opacity (1) for better readability? Currently, the background image is not displaying as desired, and the opacity settings are affecting the ...

Utilizing HTML 5 validation with dropdown menus and date pickers in .NET Web Forms for enhanced functionality

Having trouble validating two controls on my aspx page with HTML 5: <asp:DropDownList ID="dropCountry" required runat="server"></asp:DropDownList> <input id="datepicker" class="datepicker" required runat="server" readonly="true"/> Here ...

How to center an image within a div without it moving

I have set up a JSFiddle for reference: http://jsfiddle.net/AsHW6/ My task involves centering the down_arrow.png images within their respective div containers. I have successfully centered the up_arrow.png images using auto margins. These images are posit ...

Contrasting one-finger scrolling versus dual-finger scrolling on a touch device with IE11

Can you explain the distinction between scrolling using one finger versus two fingers in IE11? I have noticed that I am able to scroll/pan the webpage with just one finger on certain elements, while on other elements, I need to use two fingers on a Micros ...

When trying to use ngModel with Angular, an error occurs where the property 'selected' cannot be created on a string

I'm currently attempting to utilize [(ngModel)] in the following manner: <div class="items"> <tbody> <tr *ngFor="let info of this.information"> <td> <input type="checkbox" [(ngModel)]="this.info.n ...

What could be causing the divs to overlap? Without the use of floats or absolute positioning,

When resizing vertically on mobile, my date-time-container is overlapping the upper elements welcome and weather. Despite setting them as block level elements, adding clear: both, and not using absolute positioning or floats, the overlap issue persists. An ...

The error message "Conflict Between Editor Elements Detected (Code: CKEDITOR Error code

Incorporated within my web application is the CKEditor jquery adapter which functions smoothly, however, a console error appears: Upon consulting the CKEDITOR dev error documentation, I found an explanation for the error but unfortunately no solution was ...

Tips for protecting your Rails application from unauthorized duplication

I am a Ruby on Rails developer with expertise in Point of Sale Systems. Recently, a situation arose where one of my clients managed to duplicate my app along with the DB structure and distributed it among others (theft/piracy). What steps can I take to pr ...

What is the best way to create a semi-completed circle?

I'm looking to create a unique design by filling half of the circled outline. Take a look at this image and accompanying code snippet: <body> <div id="ui"> <div class="status"> ...

I am looking to implement an animation that automatically scrolls to the bottom of a scrollable DIV when the page is loaded

My DIV is configured with overflow-y set to scroll. .scrolling-div { width: 85%; height: 200px; overflow-y: scroll; } If I have an abundance of content within this div, my goal is for it to automatically scroll to the bottom upon loading the page. I am ...