Transform a toggle button into a permanent sticky feature

I've encountered an issue with creating a sticky button in the code snippet below.

https://codepen.io/nht910/pen/KKKKerQ

Snippet:

<div class="post-body d-flex justify-content-center">
    <!-- content -->
    <div class="post-content">
        <p>
            ...
        </p>    
    </div>

    <!-- button toggle -->
    <button type="button" class="btn btn-outline-primary" id="toc-button">
        button
    </button>

    <!-- table of contents -->
    <div class="post-toc">
        <nav class="sticky-top" id="toc"></nav>
    </div>

</div>

The button in this code is currently set to have the property "position: absolute;". I attempted to change this property to "position: sticky", but it caused the layout to break and the content was no longer centered.

Additionally, upon page load, the table of contents is already expanded by default.

I'm seeking assistance in making the button sticky within the parent div ".post-body" like the table of contents, ensuring that it remains above the table of contents. Furthermore, I would also appreciate help with setting the table of contents to be closed on page load.

Thank you for your assistance!

Answer №1

Insert the button following the table of contents and set it to position: sticky.

See this example:

Answer №2

Could an automatic scroll feature be implemented when the Table of Contents menu becomes lengthy due to numerous headings being present? To experience this scenario, try reducing the height of the preview window in codepen.io examples significantly. This will result in only a few headlines being visible in the ToC menu. As a consequence, navigating through the content becomes challenging as the menu selection vanishes and the reader loses track of their position.

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

Transferring Data Between Two Modals

Currently working on developing an approval/edit process for the company website. The setup involves a jquery datatable that showcases scheduled events specific to the logged-in user in a particular format. results = await response.json(); var tr = $(this) ...

"Troublesome issues with the functionality of the jquery lazyload plugin

I've come across an issue with the jquery lazyload plugin in my project. It seems that the plugin doesn't kick in when the page initially loads, but rather only starts working when I click on the Home tab of my website. Below are snippets from my ...

The Codeigniter submission form using jQuery ajax() encountered an error

Can someone please help me out? I've gone through numerous tutorials on CodeIgniter from various websites, including Stack Overflow, but haven't found a solution to my problem. Guys, can you please teach me? This is the jQuery ajax function code ...

What is causing Visual Studio 2022 to flag the use of an unidentified CSS class property from Bootstrap?

I recently set up a new razor page web app template project in visual studio 2022. As I started adding some bootstrap CSS classes, here is the HTML snippet that I used: <form method="get"> <div class="form-group"> ...

Include jQuery from a different directory

A Closer Look at the Symptoms Encountering issues with jQuery file paths can lead to undefined errors. With jQuery located in different directories, such as the root or child directory, certain JavaScript files may fail to recognize its presence. The sol ...

Facing challenges in implementing a logic to showcase an intricate page on express.js (node.js) platform

Having trouble setting up a functional logic to display a detailed page showcasing the post title, image, and details. Currently, I've successfully created a list page that displays all the posts by the logged-in user with the title, image, and detail ...

Loop through an array of names in a post input field

There is an array in a while loop, please check the code below: <?php $addItemSql = "SELECT * from tbl_invoice_services where col_cust_id='$getID' ORDER BY col_service_id"; $addItemResult = mysqli_query($db, $addItemSql); while ($addItemRow ...

Sending emails through a basic HTML/PHP form is incredibly time-consuming

Seeking assistance with optimizing my email form that utilizes the POST function alongside a PHP mailto() feature. The process of sending the form takes significantly longer than expected. Any suggestions or guidance would be greatly appreciated. Here&apo ...

Issue: Undefined onClick function for HTML when using Node.js, NPM, and Parcel

I am currently working on a weather application using Node.js, NPM, and Parcel for development. To ensure OpenLayers functions properly, I need to incorporate these technologies, even though I'm not very familiar with them. One key aspect of my proje ...

Place an image at the center with a height set to 100% within a div that has a fixed height and

Apologies for asking about this topic again, but I have been unable to find a solution where the image fills 100% of the height. If you'd like to see the issue in action, here's a link to the jsfiddle: http://jsfiddle.net/BBQvd/3/ I'm just ...

Is there a way to construct a Javascript function, without relying on JQuery, for fetching JSON objects from varying

I have been searching for a non-JQuery AJAX function that can fetch data from a URL and return it as a JSON object. For example, let's say I want to display information about Users from one JSON located at URL1 and also include information about Post ...

Can Django implement pagination without altering the URL structure?

I discovered the fundamentals of pagination in Django at this resource: Nevertheless, I am faced with a challenge - I wish to implement pagination without modifying the URL. Specifically, I need to paginate a single table among multiple tables on an HTML ...

Looking for a way to incorporate an image source property into a larger image?

I am working on a function that will enlarge an image when clicked on from a selection of 5 or more available images. The idea is that clicking on a small image will trigger the display of a larger version of that image in a frame. Here is an example of t ...

Ways to conceal a form post-submission

Looking for help with a programming issue - I'm trying to hide a form after submitting form data to prevent multiple submissions. The forms are created dynamically and I've tried hiding both the form and the div it's inside without success. ...

It is not possible to highlight the text that appears in the title of the tooltip in React MUI

Within my react application, there is a component that utilizes the code snippet below: <FormControlLabel labelPlacement="top" control={ comp?.manualInput ? ( <TextField name="price" type= ...

Creating uniform height for Definition Terms (DT) and Definition Descriptions (

Looking to creatively style a description list <dl> in a way that showcases two columns, even when there are multiple <dd>s following the <dt>. The challenge lies in wanting this design to be dynamic without resorting to floating the < ...

All fields in the form are showing the same error message during validation

This is the HTML code: The form below consists of two fields, firstname and lastname. Validation Form <form action="" method="post"> Firstname : <input type="text" placeholder="first name" class="fname" name="fname" /> <span class= ...

Is there a way to transform a basic array into an array containing objects?

In my Vue.js project, the API data is returned in a flat array format. I need to restructure this data into a hierarchical tree structure by assigning parent IDs to their corresponding parent items. I attempted to achieve this but ended up with an empty t ...

Using jQuery: How can we manipulate the data returned by sortable('serialize') on a list?

Using jQuery, I have successfully retrieved the positions of a sortable list by using 'serialize' as shown below: var order = $('ul').sortable('serialize'); The variable 'order' now contains this data: id[]=2& ...

Smoothly transition between the new and existing child elements in a React component with a

Currently, I am utilizing a React component that renders a child element through props.children. The content within this child element varies dynamically. I am looking for the most effective method to smoothly transition (fade out the old element and fad ...