如何使用div在html表单中并排获取两个字段

时间:2013-04-03 04:49:09

标签: html css

我尝试使用divs并排创建带有字段的表单。但是它们在行中一个接一个地显示。在这里我得到批量代码和课程代码两行。如何将它们并排放在一行中。

<!DOCTYPE html>
<html>  
    <head>

        <link href="css/reset.css" media="screen" rel="stylesheet"/>
        <link href="css/redmond/jquery-ui-1.10.2.custom.min.css" media="screen" rel="stylesheet"/>
        <link href="css/default.css" media="screen" rel="stylesheet"/>
        <link href="sidebarmenu.css" rel="stylesheet" type="text/css">
                <script src="sidebarmenu.js"></script>
                <script src="scripts/jquery-1.9.1.min.js"></script> 
        <script src="scripts/jquery-ui-1.10.2.custom.min.js"></script> 

</head>
    <body>  

        <!--<form id="frmExamRegistration" method="post" enctype="multipart/form-data" class="anu">-->


    <form name="f1" method="post" class="anu">  
    <%@include file="header.html"%>

<div  style="float:right;width:10%;border:0;font-color:#163362"><a href=logout.jsp">Log Out</a></div>
<%@include file="sidemenus.jsp" %>

    <div id="container">
        <div class="content"  ><center>Delete Batch</center><br>
            <div class="formElements">
                <label>Batch code:</label>
                 <span><input type="text" id="txtBatchcode" name="txtBatchcode" ></span>

            <div class="formElements" style="z-index:-1">
                <label>Course Code:</label>
                 <span><input type="text" id="txtCoursecode" name="txtCoursecode" ></span>

            <div class="buttons" align="center">
                <button type="submit" class="primaryAction">Delete</button>
                                <button type="reset" class="primaryAction">Clear</button>

            </div></div></div>
                 </div>
         </div>
       </form>
  </body>
</html>

4 个答案:

答案 0 :(得分:0)

您可以在每个display:inline-block;块上使用.formElements

见这里:http://jsfiddle.net/hAwyy/

<div id="container">
    <div class="content">
        <h2 style="text-align:center;">Delete Batch</h2>
        <div class="formElements" style="display:inline-block;">
            <label>Batch code:</label>
            <span><input type="text" id="txtBatchcode" name="txtBatchcode" /></span>
        </div>
        <div class="formElements" style="display:inline-block;">
            <label>Course code:</label>
            <span><input type="text" id="txtCoursecode" name="txtCoursecode" /></span>
        </div>
        <div class="buttons" style="text-align:center;">
            <button type="submit" class="primaryAction">Delete</button>
            <button type="reset" class="primaryAction">Clear</button>
        </div>
    </div>
</div>

答案 1 :(得分:0)

试试这个:在class中定义css,它将适用于所有.formElements

<style type="text/css">
.formElements{
    display:inline;
}
</style>

答案 2 :(得分:0)

您可以使用表格执行此操作:

 <form name="f1" method="post" class="anu">
    <div style="float:right;width:10%;border:0;font-color:#163362">
        <a href=logout.jsp ">Log Out</a></div>


    <div id="container ">
        <div class="content "  ><center>Delete Batch</center><br>

    <table>
        <tr>
            <td>
                <label>Batch code:</label> <span><input type="text " id="txtBatchcode " name="txtBatchcode " ></span>

            </td>
            <td>
                <label>Course Code:</label> <span><input type="text " id="txtCoursecode " name="txtCoursecode " ></span>

            </td>
        </tr>
        </table>
        </div>
        </div>

</form>

<强> DEMO

使用表格是最简单的方法。您根本不必使用 CSS

答案 3 :(得分:0)

看到它的非常简单,让你的div向左浮动..所以它们将从左边开始并以最大宽度结束然后,它们将在下一行发生。

<div width="100%">
<div style="float:left , width :50%">your content here    </div>
<div style="float:left , width :50%">  your content here </div>
<div></div>,
</div>