What methods can be used to prevent the page body from resizing when additional elements are added?

I am having an issue with setting up the layout of my webpage. I want the body to take up 100% of the screen size, and within the body there is a mainContainer div that should be 90% of the body's dimensions.

The problem arises when I try to add a div (LoginInnerContainer) inside the mainContainer and set it to be 30% of the root div while also centering it. For some reason, the body becomes longer than necessary and the inner container does not align in the middle as expected.

How can I resolve this issue while ensuring the body retains its original screen size? Unfortunately, I am unable to upload a picture to provide visual clarity on the matter.

Here is my HTML code:

<html>
          <head>
             <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
              <link rel="stylesheet" href="<?php URL?> public/css/default.css">
              <title></title>
           </head>
           <body>
             <div id="mainContainer">
                 <header>

                </header>

                     <div id="loginInnerContainer">
                          <div id="loginLogoContainer"></div>
                          <div id="loginFormContainer"></div>
                     </div>
            </body>
 </html>

Here is my current CSS code where the #loginInnerContainer is not properly centered resulting in unnecessary lengthening of the page:

#mainContainer { 
/*    height: 590px;*/
    height: 90%;
    width: 90%;
    background-color: white;
    margin: 50px;

}
html, body{
    height: 100%;
}

html {
    background-image:url('../images/wood-dark.png');
    height: 100%;
}
body {
    width: 100%;
    min-height:100%;
    display: block;

}
#loginMainContainer{
    height: 100%;
    padding:auto 0;
}

#loginInnerContainer{
    background-color:blue;
    width: 100%;
    height: 30%;
    margin:auto 0 auto 0;

}

Answer №1

My first suggestion would be to reset the margin and padding of the entire page:


*
{
  margin: 0;
  padding: 0;
} 

Learn more here.
By doing this, default paddings and margins will be cleared.

Another issue is that you are using margin for your #loginInnerContainer. Margins are located outside the container, so it is better to use padding here. For additional information, click here.
However, the page may still appear larger than necessary...

This is because the padding also extends the container. To avoid this, you could utilize box-sizing.
Now you can set the width and height to 100% of the screen without worrying about the padding.

Check out this jsFiddle link

I hope this explanation was helpful :)

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

Ways to specify the encoding for a text iframe embedded in a webpage

Can anyone help me with document embedding in HTML? In my current project, I am directly embedding my update logs as text files into the webpage, along with a selection menu to view all the updates. However, I ran into a minor issue where the Firefox cons ...

Using jQuery, compare the values of two elements and update the class based on the comparison outcome

My objective is to retrieve data from a database, place it into an <li> element, and then highlight the larger of the two values obtained from these HTML <li> elements. I have created a jsfiddle, but I am uncertain about how to use the addCla ...

The CSS syntax definition for list styles

Inside the body of my site is this code: Here is the HTML: <ul id="navlist"> <li class="first"> <a href="/" id="current">Home</a> </li> <li> <a href="/Store/">Store</a> </ ...

Troubleshooting Flexbox: Content within a Flex Item is not displaying properly within a scrollable container

I recently embarked on converting one of my websites to flexbox and encountered an issue with vertical scrolling within a flex container. It appears that Firefox displays it correctly, while Chrome and Safari do not. You can check out the code here. . ...

Steps to retrieve data (token) from developer tools and incorporate it into a fetch Post request

Is there a simple way to extract data using dev tools and insert it into a fetch request? I am trying to make a POST request through the console, but I am struggling to correctly copy a token. I attempted to use querySelector but instead of finding the t ...

Adding audio to HTML using PHP in Internet Explorer and Firefox

I am working on developing an audio captcha system specifically for individuals with visual impairments. I have a process in place that stitches together multiple wave files, but I am encountering difficulties when it comes to embedding them in internet e ...

Issue: JavaScript code is not functional when operating on data obtained through an Ajax

I am experiencing an issue with my JavaScript code (abc.js) that is designed to run on a div element with the class of "one". While the script works perfectly fine on elements already present within the div in the HTML code, it fails to execute on data tha ...

Fully responsive header designed for optimal experience at any screen height

I am facing issues with the header and cannot seem to find a solution. My goal is to make the header span 100% of the window screen height. I need a responsive header that adjusts to 100% height, and when resizing for a smaller viewport, nothing should sho ...

What are the steps to modify the MIME type in order for the browser to correctly display a CSS

I'm encountering an issue where my CSS code is not rendering in the browser, and it seems to be related to MIME types (which I'm not familiar with). I am using EJS as a template engine. The error I see in the console is... Refused to apply sty ...

CSS malfunctioning on a single box display

Our team has been successfully maintaining a client's website for several years without any issues. Recently, we encountered a problem where one block is not displaying correctly, but strangely it only seems to affect Google Chrome: We suspect that t ...

Storing information in the concealed and interactive tabs within a tabview system

In my program, users are able to access groups through a left column list, similar to how Google Groups are displayed (seen in the image below). I would like the front-end to cache visited groups as users switch between them, so that when they revisit a g ...

Selectize Fails to Populate Options Using Ajax

ExpressJS is the platform I am using to develop a management dashboard for a community project. Right now, my main concern is integrating a modal that allows users to add new games to a database. Although I am able to fetch data remotely, I am facing diffi ...

The GLTFLoader encountered an issue with importing files outside of the module

Having some issues with the updated version of THREE.js, specifically when using the GLTFLoader. I keep encountering the error referenced above. Here is how I imported it in a separate js file: test.js import { GLTFLoader } from "three/examples/jsm/load ...

Styling HTML elements for Google custom search API json results: Best practices

I have been working with the Google Custom Search API to retrieve JSON callback results for my project. I have been experimenting with the JSON callback variables based on the recommendations from Google's documentation available here: https://github ...

adding <script> elements directly before </body> tag produces unexpected results

While following a tutorial, the instructor recommended adding <script> tags right before the </body> to enhance user experience. This way, the script will run after the entire page content is loaded. After implementing the code block as sugges ...

Creating unsightly class names using Node.js

Is it possible to automatically create random and unattractive class names? For example, is there a tool or plugin that can substitute the .top-header class with something like .a9ev within my CSS code? It would also be ideal if the class name in the HTML ...

Retrieving PHP information in an ionic 3 user interface

We are experiencing a problem with displaying data in Ionic 3, as the data is being sourced from PHP REST. Here is my Angular code for retrieving the data: this.auth.displayinformation(this.customer_info.cid).subscribe(takecusinfo => { if(takecusi ...

Ways to prevent the jQuery simple slider from transitioning slides while it is in an invisible state

There is a jQuery slider on my website that behaves strangely when I navigate away from the Chrome browser and return later. It seems to speed through all pending slides quickly when it was not visible. Now, I want the slider to pause when it becomes invi ...

Creating a div with a scrollbar limited to a maximum of 100% of the page size

<!-- ********************************************************************************************************************************************************** RIGHT SIDEBAR CONTENT *********************************************************** ...

Initiate Bootstrap 4 dropdown menus from the initial menu point

I need the dropdown menus to align with the position of the first dropdown. There are multiple dropdowns on the page. Check out the screenshot for reference. JSFiddel: https://jsfiddle.net/kfh9Lbep/ https://i.sstatic.net/hHAKz.png Any ideas on how to ac ...