What is the best way to align multiple form elements both horizontally and vertically within a frame?

Issue

I have checked out various resources, like this one and this one, but I am still struggling to center my form elements both vertically and horizontally on the screen. I have tried using flex, padding, and stretching but nothing seems to work. Even after wrapping my form elements in a div, I am unable to achieve the desired centering effect.

If anyone could provide me with guidance on how to accomplish this task, I would greatly appreciate it!

https://i.sstatic.net/u6hag.png I want the fields to be stacked on top of each other, with both fields being vertically and horizontally centered on the screen.

Code

body {
    background: #3472FF;
    color: white;
}

.closeBtn {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 2em;
    color: white;
}

.login {
    display: flex;
    justify-content: center;
    align-items: center;
}

.field {
    display: block;
}
 <button class="closeBtn"><span class="icon-x"></span></button>
    <form action="form.php" method="post">
        <div class="login">
            <div class="field">
                <input type="text" name="username" placeholder="Username"> </div>
            <div class="field">
                <input type="text" name="password" placeholder="Password"> </div>
        </div>
    </form>

Answer №1

Utilizing <code>flex-direction: column;
and height: 100% on most elements can effectively address the layout:

html,
body {
  height: 100%;
}

body {
  background: #3472FF;
  color: white;
}

.closeBtn {
  position: absolute;
  top: 20px;
  right: 20px;
  font-size: 2em;
  color: white;
}

form {
  height: 100%;
}

.login {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.field {
  display: block;
}
<button class="closeBtn"><span class="icon-x"></span></button>
<form action="form.php" method="post">
  <div class="login">
    <div class="field">
      <input type="text" name="username" placeholder="Username"> </div>
    <div class="field">
      <input type="text" name="password" placeholder="Password"> </div>
  </div>
</form>

Answer №2

Experiment with this: top: 0; right: 0; bottom: 0; left: 0; in your CSS code

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

Step-by-step guide on permanently updating the text of select options with JavaScript

Here is the code for a select option with different values: <select id="test" onchange="changeContent()"> <option>1</option> <option>2</option> <option>3</option> </select> The javascript function that chan ...

Incorporating various elements within a single column of a table

I have been attempting to include multiple table cells elements beneath each of my heading cells, but I am facing issues in getting it to work properly. For better understanding, this is a screenshot: https://i.sstatic.net/kg2XR.png My goal is to have o ...

Tips for creating an expandable div with floated content

I am looking to create a flexible div that expands based on its content. Inside this div, there are two floated left divs with fixed widths. These inner divs should also expand according to their text content. To clear the floats, there is another div wi ...

Condense everything when displaying one at a time (various divs)

I found the solution at https://getbootstrap.com/docs/4.2/components/collapse/ My query is related to collapsing multiple elements and showing only one when divided into different divs. While it works within the same div, dividing content into separate di ...

Tips for creating a clickable textbox

Does anyone know how to make a textbox clickable even when it is set as readonly. I'm looking for a way to make my textboxes clickable like buttons because I have some future plans for them. <input type="text" readonly value="Click me" id="clickm ...

What is the best way to place a p-growl element in the bottom right corner of the page?

I've been struggling to fix the positioning of my growl message at the bottom right corner by overriding the CSS classes of p-growl. Initially, I attempted to override the .ui-growl class like this: ::ng-deep .ui-growl { position: fixed; b ...

Customize the background color of a Material UI row with varying colors for each cell

I am facing an issue with a Material UI table in React 18 where each cell has a different background color, causing the hover style on the row to break. Despite trying various solutions, I have been unable to find a resolution. For reference, you can see ...

Leveraging $implict for transferring several parameters

Need help with creating a recursive template that can accept multiple parameters: <ng-container *ngTemplateOutlet="testTemplate; context: {$implicit:jsonObj1}"> </ng-container> <ng-template #testTemplate let-Json1> </ng-template> ...

Turn on and off jquery sorting with a simple click

One issue I'm facing is with a button that I toggle on click. This button is supposed to enable the jquery sortable class on a list in my html named "subtaskTaskList". Despite being able to toggle the button, I can't seem to get the sortable enab ...

Tips for concealing the currently playing track title on a media player

I'm looking for a way to conceal the track title displayed in the media player on Internet Explorer. Here's the code I currently have: <object id="mediaPlayer" width="320" height="240" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" type ...

A step-by-step guide to triggering a click event using CSS

This code snippet is working as expected: .rightone ul { list-style: none; padding: 0px; margin: 0px; } .rightone ul li { display: block; position: relative; float:right; } .rightone li ul { display: none; } .rightone ul li a ...

How to effectively use flexbox to resize child elements in a CSS layout

My goal is to create a layout where a child element fills the entire flex box of a flex layout. Here is an example of the HTML structure I am using: <div class="parent"> <div class="child1"> should have 100px height </div&g ...

Leveraging $last in Angular for obtaining the final visible element with ng-show

My ng-repeat container div contains multiple images, each with a corresponding div next to it. Using $last, I can make sure only the div of the final image is visible after all images. However, I also have a filter button on the page that hides certain im ...

"Clicking on the 'View More' button within a foreach loop is only functional for the

I am trying to iterate through a list of items using a foreach loop, and each item has a "view more" option. The intention is that when I click on this option, it should expand to show more details about the respective item. However, I am encountering an i ...

HTML: Launch Frameset in a Separate Tab or Window

I have encountered an issue with using FRAMESET. Situation: Within my frameset, I have a menu with 5 links. When I click on the home page, everything looks fine. However, when I open it in a new tab or window, the design is not displayed. Is there a way ...

Tips for ensuring that divs resize to match the container while preserving their original proportions:

#container { height: 500px; width: 500px; background-color: blue; } #container>div { background-color: rgb(36, 209, 13); height: 100px; } #one { width: 1000px; } #two { width: 800px; } <div id="container"> <div id="one">&l ...

Guide to making control bar fade out when video is paused on video.js

Is there a way for the control bar to automatically disappear when the video is paused? Thank you in advance! ...

Assistance required in adjusting the clickable area of the link

While casually exploring a random website, I decided to experiment with some design elements. However, I encountered an issue where the link was only clickable on the left side and not the right. This is how it appears: There is a div containing a heading ...

Utilizing the W3Schools CodeColor library across various elements

Looking to provide code examples using a JS code highlighter with an ID? Check out this URL for a demonstration: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_syntax_highlight Want to use this for multiple examples but finding that the ID only ...

The periodLookup array does not have a defined value for periodStr. Why is this error being caught?

Here is a method that I am working with: set_period_help_text: function(periodInput){ var metric = MonitorMetric.getSelectedMetric(); var periodStr = $('select[name=metric_period]').val(); var datapoints = Number(periodIn ...