Problem encountered when attempting to insert a new division into an existing flexbox container

I'm in the process of designing a basic login page using HTML/CSS. To center the box on the screen, I opted for a flexbox layout. So far, I have successfully positioned the Username/password fields and login button just the way I want them. The only thing left to do is add a title to the page. However, whenever I try to introduce a div element to contain the title within the flexbox container, it disrupts the positioning of the other elements.

Below is the HTML code for the login page with the title div that I wish to include commented out to demonstrate the current functionality:

html {
  margin: 0px;
  height: 100%;
  width: 100%;
}

body {
  margin: 0px;
  min-height: 100%;
  width: 100%;
}

#container {
  display: flex;
  flex-flow: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 700px;
}

#loginBox {
  height: 400px;
  width: 400px;
  background-color: white;
  box-shadow: rgba(0, 0, 0, .4) 0px 3px 8px;
}

#usernameBox {
  background-color: white;
  position: relative;
  top: 35%;
  left: 10%;
  width: 80%;
  height: 10%;
  font-size: large;
  border: 1px groove black;
  border-radius: 5px;
  text-indent: 10px;
}

#passwordBox {
  background-color: white;
  position: relative;
  top: 44%;
  left: 10%;
  width: 80%;
  height: 10%;
  width: 80%;
  font-size: large;
  border: 1px groove black;
  border-radius: 5px;
  text-indent: 10px;
}

#loginButton {
  position: relative;
  left: 58%;
  top: 53%;
  text-align: center;
  width: 130px;
  height: 40px;
  border-radius: 8px;
  background-color: rgba(0, 102, 72, 1);
  color: white;
  border: .5px solid black;
  font-size: small;
}
<div id="container">
  <div id="loginBox">
    <!-- <div id="title">Log in</div> -->
    <input id="usernameBox" placeholder="Username"></input>
    <input id="passwordBox" placeholder="Password"></input>
    <button id="loginButton" type="submit">Log in</button>
  </div>
</div>

The image below depicts the appearance prior to the addition of the title div:

https://i.sstatic.net/r0Uxw.png

Upon uncommenting the html code containing the title:

https://i.sstatic.net/CQf69.png

My question now arises as to how could I integrate the title into the "loginBox" while preserving the existing formatting of the Username, password fields, and the login button?

Answer №1

The issue at hand stems from setting fixed dimensions for the positions of your input elements.

To resolve this, consider utilizing the flex property and incorporating margin-top to the various elements in the following manner:

#container {
  display: flex;
  flex-flow: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 700px;
}

#loginBox {
  height: 400px;
  width: 400px;
  background-color: white;
  box-shadow: rgba(0, 0, 0, .4) 0px 3px 8px;
  display:flex;
  flex-direction:column;
  justify-content:center;
  align-items:center;
}

#usernameBox {
  background-color: white;
  position: relative;
  width: 80%;
  height: 10%;
  font-size: large;
  border: 1px groove black;
  border-radius: 5px;
  text-indent: 10px;
}

#passwordBox {
  background-color: white;
  position: relative;
  width: 80%;
  height: 10%;
  width: 80%;
  font-size: large;
  border: 1px groove black;
  border-radius: 5px;
  text-indent: 10px;
}

#loginButton {
  position: relative;
  text-align: center;
  width: 130px;
  height: 40px;
  border-radius: 8px;
  background-color: rgba(0, 102, 72, 
    1);
  color: white;
  border: .5px solid black;
  font-size: small;
  align-self: end;
  margin-right: 35px;
}

#loginBox > *{
  margin-top:20px;
  
}
<div id="container">
  <div id="loginBox">
    <h1>This is the Header!</h1>
    <input id="usernameBox" placeholder="Username"></input>
    <input id="passwordBox" placeholder="Password"></input>
    <button id="loginButton" type="submit">Log in</button>
  </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

Show the modal content in Bootstrap on mobile and tablet screens

I have exhaustively searched the web for a solution to my issue, but unfortunately, I have come up empty-handed. I am hoping that you can offer some guidance in this matter. I am currently utilizing modals to showcase details on my website. However, when ...

Achieve a full height for children without the need to specify a fixed height for the

I'm currently working on a container div that houses two child divs: the first one, positioned to align with its parent's left border, contains a series of buttons while the second one holds the main content. In essence, it operates like a tab na ...

A pop-up window for selecting a file within an <a> tag

