I am trying to display two form buttons (each belonging to a separate form) inline and centered (Horizontally).
Here's my code:
<div>
<?php
if(($id && $upperConvLim) || ($id && !$upperConvLim && !$lowerConvLim)){
?>
<span> </span>
<form method="post" action="index.php" class="previous">
<input type="submit" value="Previous" name="submit1" />
<?php echo '<input type = "hidden" value="'.$id.'"name = "id" />'; ?>
<?php echo '<input type = "hidden" value="'.$previous.'"name = "previous" />'; ? >
</form>
<?php
}
if(($id && $lowerConvLim) || ($id && !$upperConvLim && !$lowerConvLim)){
?>
<span> </span>
<form method="post" action="index.php" class="next">
<input type="submit" value="Next" name="submit2" />
<?php echo '<input type = "hidden" value="'.$id.'"name = "id" />'; ?>
<?php echo '<input type = "hidden" value="'.$next.'"name = "next" />'; ?>
</form>
<?php
}
?>
</div>
I tried to inline them using the following in my CSS,
form, form div {
display: inline-block;
}
This works fine but I'm unable to center the two buttons. I would like to know both 1) How to center the two buttons as well as 2) If there is a better way to inline them. Appreciate it.
EDIT: Following Cynthia's suggestion, I changed my php/html code to,
<div class="center-form">
<?php
if(($id && $upperConvLim) || ($id && !$upperConvLim && !$lowerConvLim)){
?>
<span> </span>
<form method="post" action="index.php" class="hform">
<input type="submit" value="Previous" name="submit1" />
<?php echo '<input type = "hidden" value="'.$id.'"name = "id" />'; ?>
<?php echo '<input type = "hidden" value="'.$previous.'"name = "previous" />'; ?>
</form>
<?php
}
if(($id && $lowerConvLim) || ($id && !$upperConvLim && !$lowerConvLim)){
?>
<span> </span>
<form method="post" action="index.php" class="hform">
<input type="submit" value="Next" name="submit2" />
<?php echo '<input type = "hidden" value="'.$id.'"name = "id" />'; ?>
<?php echo '<input type = "hidden" value="'.$next.'"name = "next" />'; ?>
</form>
<?php
}
?>
</div>
And my new CSS:
table {
font-family: Verdana;
font-size: 10pt
}
#table4 {
margin-left: auto;
margin-right: auto;
border:1px solid black;
width :70%;
padding:10px;
background-color:#FAFAD2;
}
#table3 {
margin-left: auto;
margin-right: auto;
border:1px solid black;
width :60%;
padding:10px;
background-color:#FFDAB9;
}
/*
form, form div {
display: inline-block;
text-align: center;
}
*/
.center-form { display:block ; width:165px ; margin:auto }
form.hform { display:inline }
input[type=submit] { padding:4px 0 ; width:80px }
Thanks!
This will centre all all elements within a div or form.
form, form div {
text-align: center;
}
Here you go: http://jsfiddle.net/myQtb/
In a nutshell:
Here is the abbreviated HTML code (I just used barebones forms):
<div class="center-form">
<form class="hform">
<input type="submit" name="submit" value="Submit">
</form>
<form class="hform">
<input type="submit" name="submit" value="Reset">
</form>
</div>
and here is the CSS:
.center-form { display:block ; width:165px ; margin:auto }
form.hform { display:inline }
input[type=submit] { padding:4px 0 ; width:80px }
Why it works:
Change your form tag styling from inline-block
to simply inline
. Then enclose BOTH forms in the .center-form
DIV that is as wide as the combined width of both buttons. Assign display:block
to the .center-form
DIV and add margin:auto
(so that the left / right margins will auto adjust).
And voila!