How do I center forms inside a table? - html

The below code works for what I need (three horizontal buttons), except it is not centered. It sits on the left side of the screen. I did some research and couldn't fix it, and also tried a tableless design using "float" and "display:inline" and just can't get it centered. Help please!
<table>
<td>
<tr>
<form method="get" action="index.php"><button type="submit">Home</button></form>
<form method="get" action="signup"><button type="submit">Create Account</button></form>
<form method="get" action="login"><button type="submit">Login to Account</button></form>
</tr>
</td>
</table>

As other people mentioned <td> should be inside <tr>. By default <form> is displayed as block element. Change it to inline-block.
table {
border: 1px solid red;
width: 100%;
}
td {
border: 1px solid green;
text-align: center;
}
form {
display: inline-block;
}
<table>
<tr>
<td>
<form method="get" action="index.php">
<button type="submit">Home</button>
</form>
<form method="get" action="signup">
<button type="submit">Create Account</button>
</form>
<form method="get" action="login">
<button type="submit">Login to Account</button>
</form>
</td>
</tr>
</table>

The <tr> (rows) should be on the outside
<table>
<tr>
<td>
<form method="get" action="index.php"><button type="submit">Home</button></form>
</td>
<td>
<form method="get" action="signup"><button type="submit">Create Account</button></form>
</td>
<td>
<form method="get" action="login"><button type="submit">Login to Account</button></form>
</td>
</tr>
</table>
jsFiddle Demo

Inverse the tr and td in the html.
<table>
<tr>
<td>
<form method="get" action="index.php"><button type="submit">Home</button></form>
</td>
<td>
<form method="get" action="signup"><button type="submit">Create Account</button></form>
</td>
<td>
<form method="get" action="login"><button type="submit">Login to Account</button></form>
</td>
</tr>
</table>
And try adding this to css
td {
text-align: center;
}
And don't forget to set the width of your table ! (100% for all the window)

Related

How do I bring the entry box closer to the names in html?

I am trying to bring the entry fields closer to the names; it looks bit far. wanted to see if anybody can suggest. Html and css are shown below. This is how it looks now:
<div id="container">
<h3>Add Student</h3>
<form action="StudentControllerServlet" method="GET">
<input type="hidden" name="command" value="ADD" />
<table>
<tbody>
<tr>
<td><label>First name:</label></td>
<td> <input type="text" name="firstName"/></td>
</tr>
<tr>
<td><label>Last name:</label></td>
<td><input type="text" name="lastName"/></td>
</tr>
<tr>
<td><label>Email:</label></td>
<td> <input type="text" name="email" /></td>
</tr>
</tbody>
</table>
</form>
</div>
/* To mimic your screenshot */
#container table {
width: 50%;
outline: red 1px dotted;
}
/* To align your text closer */
#container table td:first-child {
outline: green 1px dashed;
text-align: right;
}
<div id="container">
<h3>Add Student</h3>
<form action="StudentControllerServlet" method="GET">
<input type="hidden" name="command" value="ADD" />
<table>
<tbody>
<tr>
<td><label>First name:</label></td>
<td> <input type="text" name="firstName"/></td>
</tr>
<tr>
<td><label>Last name:</label></td>
<td><input type="text" name="lastName"/></td>
</tr>
<tr>
<td><label>Email:</label></td>
<td> <input type="text" name="email" /></td>
</tr>
</tbody>
</table>
</form>
</div>

how to insert gap between rows of a table?

i want to add gap betweenw my input boxes in input form.
They are heavily packed together and not looking good.
so that the input form looks and feels good...form is a object passed from views.py....
createcustomer.html
{% extends 'customer/base.html' %}
{% block title %}Create Customer{% endblock %}
{% block body %}
<div class="container-fluid" style="background-color: #f2f2f2; width:100%; height:135%;" >
<div class="row ">
<div class="col-lg-4"></div>
<div class="col-lg-8 text-left" align="center">
<h2>Create a Customer</h2>
<form action="." method="post">
<div class="form-group" >
{% csrf_token %}
<table>
<tr style="width:100%;" class="highlight">
<td>First Name:</td>
<td>{{form.fname}}</td>
</tr>
<tr>
<td>Last Name:</td>
<td>{{form.lname}}</td>
</tr>
<tr>
<td>Email:</td>
<td>{{form.email}}</td>
</tr>
<tr>
<td>Address:</td>
<td>{{form.address}}</td>
</tr>
<tr>
<td>City:</td>
<td>{{form.city}}</td>
</tr>
<tr>
<td>State:</td>
<td>{{form.state}}</td>
</tr>
<tr>
<td>Zip:</td>
<td>{{form.zip}}</td>
</tr>
<tr>
<td>Username:</td>
<td>{{form.uname}}</td>
</tr>
<tr>
<td>Phone:</td>
<td>{{form.phone}}</td>
</tr>
</table>
<div style="margin-left:200px; margin-top:20px;margin-bottom:20px">
<input type="submit" class="btn btn-success" value="Submit">
</div></div>
</form>
</div>
</div>
</div>
{%endblock%}
you can try with css:
table tr {
margin-bottom: 10px;
display: table;
}
add table to cellspacing & cellpadding
<table cellspacing="1" cellpadding="1">
You can apply padding to the input itself by applying a class and css:
HTML
<input id="padded" type="submit" class="btn btn-success" value="Submit">
CSS
#padded {
padding: 10px;
}
This will create space around the input form, giving it room to breath.

