Unable to reach the top while perfectly centered

I am currently struggling to create a perfectly centered popup menu that allows me to scroll all the way to the top. It seems like the transform property only affects visuals, so even though #content is set to top: 50%, I can't see everything at the top. Is there a way to keep the div centered on the window while ensuring full visibility of the content inside #content?

var toggle = false;
var p = document.getElementById("popup");

document.getElementById("show").onclick = function(event) {
  p.style.display = "initial";
  document.body.style.overflow = "hidden";
};

document.getElementById("hide").onclick = function(event) {
  p.style.display = "none";
  document.body.style.overflow = "auto";
  
};
html, body {
  margin 0;
  padding 0;
  width: 100%;
  height: 100%;
}

#popup {
  position: fixed;
  right: 0;
  left: 0;
  top: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.4);
  display: none;
  overflow-y: scroll;
}

#content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<div id="popup">
  <div id="content">
    <button id="hide">Hide</button>
    <h1>Cant See</h1>
    <p>Cant SeeCant SeeCant SeeCant SeeCant SeeCant SeeCant See</p>
    <h1>Test</h1>
    <p>test test test test test test</p>
    <h1>Test</h1>
    <p>test test test test test test</p>
    <h1>Test</h1>
    <p>test test test test test test</p>
    <h1>Test</h1>
    <p>test test test test test test</p>
    <h1>Test</h1>
    <p>test test test test test test</p>
    <h1>Test</h1>
    <p>test test test test test test</p>
    <h1>Test</h1>
    <p>test test test test test test</p>
    <p>test test test test test test</p>
    <h1>Test</h1>
    <p>test test test test test test</p>
    <p>test test test test test test</p>
    <h1>Test</h1>
    <p>test test test test test test</p>
  </div>
</div>
<div>
  <button id="show">Show</button>
  <h1>Hello World!</h1>
  <p>Hello Hello Hello Hello Hello Hello</p>
  <h1>Hello World!</h1>
  <p>Hello Hello Hello Hello Hello Hello</p>
  <h1>Hello World!</h1>
  <p>Hello Hello Hello Hello Hello Hello</p>
  <h1>Hello World!</h1>
  <p>Hello Hello Hello Hello Hello Hello</p>
  <h1>Hello World!</h1>
  <p>Hello Hello Hello Hello Hello Hello</p>
  <h1>Hello World!</h1>
  <p>Hello Hello Hello Hello Hello Hello</p>
  <h1>Hello World!</h1>
  <p>Hello Hello Hello Hello Hello Hello</p>
  <h1>Hello World!</h1>
  <p>Hello Hello Hello Hello Hello Hello</p>
  <h1>Hello World!</h1>
  <p>Hello Hello Hello Hello Hello Hello</p>
 
</div>

Answer №1

To center the #content element, try using display: flex on the parent container (#popup) instead of position: absolute, and then apply margin: auto

Here's an example:

var toggle = false;
var p = document.getElementById("popup");

document.getElementById("show").onclick = function(event) {
  p.style.display = "flex";
  document.body.style.overflow = "hidden";
};

document.getElementById("hide").onclick = function(event) {
  p.style.display = "none";
  document.body.style.overflow = "auto";

};
html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

#popup {
  position: fixed;
  right: 0;
  left: 0;
  top: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.4);
  display: none;
  overflow-y: scroll;
}

#content {
  margin: auto;
}
<div id="popup">
  <div id="content">
    <button id="hide">Hide</button>
    <h1>Cant See</h1>
    <p>Cant SeeCant SeeCant SeeCant SeeCant SeeCant SeeCant See</p>
    <h1>Test</h1>
    <p>test test test test test test</p>
    // More content here...
  </div>
</div>
<div>
  <button id="show">Show</button>
  <h1>Hello World!</h1>
  <p>Hello Hello Hello Hello Hello Hello</p>
  // More content here...
</div>

Answer №2

For a quick fix, try applying height: 100% to the CSS selector #content.

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

Sequelize: When attempting to use .get({plain: true})) method, an error is returned indicating that .get is

I'm facing a strange issue as I am able to retrieve only the values of an instance in other parts of my code without any problems. Can you spot what might be wrong with my current code? app.get('/profile', checkAuth, function(req, res) { ...

Error with Webdriver/FXDriver utils.js leading to Firefox unresponsive script issue

While running browser tests with Watir webdriver and FXDriver, everything seems to be functioning well except for one test that loads a lightbox containing a large amount of HTML. When this lightbox opens, Firefox displays a popup indicating that Utils.js ...

Navigating through a dropdown menu using Selenium in Javascript for Excel VBA - Tips and tricks

