The fixed div is always positioned in the top corner

My webpage has a 'Book Now' button that remains at the top of the viewport with a 10px gap. However, due to the parent element having a height of 100%, the button scrolls out of view once the viewport surpasses that height. To see an example, click here.

Is there a way to make the parent element 100% of the entire webpage height, ensuring that the 'Book Now' button always remains at the top?

Here is the HTML code:

<div class="book font">
        <a href="index.html">BOOK NOW</a>
</div>

And the CSS code:

.book {
position: absolute;
height: 100%;
z-index: 100;
margin-top: 80px;
margin-right: 10px;
right: 0;
top: 0;
}

.book a {
    position: sticky;
    top: 10px;
    text-decoration: none;
    color: #1e1e1e;
    border: 4px solid #cfac44;
    background: #cfac44;
    padding: 10px 15px;
    transition: .2s;
    -webkit-transition: .2s;
}

Answer №1

Your issue can be resolved by utilizing position: sticky instead of position: fixed.

Simply include position: relative in the body style:

body {
    ...
    position: relative;
}

Also, apply bottom: 0 in the .book selector. This will extend the div to the very bottom of the body tag:

.book {
    ...
    bottom: 0;
}

Answer №3

Adjust the positioning to fixed for both elements and modify the margin top and right as shown in the code below

.book {
   position: fixed;
   height: 100%;
   z-index: 100;
   margin-top: 30px;
   margin-right: 150px;
   right: 0;
   top: 0;
 }

Here is the code for the 'a' tag:

.book a {
     position: fixed;
     text-decoration: none;
     color: #1e1e1e;
     border: 4px solid #cfac44;
     background: #cfac44;
     padding: 10px 15px;
     transition: .2s;
     -webkit-transition: .2s;
    }

Answer №4

Below is the updated CSS code:

book {
    position: fixed;
    height: 100%;
    z-index: 100;
    margin-top: 0;
    margin-right: 0;
    right: 0;
    top: 0;
}

.book a {
    position: sticky;
    top: 0;
    text-decoration: none;
    color: #1e1e1e;
    border: 4px solid #cfac44;
    background: #cfac44;
    padding: 10px 15px;
    transition: 0.2s;
    -webkit-transition: 0.2s;
}

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

Is there a way to retrieve the ID from a span and convert it into a string format?

let wordSpan = '<span class="we w_o_r_d_s" id="we" contenteditable="false">we</span>' console.log(wordSpan.id); console.log(wordSpan.attr('id')); console.log(wordSpan.getAttribute('id')); let newDiv = document.c ...

Guide to displaying a pop-up modal containing text and an image when clicking on a thumbnail image

Recently delving into Bootstrap 3, I created a thumbnail grid showcasing images related to various projects. My goal is to have a modal window pop up when clicking on an image. Within the modal, I want to display the selected image alongside some descrip ...

When PHP is connected to the database, Ajax remains inactive and does not perform any tasks

I am currently working on setting up a simple connection between JavaScript and my database using ajax and PHP. The goal is for JavaScript to receive a name from an HTML form, make changes to it, send it to PHP to check if the name already exists in the da ...

Reset input fields while retaining placeholder text

Seeking advice on how to use this handy jQuery tool properly: $('.myDiv input').each(function () { $(this).val(""); }); Though it clears my form, I'm struggling to maintain the placeholders of the inputs. Any suggestions? C ...

Tips for using jQuery to adjust HTML attributes based on conditions

I'm still trying to figure out how to assign a different class attribute to an element based on another element's dynamic numberical attribute. Here is the HTML & PHP code I am working with: <?php for($i=1;$i<=$total;$i++){ ?> <a hr ...

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 ...

The span is unresponsive

When focusing on an input or checking its validity, the span should move up but it's not responding to any styles. <div class="login"> <span class="logtxt">Username or email</span> <input type=& ...

What could be causing the DIV to be out of place when the window is resized?

When the window is minimized, all elements remain intact except for the navigation. https://i.sstatic.net/xewNu.jpg The image below shows the layout when the window is maximized, while the image above depicts the layout when the window is minimized. HTM ...

Display a sneak peek on a separate tab

I have an editor on my website where users can input and edit their own HTML code. Instead of saving this to a database, I want to display the current user's HTML code in a new window using JavaScript. How can I achieve this without storing the code p ...

What is the best way to apply CSS styles to a child div element in Next.js?

I'm currently working on enhancing the menu bar for a website project. Utilizing nextjs for my application I am aiming to customize the look of the anchor tag, however, encountering some issues with it not rendering correctly. Here is a snippet of ...

The issue of Jquery .click event handler failing to work for class selector specifically in CHROME

Is there a way to achieve the same opacity effect on click as on mouseover? I've tried various selectors without success. Any help on this issue would be greatly appreciated. Thank you in advance! .container { position: absolute; background:url(&a ...

What is the best way to exchange configuration data among Javascript, Python, and CSS within my Django application?

How can I efficiently configure Javascript, Django templates, Python code, and CSS that all rely on the same data? For example, let's say I have a browser-side entry widget written in Javascript that interacts with an embedded Java app. Once the user ...

Resolve issues with vue js checkbox filtering

As I embark on my Vue journey, I came across some insightful queries like this one regarding filtering with the help of computed(). While I believe I'm moving in the right direction to tackle my issue, the solution has been elusive so far. On my web ...

Is there a way to horizontally center a content container in Flutter similar to a "max-width" container in CSS?

How can I create a centered content box like this in Flutter?: .content-box { margin-left: auto; margin-right: auto; width: 100%; max-width: 600px; background-color: blue; height: 100vh; } <div class="content-box"> Cont ...

challenge encountered while trying to remove ScrollTop feature on mobile gadgets

Currently facing a challenge with responsive design - I have implemented a sticky navbar effect that works seamlessly on desktop, but causes usability issues on mobile. When scrolling down on the mobile version, the sticky navbar obscures the text on the s ...

I am encountering a problem with the $invalid property in AngularJS

Hey there, I've got a coding dilemma involving three number inputs, which I will refer to as the first input, second input, and third input for clarity. Here's the setup: <div> <form name="formOpenMax"> <div clas ...

What is the best way to create spacing between images in a CSS image slider?

UPDATE: I've made significant progress by modifying the code provided in the link below. I am very close to achieving my desired result, but there are parts of it that still puzzle me. The transition now has spacing on both sides and the hidden space ...

Ajax immediately going to the error section

I'm having trouble with my ajax as it always goes straight to the error part. Below is my code along with comments on the lines I'm unsure about: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script&g ...

What are some ways to utilize the pseudo elements ::before and ::after within the Material-UI framework?

Can someone please explain how to use the pseudo-element method in MUI? I couldn't find clear instructions in the documentation, so I decided to post a question here. The docs mention the following about pseudo elements: The box-sizing property is ...

Modify the appearance of the datepicker and set the field to be view-only

As a beginner in jquery and the date picker, I'm struggling to change the date format to YYYY-MM-DD and restrict users from typing in the field, allowing only date selection. I have tried various methods without success. Any help or guidance would be ...