Center form in Chrome and Firefox

I have a simple login form and it is centered in internet explorer but in Chrome and Firefox it is aligned to the left of the page. What do I need to do to have the form centered in the other 2 browsers.
<form name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="4"><div align="center"><strong>Client Login</strong></div></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input name="myusername" type="text" id="myusername" placeholder="Company Name" size="24"></td>
<td> </td>
</tr>
<tr>
<td></td>
<td></td>
<td><input name="mypassword" type="password" id="mypassword" placeholder="password" size="25"></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input name="Submit" type="submit" class="submit_button" value="Login"></td>
<td> </td>
</tr>
</table>
</td>
</form>
Try to change the form width to 100%. This the example
<form name="form1" method="POST" action="<?php echo $loginFormAction; ?>" style="width:100%;">
You should avoid using HTML tables to define the layout of your page. Here's a good overview on why tables should not be used for layout.
You should rely on HTML tag for semantic and CSS to tell the browser how he should display the stuff in the page.
Now consider the following:
<style>
form {
width: 200px;
margin: 0 auto;
}
input {
display: block;
margin: 5px 0;
}
</style>
<form name="form1" method="POST" action="/">
<h1>Client Login</h1>
<input name="myusername" type="text" id="myusername" placeholder="Company Name" size="24">
<input name="mypassword" type="password" id="mypassword" placeholder="password" size="25">
<input name="Submit" type="submit" class="submit_button" value="Login">
</form>
The HTML is stripped down to the bare essential: your title then your form elements. The form here is the main container. With CSS we tell the form to have 200px width and we use the margin property to center it.
We also tell the input element to display like a block element to fill it'S container, this way they each occupy 1 line.
Learning CSS is useful to separate appearance from meaning, here's a great demonstration of the relation between content, container and design.
Would you mind to try this maybe :)
<style>
table {
text-align:center;
margin-left:auto;
margin-right:auto;
}
</style>
<form name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="4"><div align="center"><strong>Client Login</strong></div></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input name="myusername" type="text" id="myusername" placeholder="Company Name" size="24"></td>
<td> </td>
</tr>
<tr>
<td></td>
<td></td>
<td><input name="mypassword" type="password" id="mypassword" placeholder="password" size="25"></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input name="Submit" type="submit" class="submit_button" value="Login"></td>
<td> </td>
</tr>
</table>
</td>
</form>

Unable to put table next to each other

