Centering the viewport on a spacious website

Currently, I am working on developing a horizontal website that spans 16000px using a specific template that can be found at the following link: http://tympanus.net/Tutorials/WebsiteScrolling/

The website is divided into four sections, each 4000px wide. My goal is to create a centered div within the browser's viewport for each section.

Vertically, achieving 50% alignment is not an issue. However, horizontally, 50% would place the div at 2000px into the page.

Does anyone have any suggestions on how to make this work?


I have managed to center my #verycentre div on the first page using a script. However, I am struggling with centering a window div at each section anchor.

As the page scrolls across and reaches each section, I am attempting to create a resizable #verycentre div for each section.

The current code that I am using works perfectly for the first centered div in section 1. I suspect that the issue may be due to the function call being made on document.ready.

At that time, there is only one #verycentered div in the window, which I believe is causing the problem. How can I ensure that all #verycentered divs at each anchor point are centered and resizable?

Thank you for your help.

Answer №1

Aligning a div to the center using CSS can be tricky when the width offsets the window area. One alternative solution is to utilize a JQuery function to help center the div based on the window position.

For more information on how to center an object based on the window area display, refer to this informative post.

Learn more about using jQuery to center a DIV on the screen

Answer №2

To achieve the desired effect, apply a negative left margin along with setting the position to relative:


div{
 position: relative;
 left: 50%;
 margin-left: -2000px;
}

Answer №3

<script type="text/javascript">
$(document).ready(function() {
function adjust_position() {
    let win_width = $(window).width();
    let win_height = $(window).height();    

    let obj_width = $('#centered-element').width();
    let obj_height = $('#centered-element').height();

    $('#centered-element').css('top', (win_height / 2) - (obj_height / 2)).css('left', (win_width / 2) - (obj_width / 2));
    }

    adjust_position();

$(window).resize(function() {
    adjust_position();
});

});
</script> 

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

Separate the information into different sets in JavaScript when there are more than two elements

Upon extraction, I have obtained the following data: ╔════╦══════════════╦ ║ id ║ group_concat ║ ╠════╬══════════════╬ ║ 2 ║ a ║ ║ 3 ║ a,a ...

Creating a dynamic webpage using Javascript that responds to user clicks on page links

I am interested in the process of inserting JavaScript objects from a JSON file in order to dynamically create a unique page based on a user's clicked link. This concept is similar to having a wildcard page in popular frameworks like Laravel or Django ...

css: Aligning fonts horizontally when they have varying sizes

First time asking a question on this platform! I'm attempting to achieve a "pixel perfect" horizontal alignment of two lines of text, with each line having a different font size. <style type="text/css"> * { font-family: sans-serif;} ...

What is the most effective way to identify mobile browsers using javascript/jquery?

As I develop a website, I am incorporating image preloading using JavaScript. However, I want to ensure that the preload_images() function is not called for users on slow bandwidth connections. In my experience, the main demographic with slow internet spe ...

Initiate and cease automatic refresh with AJAX

Currently, I am in the process of developing a school e-learning forums module. The posts within this module are loaded in the following <div id="posts"></div> using a JavaScript function: var refreshcon = setInterval(function() { $('#po ...

Generate a download link for the image being shown in a div element

I am working on a project where I need to display HTML content offline within a Windows Application. The application, developed in Autoplay Media Studio, includes an iexplore box to load the HTML. Before the application loads, I have set up a silent insta ...

A guide on rendering a JSON array received from a URL using AJAX in AngularJS

Upon receiving a JSON array from the following URL: , it appears like this:https://i.stack.imgur.com/1Xrf4.png Within my AngularJS controller, I have the following code: $('#example-1').DataTable({ "ajax" : 'http://blahblahbla ...

PHP seems to be resistant to receiving data from ajax requests

I am attempting to develop a drag and drop file upload feature without using a traditional form, utilizing JavaScript's FormData. However, I am encountering an issue where PHP does not seem to be receiving the uploaded file. Could there be some missin ...

What is the best way to handle the return value from using indexOf on a string?

I am looking to manipulate the value returned by a specific conditional statement. {{#each maindata-hold.[2].qa}} <div class="section"> <a href="javascript:void(0)" class="person-link" id="{{id}}"> <span class="name- ...

Issue with Bootstrap CSS not rendering properly in Chrome and Firefox

After creating a simple "Hello World" web page and including Bootstrap CSS from a CDN, I noticed that it's only working properly in Microsoft Edge. Both Google Chrome and Mozilla Firefox are not displaying the correct output. I would greatly apprecia ...

Enhancing JQuery Validation by incorporating a bespoke method for submission validation

Once the form is submitted, I need a simple js/jquery function to perform additional validation. My form supports multiple file inputs, and I must ensure that the combined total size of all files does not exceed a predefined file limit. var totalFileSize ...

presentation not transitioning smoothly as the next slide appears

Initially, my aim is to center the slideshow on the page but I am uncertain about how to achieve it. This is how my current webpage appears: https://i.sstatic.net/E6bzQ.png Furthermore, as shown in the image, the sliding effect of the blue slide is movi ...

Toggle class on element based on presence of class on another element

If I have 4 boxes and the user is required to choose one. I aim to assign an active class to the box that the user clicks on. However, if the user selects another box, I want to remove the class from the first clicked box and apply it to the newly clicked ...

The grid expands to cover the entire width of the browser

Hello everyone, I am a newbie when it comes to JavaScript and CSS. I am in need of a code, whether it be JavaScript or CSS, that will allow me to stretch out a grid layout measuring 5 x 2 along with pictures to completely cover the width of the browser wi ...

Is it possible to combine Bootstrap 2.3.1 with newer versions such as 4.0 or 3.x.x?

My current website is built using Bootstrap 2.3.1, but now I am looking to update to a new version of Bootstrap. However, when I tried updating, the entire UI changed drastically and the website started to look clumsy. Instead of completely replacing the ...

Struggling to perfectly align text in the middle of a div container that also has

Seeking help with aligning text vertically within a div that also contains an image. Previous attempts with CSS properties like vertical-align: center; and line-height:90px have proven ineffective. Removing the image allows for the successful use of line- ...

Using 2 viewports depending on the device

My website looks great on all devices and desktop, but I want to set a different viewport for iPad. I would like the iPad to have the same width as my desktop version, which is 1100px wide. Currently, the site is designed with percentages and ems up to (ma ...

The Material UI button shifts to a different row

I need help adjusting the spacing between text and a button on my webpage. Currently, they are too close to each other with no space in between. How can I add some space without causing the button to move to the next line? const useStyles = makeStyles((the ...

Add custom CSS styles to a webpage using the CSS Style element retrieved from

I am working on an HTML page that has a TextArea and label. My goal is to create CSS classes in the TextArea and apply them to the label: <textarea id="txtCSS" name="txtCSS" rows="4" cols="50"> .FC{color:gr ...

How to customize Material UI Autocomplete options background color

Is there a way to change the background color of the dropdown options in my Material UI Autocomplete component? I've checked out some resources, but they only explain how to use the renderOption prop to modify the option text itself, resulting in a a ...