What is the best way to incorporate white space in Bootstrap until a specific element is at the top of the screen?

I want to enhance my website design in Bootstrap 4 by having an image appear at the top of the user's screen when a certain button or image is clicked. This can be achieved by adding white space to the bottom of the website so that the image remains visible at the top even when scrolling all the way down. I have already figured out the JavaScript function for scrolling, but how can I dynamically add whitespace to the website until the image is positioned at the top of the screen? My current solution involves setting fixed margins for a specific element in CSS and making it visible upon clicking, but I need this to work for various viewport sizes.

scrollingElement = (document.scrollingElement || document.body);

function scrollSmoothToBottom(id) {
 $(scrollingElement).animate({
    scrollTop: document.body.scrollHeight
 }, 500);
}
window.onload = function(){
 // Hide whitespace initially
 document.getElementById("whiteSpace").style.display = "none";
 document.getElementById("submitButton").addEventListener("click", function() {
 // Make whitespace visible and scroll to bottom on button click
 document.getElementById("whiteSpace").style.display = "block";
 scrollSmoothToBottom();
 });

}
.space{
 margin:520px; 
}
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <center>
        <h1 id  = "collegeNameTitle">Name</h1>
        <h3>Student Size</h3>
         <span class="badge badge-success even-larger-badge" id = "collegeStudentSizeText">10</span>
         <h3>Acceptance Rate:</h3>
          <span class="badge badge-success even-larger-badge" id = "collegeAcceptanceRateText">10</span>
          <h3>Student-Faculty Ratio:</h3><span class="badge badge-success even-larger-badge" id = "collegeRatioText">10</span>
        </center>
<center><button id = "submitButton"><img class = "collegeImage"src="sample.png" id  = "collegeImage"></button></center>
<div id = "whiteSpace" class ="space" visibility = "hidden">
  </div>

Currently, my website code has fixed measurements for spacing in CSS, but I would like to have a dynamic approach where there are no fixed measurements and the whitespace adjusts until the image reaches the top.

Answer №1

If you simply desire your img to move to the top of the screen upon clicking a button, you can experiment with the following code snippet:

const myImg = document.getElementById("myImg");
const myBtn = document.getElementById("myBtn");
myBtn.addEventListener("click", function(){
  myImg.style.position="fixed";
  myImg.style.top=0;
});
body{
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-item: center;
  justify-content: center;
}
img{
  height: 5rem;
  width: 100%;
}
<button id="myBtn">Button</button>
<img id="myImg" src="https://www.w3schools.com/css/paris.jpg">

If CSS positioning is unfamiliar to you, it may be helpful to refer to this documentation.

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

How can you create a blurred effect on a navbar?

Looking to achieve a unique blur effect in the background of my navbar component. The standard CSS blur effect isn't giving me the desired result, so I'm seeking some guidance on how to customize it. For example: https://i.sstatic.net/bPEXZ.png ...

Enhancing Bootstrap with VueJS for better component ordering

I've been struggling with Vue components in Bootstrap lately. I am attempting to create collapsible Bootstrap content in Vue, and here is the current code: HTML <div class="col-sm main-content" id="main-content"> <p&g ...

Retrieve a DOCX file via AJAX response

I am encountering an issue with a Django function that returns a file: ... return FileResponse(open('demo.docx', 'rb')) I am using ajax to fetch it on the client side. Now, I need to download it on the client side. This is the code I a ...

Using Filters in VueJs