Is there a way to create an Open File dialog box on a link within my webpage? I attempted <input name="uploadedfile" type="file"> However, it only functions as a button and does not allow for the selection of multiple files. I would like somethin ...

Which characters are permissible for the id attribute to prevent the jQuery selector from throwing an exception?

I am facing a situation where the id attribute is inputted by an end user. For instance, if the id for a textbox is "11_=11" as entered by the user, then the HTML code will appear like this: <input type="text" id="11_=11"> The corresponding jQuery ...

Central alignment of div with cursor

I'm experimenting with creating a unique custom cursor using a <div> that trails the movement of the mouse pointer. While the current setup works smoothly, I've noticed that when scrolling down the page, the div lags behind until the scrol ...

Troubleshooting media queries on an example webpage

Hey there! So, I'm having a bit of trouble with running media queries on a simple HTML page. It's been a while since I worked on this stuff, and I feel like I must be doing something silly that I can't quite pinpoint. If you want to take a ...

Troubleshooting: AngularJS routing issue

I'm facing an issue with my AngularJS routing code. Here is the code snippet: /// <reference path="C:\Users\elwany\documents\visual studio 2015\Projects\spaapplication\spaapplication\scripts/angular.js" /& ...

I am attempting to create a stylish border for the .navbar-collapse element, but unfortunately, my efforts have been unsuccessful so far

I need assistance with adding a border around my mobile drop-down menu. I've tried using .navbar-collapse but it's not working. Any suggestions? Here is a sample image of the mobile size: Below is my code: <nav class="navbar navbar-default ...

Replacing an array element in React.js

Here is a React.js code snippet where I am trying to utilize an array called distances from the file Constants.js in my Main.js file. Specifically, I want to replace the APT column in the sampleData with a suitable value from the array distances extracted ...

Modify the table layout by incorporating images within separate div containers for each row

In my layout, there is a grey box element that contains a table with rows of small images. I want the table to automatically adjust based on the number of images so that when the grey box reaches its maximum size, the images move to the next row. Is this a ...

What methods can be used to ensure a div reaches all the way down to a sticky footer?

I'm currently working on a layout with a sticky footer, and I have a specific div that needs to extend all the way down to the footer. However, simply setting the height to 100% doesn't achieve the desired result. I've also experimented with ...

Trouble with JavaScript loading in HTML webpage

<!DOCTYPE html> <html> <head> <title>Breakout Game</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <canvas width="900" height="450" class="canvas"></canvas> ...

Ways to center vertically aligned buttons within cards in a React application with Material-UI

I've encountered an issue with my ReactJS project using material-ui. I created 3 cards, each with a paragraph of varying lengths. This caused the buttons to be misaligned vertically in each card, as the position differs due to paragraph size differenc ...

Creating a stylish CSS shadow effect for a collection of elements

In my Ionic2 project, I have an <ion-list> consisting of various items. Currently, these items are plain, but I desire to add some styling such that each item in the list displays a subtle shadow effect, similar to the one shown in the first image of ...

Glitch with menu presentation in Safari browser

I'm currently working on a website for a client who specifically wants a traditional, non-responsive design. The site has been successful over time, but I've encountered an issue with the menu display in Safari. While it looks fine on other brows ...

Creating a central navigation menu for a website

Currently, I am working on incorporating a menu in the center of a webpage (please note that this is not a top navigation menu). Here is the initial setup: Users are able to click on various menu items to access different content. I have written code fo ...

Using JQUERY to fadeIn elements when clicking on a button and loading content from an external HTML

On my webpage, when a user clicks on a navigation link, instead of changing to a new page, the content from the linked pages loads into a div on the main page using JQuery and .load function. Both tests worked perfectly with this implementation. Now, I wa ...

Stop images within contenteditable divs from being easily dragged and dropped into unrelated div elements

In order to maintain data integrity, I have implemented a rule where users are allowed to drag images within a specific div. Later on, I need to retrieve the positions of these images within the div. However, it is crucial for my business requirements tha ...

Why is the Google Map missing from the Bootstrap modal dialog?

I have multiple links on my website, each describing a different location with unique map data. I would like to display a modal bootstrap dialog with an embedded Google map when a user clicks on any of the links - ensuring that the location shown correspon ...

Endless Google Locations Instances

My database entries are being displayed in a table using input fields. I want the Location field for each entry to be automatically completed using Google's API. HTML: <input name="edit_airplane[1][location]" type="text" class="airplane_location" ...