I need to access a web page using Excel VBA that is only compatible with Chrome or Firefox, not Internet Explorer. I have successfully accessed the website using Selenium, but I am having trouble navigating through the drop-down menu to reach the section w ...

AngularJS supports asynchronous validation on blur

Using Angular JS version 1.5.6, I am looking to implement asynchronous input validation that occurs only on blur. However, I am unable to use modelOption: {debounce: 500} or modelOption: {updateOn: 'blur'} due to the directive being used with oth ...

Leveraging various techniques within a single controller in AngularJS

I am seeking assistance and advice on a coding issue I am facing. I am attempting to use two methods in one controller. The first method is to display selected rooms, while the second method is to display selected pax. However, only the first method seems ...

Encountering difficulties retrieving information from an API using Angular

I am encountering an issue while trying to retrieve data from an API. When I use console.log() to view the data, it shows up in the console without any problem. However, when I attempt to display this data in a table, I keep receiving the error message: ER ...

jsGrid is failing to load within a Vue application that is utilizing static data

Struggling to implement jsGrid for a basic table with header sorting in my Javascript and Vue application. Having trouble loading the sample code with various components spread across different files. Here are the relevant parts: HTML (symbol-container is ...

Issues arise with the behavior of Bootstrap design when adapting to different screen sizes, as well as

I am currently working on designing my personal portfolio using Bootstrap. I have made some progress with the setup, but I am encountering some issues. When I resize the window, the top menu collapses into a button. However, when I click on the button, n ...

Regain the original properties after implementing a universal CSS reset

Working with older code that I must build upon always reminds me of the drawbacks of using global CSS resets. I have this outdated foo.css file that begins with * {margin:0; padding:0;} In the past, I would duplicate it to a new file called bar.css, ma ...

Looking for the optimal method to display numerous lines of text in HTML, one by one, at intervals of 60 seconds?

I'm working on a display page for my website. The main text in the center needs to change every 60 seconds. I have over 150 individual lines of text that I want to cycle through on the page. What would be the most efficient way to load all these te ...

Having a concise way to submit forms with Javascript and JQuery

Seeking a more concise way to submit form data to Electron in JSON format as a beginner in JavaScript and jQuery. How can this code be rewritten for brevity? <section> <input id="server" type="text" placeholder="Server"> <i ...

Does every part have an unidentified index?

Similar Issue: PHP: "Notice: Undefined variable" and "Notice: Undefined index" This dilemma has puzzled me for three days now, and I'm at a loss on how to resolve it as I keep receiving... Notice: Undefined index: surname in C:\xampp\ ...

The remaining visible portion of a viewport is equivalent to the height of an element

Is there a way to dynamically set a div's height so that it expands from its starting position to the end of its parent div, which is 100% of the viewport minus 20 pixels? Here is an example of how you can achieve this using jQuery: $(document).read ...

The icon is being displayed as text instead of the actual Fontawesome icon

Using jquery version 3.3.1 I am dynamically creating an anchor tag: let link = $("<a>"); link.attr("href", "#"); link.text("My anchor" + ' <i class="fas fa-people-carry"></i>'); However, when I try to display ...

Can you explain the functions of this "malicious" JavaScript code?

I came across this piece of code on a website that labeled it as "malicious" javascript. Given my limited knowledge of javascript and the potential risks involved in trying out the code on my own site, I was hoping someone here might be able to shed some l ...

Display numeric data when hovering over circles in the Google Maps API using Javascript

I recently implemented the Google Maps example code that displays a circle hovering over a city, with the size of the circle representing the population. I'm looking to enhance this feature by including numeric data display on mouseover as well. Any a ...

What is the best way to retrieve the first item in owl carousel's content?

I need help creating a slider with two sections: top and bottom. My goal is to display the content from the bottom slider on the top slider. I tried adding a class to the first item element, but it didn't work. If anyone knows how to target the first ...

Steps for preventing form submission when the username is unavailable

After checking the availability of the user name, I encountered an issue where the form still gets submitted even if the username is not available. Updated Code: <SCRIPT type="text/javascript"> $(document).ready(function(){ $("#emailch").change(fu ...

Make the text area occupy the full width of the remaining screen space

I've been struggling to make the text area fill the remaining width of the screen. Everything is set up nicely, but I just can't seem to find a solution to get it to expand and occupy the rest of the available space. I intentionally refrained fr ...

The AJAX call was successful with a return code of 200, however an error

HTML code snippet: <a href="javascript:void(0)" onclick="$.join_group(<?=$USER_ID?>, <?=$groups[$i]["id"]?>)"><?=$language["join"]?></a> JavaScript function: $.join_group = function(user_id, group_id) { var input = "u ...