How come my div elements are overlapping as I resize the browser window?

While attempting to design a straightforward login form, everything appears to be working fine until the browser window is resized. The setup involves three <div> elements: center, center2, and center3, all enclosed within main. Additionally, there's a background image represented by bgpc.

Upon resizing the browser, the 3 center divs start overlapping each other, causing misalignment.

Various attempts have been made to address this issue by setting positions to either absolute or relative in both the wrapper and the individual center divs, yet no solution has proved effective.

#bgpc {
  float: bottom;
  height: 100%;
  background-position: absolute;
  background-repeat: no-repeat;
  background-size: cover;
  z-index: -1;
  filter: blur(3px) brightness(50%);
  background-image: url("../images/background1.gif");
  /*background-image: url("../images/home_alt.png");*/
  /*background-image: url('../images/backTest.PNG');*/
}

#center {
  position: absolute;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #35ada7;
  padding: 5px;
  padding-left: 50px;
  padding-right: 50px;
  padding-bottom: 10px;
  font-size: 180%;
  filter: drop-shadow(8px 8px 10px gray) opacity(75%);
  color: white;
  display: block;
}

#center2 {
  position: absolute;
  top: 80%;
  left: 50%;
  transform: translate(-50%, -50%);
  filter: drop-shadow(8px 8px 10px gray) opacity(75%);
  display: block;
}

#center3 {
  position: absolute;
  top: 62%;
  left: 50%;
  transform: translate(-50%, -50%);
  filter: drop-shadow(8px 8px 10px gray) opacity(75%);
  display: block;
}

#main {
  float: bottom;
}
<div id="main">
  <div id="bgpc"></div>
  <div id="center">
    <h1>MyCalc</h1>
    <b>Si</b>mple <b>Cal</b>culator
  </div>
  <div id="center3">
    <center>
      <h1 style="color: white">Log in to use the<br> Calculator</h1>
    </center>
  </div>
  <div id="center2">
    <form method="post" action="testAspx.aspx" class="login-form">
      <div class="input-container">
        <i class="fa fa-envelope"></i>
        <input type="email" id="userName" class="input" name="email" style="background-color: transparent; color: white" placeholder="Username" />
      </div>
      <div class="input-container">
        <i class="fa fa-lock"></i>
        <input type="password" id="loginPassword" class="input" name="password" style="background-color: transparent; color: white" placeholder="Password" />
        <i id="show-password" class="fa fa-eye"></i>
      </div>
      <input id="submit" type="button" name="login" value="Login" onclick="Schleife(0)" class="button" />
    </form>
  </div>
</div>

Answer №1

It is recommended to nest your elements in the following structure:

  -- center
     --center-3
     --center-2

Additionally, remove absolute positioning on center-2 and -3 to let the parent handle the positioning.

Simplifying further, consider inline content without center-2 and center-3, utilizing flexbox for centering instead.

.center {
  display: flex;
  flex-direction: column;
  max-width: 300px;
  box-shadow: 9px 8px 10px 0px rgba(158, 155, 158, 1);
  padding: 5px 0 10px;
  flex: 1;
}

.column {
  padding: 0 16px;
}

.titles {
  background-color: #35ada7;
  color: white;
}

.main {
  display: flex;
  justify-content: center;
  align-content: center;
}
<div class="main">
  <div class="center">
    <div class="titles column">
      <h1>MyCalc</h1>
    </div>
    <div class="column">
      <p>Simple Calculator</p>

      <h1>Log in to use the<br> Calculator</h1>
      <form method="post" action="testAspx.aspx" class="login-form">
        <div class="input-container">
          <i class="fa fa-envelope"></i>
          <input type="email" id="userName" class="input" name="email" style="background-color: transparent;" placeholder="Username" />
        </div>
        <div class="input-container">
          <i class="fa fa-lock"></i>
          <input type="password" id="loginPassword" class="input" name="password" style="background-color: transparent;" placeholder="Password" />
          <i id="show-password" class="fa fa-eye"></i>
        </div>
        <input id="submit" type="button" name="login" value="Login" onclick="Schleife(0)" class="button" />
      </form>
    </div>

  </div>
</div>

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 I add color to arrow icons in tabulator group rows?

Is there a way to specify the color of the arrows in collapsed/expanded group rows? Also, what CSS code should I use to define the font color for column group summaries? You can view an example of what I am trying to customize here: https://jsfiddle.net/s ...

Assigning value to a member variable in a TypeScript Angular class

Currently, I am in the process of learning Angular. To enhance my skills, I am developing a simple web application using Angular and Spring Boot. One challenge I encountered is assigning a variable to the member variable of a Class. import { Injectable } f ...

Who is responsible for triggering a PostBack?

