Creating a Form Right in the Middle

I'm new to web development and I'm trying to figure out how to center my form on the page using CSS and HTML. Can someone help me out?

<form action="login.php" method="post">
username: <input type="text" id="txtusername" /><br />
password: <input type="text" id="txtpassword" /><br />
<input type="submit" value="login" />
</form>

Answer №1

To center an item horizontally using CSS, add a div with a specific width and margin: 0 auto. Unfortunately, there isn't a quick fix for vertically centering a page.

 <div style="width: 500px; margin: 200px auto 0 auto;">
   <form action="login.php" method="post">
     Username: <input type="text" id="txtusername" /><br />
     Password: <input type="text" id="txtpassword" /><br />
     <input type="submit" value="login" />
   </form>
 </div>

One option is to use position:fixed, but I recommend adjusting the margin-top instead, as shown in the example above.

Check out this JSFiddle example for reference. Enjoy!

Just a tip: the values for the margin property are margin: [top] [right] [bottom] [left], which explains the 200px for top, 0px for bottom, and auto for left and right.

Answer №2

<table border="0" align="center" cellpadding="0" cellspacing="0" width="300">
<tr>
    <td>
        <form method="post" action="flogin.php">
            <table width="100%" cellpadding="7" cellspacing="0" border="0">
                <tr>
                    <td colspan="3"><center><strong>Enter Your Information </strong></center><br /></td><br />
                </tr>
                <tr>
                <td width="30%">First Name</td>
                <td width="10%">:</td>
                <td width="60%"><input type="text" name="first_name" /></td>
                </tr>
                <tr>
                <td width="30%">Last Name</td>
                <td width="10%">:</td>
                <td width="60%"><input type="text" name="last_name" /></td>
                </tr>
                <tr>
                <td width="30%">Email Address</td>
                <td width="10%">:</td>
                <td width="60%"><input type="text" name="email" /></td>
                </tr>
                <tr>
                <td colspan="3"><center><input type="submit" name="submit" /></center><br /></td>
                </tr>
            </table>
        </form>
    </td>
</tr>
</table>

Answer №3

To apply the following style, insert it within the <HEAD> tag:

<STYLE type="text/css">
form {
  margin: auto;
  width: 30em; /* experiment with other values too */
}
</STYLE>

By the way, are you working with HTML or XHTML? In HTML, empty tags like <BR> are not closed with <BR /> and input tags do not have a /> at the end. However, in XHTML, they are closed properly.

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

Center the table cell element horizontally

Struggling to align a table cell element both horizontally and vertically? Currently, only managing the vertical alignment. The images vary in size, calling for fluidity and compatibility with max-height and width... Custom Style Sheet (CSS) .prod-img { ...

Ways to customize decoration in Material UI TextField

Struggling to adjust the default 14px padding-left applied by startAdornment in order to make the adornment occupy half of the TextField. Having trouble styling the startAdornment overall. Attempts to style the div itself have been partially successful, b ...

PHP script not recognizing CSS styles

My coding dilemma involves a script that is supposed to display numbers from 1 to 100 in black font. For multiples of 3, it should show "Three" in green, and for multiples of 7, it should display "Seven" in blue instead of the number. If a number is a mult ...

How come the styles in my CSS file aren't being applied to my images based on their class or ID?

When I apply a className or id to my img tag in my JavaScript (using React.js) and then add a style that references that id or class, the style does not get applied. However, when I do the same for a div, everything works perfectly fine. <--JavaScript- ...

Is there a way to make the footer adapt to the varying heights of the grid sections as they grow in size?

Currently, I am facing an issue with the positioning of the page footer. Despite my efforts, it does not remain at the bottom as intended. Please refer to the image for a visual representation of the problem. How can this be rectified? It is worth noting ...

Make sure that the element with a relatively positioned stays contained within the top container

While relative and absolute positioning can be useful, they have the drawback of taking elements out of the flow which can lead to restrictions in their usage. Recently, I came across one such restriction and I'm interested to see if there is a soluti ...

Why isn't the z-index functioning properly in Vue.js styling?

I am currently developing a simple component that displays four steps, indicating the current step and allowing users to navigate through them. I want to create a design where these numbers are enclosed within circles that are evenly distributed in a line ...

Create a pagination system in CodeIgniter for displaying data

Having trouble implementing paging in the search functionality of my result table. I want to display just 10 rows but can't get it to work properly. I tried reading about Pagination on https://codeigniter.com/, but I'm still struggling to unders ...

Is there a way to insert a colored square into a <button> tag rather than text?

Looking for help with the following HTML: <button name="darkBlue" onclick="setThemeColor(this.name)">Blue</button> <button name="black" onclick="setThemeColor(this.name)">Black</button> I'm interested in replacing the text on ...

The width of mat-table columns remains static even with the presence of an Input field

I'm currently working on an Angular component that serves the dual purpose of displaying data and receiving data. To achieve this, I created a mat-table with input form fields and used {{element.value}} for regular data display. Each column in the tab ...

Guide to incorporating a fadeIn effect in Jquery triggered by a change event

Looking for some guidance on implementing a fade-in effect on a <p> tag with relevant CSS after making an AJAX call in response to a change event. Below is the current code snippet: $('#scenarioDropdownList').change(function() { var sc ...

What is the best way to resize an image within an SVG shape to completely fill the boundaries?

There is an SVG shape in the form of a parallelogram that has been filled with an image. To view the demo, click here. The code for the SVG object is as follows: <svg style="overflow:visible; margin-left:111px; margin-top:22px; " height="86" width=" ...

Is it advisable to avoid using `&apos;` for escaping single quotes?

According to the insights shared in conversations about the popularity of single quotes in HTML and Jquery embedded quote in attribute, the Wikipedia page on HTML highlights that: The use of the single-quote character ('), as a way to quote an attri ...

When the *ngFor directive disrupts the CSS Grid Layout, resulting in all items being displayed in a single column

I am a beginner in the world of programming and web development. Currently, I am working on building my own personal website. My goal is to arrange boxes in a grid with 4 columns, similar to the layout you can find at this link: Each box represents an ob ...

The background-size:cover property fails to function properly on iPhone models 4 and 5

I have taken on the task of educating my younger sister about programming, and we collaborated on creating this project together. Nicki Minaj Website However, we encountered an issue where the background image does not fully cover the screen when using b ...

The Three JS on the website is error-free, yet it fails to function properly

I've been attempting to replicate the example three.js page on my own website, complete with a canvas for the 3D animation. However, I'm encountering an issue where nothing is displayed, despite no errors appearing. I've tried troubleshootin ...

Making a form select element responsive within a bootstrap 4 card component

Check out the code snippet I have here: This is how I envisioned it to look, and it works perfectly at large resolutions. However, when I scale it down to tablet size, the resizing of the select element doesn't seem to work properly. Here's a s ...

Styling with CSS in Zend Framework

In the process of creating a website using Zend Framework, I have successfully implemented the header, footer, and all the necessary pages. However, I am facing an issue with integrating the CSS file located at public/css/styles.css. When running the webs ...

What is the best way to implement a side navigation bar with 100 tabs using Angular 4?

I am new to learning AngularJS and could use some guidance. Currently, I am working on an Angular 4 SPA project that consists of a sidebar with 100 tabs, each containing different content. Here is an image of the sidebar. I have tried a simple approach by ...

What is the best way to parse a JSON file and create a dynamic webpage using jQuery with the data from the

As someone who is new to working with Node.js and JSON, I am encountering some issues when trying to extract data from a JSON file. Below is the code that I have written: 'use strict'; const fs = require('fs'); let questsRawData = fs ...