I have read through other posts but it seems that using display:inline-block and float:left doesn't fix my problem. Here's what I have:
.leftTable {
display: inline-block;
overflow: auto;
border: solid 1px black;
width: 20%;
}
.rightTable {
display: inline-block;
overflow: auto;
border: solid 1px black;
width: 80%;
}
<table class="leftTable">
<tr>
<td>
<form action="TestMartController" method="post">
<input type="hidden" name="action" value="dairy">
<input type="image" src="<%=request.getContextPath()%>/css/categories/dairy.jpg">
</form>
</td>
</tr>
<tr>
<td>
<form action="TestMartController" method="post">
<input type="hidden" name="action" value="meat">
<input type="image" src="<%=request.getContextPath()%>/css/categories/meats.jpg">
</form>
</td>
</tr>
<tr>
<td>
<form action="TestMartController" method="post">
<input type="hidden" name="action" value="bakery">
<input type="image" src="<%=request.getContextPath()%>/css/categories/bakery.jpg">
</form>
</td>
</tr>
<tr>
<td>
<form action="TestMartController" method="post">
<input type="hidden" name="action" value="fruitveg">
<input type="image" src="<%=request.getContextPath()%>/css/categories/fruit & veg.jpg">
</form>
</td>
</tr>
</table>
<table class="rightTable">
<tr>
<th>img</th>
<th>product name</th>
<th>price</th>
<th>button</th>
</tr>
<tr>
<td></td>
</tr>
</table>
The result I got was
[leftTable]
[IMG]
[IMG]
[IMG]
[IMG]
[rightTable]
[IMG] [Product Name] [Price] [Button]
I want the desired result of
[leftTable] [rightTable]
[IMG] [IMG] [Product Name] [Price] [Button]
[IMG]
[IMG]
[IMG]
I can't seems to spot the mistake, does anyone have suggestion on how to fix this issue?
A simple approach might be to use display: inline-table on your two tables, since you want them to behave like inline elements.
Floats could also work but it depends on other factors in your layout.
.leftTable {
border: 1px dotted blue;
display: inline-table;
vertical-align: top;
}
.rightTable {
border: 1px dotted blue;
display: inline-table;
vertical-align: top;
}
<table class="leftTable">
<tr>
<td>
<form action="TestMartController" method="post">
<input type="hidden" name="action" value="dairy">
<input type="image" src="http://placehold.it/101x50">
</form>
</td>
</tr>
<tr>
<td>
<form action="TestMartController" method="post">
<input type="hidden" name="action" value="meat">
<input type="image" src="http://placehold.it/102x50">
</form>
</td>
</tr>
<tr>
<td>
<form action="TestMartController" method="post">
<input type="hidden" name="action" value="bakery">
<input type="image" src="http://placehold.it/103x50">
</form>
</td>
</tr>
<tr>
<td>
<form action="TestMartController" method="post">
<input type="hidden" name="action" value="fruitveg">
<input type="image" src="http://placehold.it/104x50">
</form>
</td>
</tr>
</table>
<table class="rightTable">
<tr>
<th>img</th>
<th>product name</th>
<th>price</th>
<th>button</th>
</tr>
<tr>
<td></td>
</tr>
</table>
Put the tables inside a container and position the containers next to each other using the float attribute in CSS.
HTML
<div class="table1_con">
<table></table>
</div>
<div class="table2_con">
<table></table>
</div>
CSS
Body, html {width:100%;}
.table1_con {width:20%;
position:absolute;
float:left;
.table2_con {
width:80%;
position:absolute;
float:left;
}
.table1_con .leftTable{
display: inline-block;
overflow: auto;
border: solid 1px black;
width: 100%;
}
.table2_con .rightTable{
display: inline-block;
overflow: auto;
border: solid 1px black;
width: 100%;
}

How to align form at the center of the page in html/css

I am new to html/css. i am trying to build up my own form and i am trying to align it in the center. I used the align property in css but its not working.
Html code:
<!DOCTYPE HTML>
<head>
<title>Web Page Having A Form</title>
<link rel = "stylesheet" type="text/css" href="Form.css">
</head>
<body>
<h1>Create a New Account</h1>
<form >
<table>
<tr>
<td>Name :</td> <td><input type="text" name="name"></td><br>
</tr>
<tr>
<td>Email :</td> <td><input type="text" name="email"></td><br>
</tr>
<tr>
<td>Password :</td> <td><input type="password" name="pwd"></td><br>
</tr>
<tr>
<td>Confirm Password :</td> <td><input type="password" name="cpwd"><br>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
Css Code:
body
{
background-color : #484848;
}
h1
{
color : #000000;
text-align : center;
font-family: "SIMPSON";
}
form
{
align:"center";
}
How should i change my code to align the entire form in the center?
Like this
demo
css
body {
background-color : #484848;
margin: 0;
padding: 0;
}
h1 {
color : #000000;
text-align : center;
font-family: "SIMPSON";
}
form {
width: 300px;
margin: 0 auto;
}
This is my answer. I'm not a Pro. I hope this answer may help you :3
<div align="center">
<form>
.
.
Your elements
.
.
</form>
</div>
Wrap the <form> element inside a div container and apply css to the div instead which makes things easier.
#aDiv{width: 300px; height: 300px; margin: 0 auto;}
<html>
<head></head>
<body>
<div>
<form>
<div id="aDiv">
<table>
<tr>
<td>Name :</td>
<td>
<input type="text" name="name">
</td>
<br>
</tr>
<tr>
<td>Email :</td>
<td>
<input type="text" name="email">
</td>
<br>
</tr>
<tr>
<td>Password :</td>
<td>
<input type="password" name="pwd">
</td>
<br>
</tr>
<tr>
<td>Confirm Password :</td>
<td>
<input type="password" name="cpwd">
<br>
</tr>
<tr>
<td>
<input type="submit" value="Submit">
</td>
</tr>
</table>
</div>
</form>
</div>
</body>
</html>
I would just use table and not the form. Its done by using margin.
table {
margin: 0 auto;
}
also try using something like
table td {
padding-bottom: 5px;
}
instead of <br />
and also your input should end with />
e.g:
<input type="password" name="cpwd" />
Or you can just use the <center></center> tags.
Wrap the element inside a div container as a row like
your form here or something like that.
Set css attribute:
width: 30%; (or anything you want)
margin: auto;
Please take a look on following picture for more detail.