I am currently working on creating a small Vue.js 2.0 application where I am utilizing the v-select component from here. The data format that I am dealing with looks something like this: { "model": [ { "id":1, ...

Align text to the right and left side

HTML file: <div id="home"> <b><p class="split-para">java<br><a href="j_temp.html" class="float">temperature converter in terminal</a> <br> <a href="j_palindrom.html" class="float">palindrome checker</a& ...

Error retrieving individual user information from the list via REST API

routes user.js view layout.ejs user.ejs users.ejs app.js REST API - "" I am facing an issue with two GET requests. In users.ejs, I am rendering three dynamic buttons with specific href values. In user.ejs, I am rendering a single user. ...

Guide on efficiently inserting multiple records into a MySQL database using Node.js and also implementing a check to insert only when the record does not already exist

I need to add numerous records to a table while also checking for duplicates to ensure that only new records are inserted. I have successfully inserted a single record by checking for duplicates using the code below. connection.query(` INSERT INTO pla ...

Unable to adjust the margin-bottom attribute in CSS

Currently, I'm in the process of developing a website and attempting to build the client-side without using any frameworks. However, for some reason, I am facing difficulty in changing the margin-bottom property. My goal is to create space between the ...

Can text be replaced without using a selector?

I am utilizing a JavaScript library to change markdown into HTML code. If I insert <span id="printHello></span> within the markdown content, I can still access it using getElementById() after conversion. However, there is one scenario where th ...

Loading a div using Ajax within a frame

My php page includes a div that is supposed to be populated by an Ajax call. function showcopay() { var apa = document.getElementById("alert_id").value; $("#copay").load('show_copay.php?pid='+apa); } The parent page of the div used to be ...

Sending POST parameters via Jquery POST from a Java Servlet to Javascript

My JavaScript code initiates a POST request on my Servlet. $.post("RecipeServlet", { r_id: r_id, }, function(data, status){ var foo = data.foo; var bar = data.bar; }); Now, the Servlet is expected to work with the received r_id and then send ...

What is the best way to manage json-parse errors in a node.js environment?

After countless hours of research, I am still unable to find a solution to my seemingly simple and common problem: In Node.js using Express, I want to retrieve JSON-data via http-POST. To simplify the process, I intend to utilize the app.use(express.json( ...

Slideshow Display Suddenly Halts at the Last Image

I am currently working on a Bootstrap JS Carousel that showcases several images stored in the project folder. Each image's filepath is retrieved from an SQL Server database and displayed within the carousel. While the images are displaying correctly ...

Accessing a modal in Bootstrap 4 by clicking on an anchor link will automatically open the

I am implementing a bootstrap 4 modal with anchor tags that trigger the opening of the modal. Here is an example structure: <div class="experience"> <div class="container"> <div class="row"> <div cl ...

Validation for dates in Angular.Js input is important to ensure that only

Take a look at this form: <form name="user_submission" novalidate="novalidate" method="post"> <input type="date" name="date_of_birth" ng-focus="save_data()" ng-model-options="{timezone: 'UTC'}" ng-pattern="/^(19\d{2}|[2-9]& ...

Text Parallax Effect

For my website, I am interested in incorporating a unique parallax effect. Instead of just fixing a background image and allowing scrolling over it, I want to apply this effect to all of the content on my site. The website consists of a single page with m ...

The Label in Material UI TextField is appearing on top of the border,

Incorporating Material UI TextField and Material UI Tab, I have encountered an issue. There are two tabs, each containing a text field. Upon clicking on the TextField, the label's border is supposed to open, but this behavior only occurs if the curren ...

Trigger the click event on the ul element instead of the li element using jQuery

Is there a way to make the click event only affect ul tags and not all li elements using jQuery? <!-- HTML --> <ul class="wrap"> <li>test1</li> <li>test2</li> <li>test3</li> </ul> I attemp ...

Using single quotation marks within a string can cause issues in javascript

I am curious about how to handle single quote marks and other symbols within user-generated text without causing issues with the JavaScript code. Currently, if a user enters a title like "I wouldn't", it breaks the JavaScript functionality. <a cl ...

Two pretensions and an evaluation in an if statement

Currently, I am delving into the well-optimized code for voronoi implementation created by Mike Bostock (d3js). I find myself puzzled by the following snippet of code: if (!(m = (halfedges = cell.halfedges).length)) return; You can view the full code he ...