I am trying to identify the control that triggers a page PostBack within the Page_Load event, as there are multiple controls on my page. ...

What is the process of transforming async/await code into synchronous code in JavaScript?

Blocking the event loop is generally considered bad practice due to its consequences. However, even the native fs module includes some synchronous functions for specific purposes, such as CLIs using fs.readFileSync. I am interested in converting the follo ...

What is the best way to trigger a JavaScript onclick event for a button that is located within a dropdown menu on an HTML page

I've implemented a feature that resizes the font size within a text area based on the selected font size. It was functioning flawlessly... until I decided to place the font size selection in a drop-down menu, which caused the feature to stop working. ...

Issue with Bootstrap Carousel function in desktop browsers but functioning correctly on mobile devices

I'm experiencing issues with my bootstrap carousel on mobile browsers, but not on web browsers, despite trying various fixes and clearing my browser cache. Specifically, there is no sliding effect when clicking on the indicator, and everything else ap ...

The Owl carousel seems to be malfunctioning as there are no error messages displayed and the div containing it disappears unexpectedly

I'm having an issue with Owl carousel. I've included the necessary owl.carousel.css, owl.carousel.js, and owl.theme.css files. Created a div with the class "owl-carousel", and called the function $(".owl-carousel").owlCarousel();. However, after ...

Retrieve a specific field from the latest entry in MongoDB

I'm having trouble retrieving the _id field of the most recent document added to my collection. The collection I've set up is called Rooms: Rooms = new Meteor.Collection('rooms'); This snippet should fetch the latest item with a limi ...

What is the best way to preload a font with a hashed filename and style hosted on a CDN?

Is there a way to preload the Material Icons font in advance? <link rel="preload" href="https://fonts.gstatic.com/s/materialicons/v125/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2" as="font" type="font/woff2" crossorigin=&q ...

Narrow down your search results with date range filtering in Angular Material

Seeking guidance as a newcomer to Angular Material, I am trying to implement a date range filter for a table of N results. The options in the filter select are (1 day, 5 days, 1 week, 15 days), which are populated using a variable JS vm.rangos=[ {id ...

What is the best way to retrieve variables from child components within a parent component in React?

I'm currently diving into React by working on a form/calculator application. I've come to realize that React follows a unidirectional pattern, and I'm struggling with how to deal with it. My goal is to create a table where users can input i ...

Seamless Integration of Hosted Fields by Braintree

I am currently struggling with setting up Braintree hosted fields on my registration form. Unfortunately, there are significant gaps between the fields which doesn't look appealing. Despite referring to the braintree documentation and guides, I find t ...

Setting the dimensions of an HTML - CSS block

I am trying to style a navigation bar using the following CSS code: #nav {} #nav a { position: relative; display: inline-block; color: #F0F0F0; width: 1em; height: 2em; line-height: 0.9em; } #nav a.ic ...

Tips for sorting through existing data without resorting to a remote call - Material Table repository by mbrn (mbrn/material

Currently I am using the mbrn/material-table library which includes filtering on a column and remote data feature. However, when I apply a filter term, the table sends an API call to the server with the filter criteria in the query object. My desired outco ...

Adding a <div> within a <form> on button click using HTML and jQuery tactics

Whenever the '+' button [the addMore() function] is clicked, I want to insert the following div structure. Although I am able to add the div structure, the alignment of the input text is not equal as compared to the hard coded version. Here is t ...

Searching for data in a bootstrap table that is not case-sensitive

Searching for and highlighting text within a bootstrap table has proven to be a challenge. Check out the Fiddle for an example: https://jsfiddle.net/99x50s2s/74/ HTML <table class='table table-bordered'> <thead> <tr& ...

Trouble with Displaying HTML Table in Bootstrap 4.3.1 Tooltip

Struggling for hours to set up a custom HTML table in a tooltip box, I finally found that the Bootstrap version solves the issue. Surprisingly, it works perfectly under Bootstrap 4.0.0 but fails under Bootstrap 4.3.1. Could this be a bug or am I overlooki ...

Populate a select list in real time with dynamically generated options

I'm facing a challenge at the moment; I am using JavaScript to dynamically generate select boxes, however, I require Ajax to populate them with options. At the moment, my code is returning undefined and I am unsure of how to proceed. While my PHP succ ...

Combining and forming a distinctive case-sensitive array in JavaScript

Explaining a complex scenario, I have one object or array and one array. My goal is to compare selectedmodel values with mappedmodels. If the value (case insensitive) matches any key in the object, I need to fetch all associated values and push them into t ...

How can I utilize a custom function to modify CSS properties in styled-components?

I am working with styled components and need to set the transform property based on certain conditions: If condition 1 is true, set the value to x. If condition 2 is true, set the value to y. If neither condition is true, set the value to null. Despite ...