2012-05-29 23 views
1

我需要使用CSS來對齊表單的標籤和輸入。結果應該是這樣的(我希望這個簡單的方案是明確的):使用CSS在窗體上定位元素

Label1: ______ 
Labellll2: ______ 
Button 

所以,我的HTML和CSS的外觀如下。問題在於標籤放置在輸入頂部,並且按鈕位於右側而不是底部。

<form width="200px" name="optform" method="post" action="#"> 
     <div class = "boxx"> 
      <label for="param1">Param1:</label> 
      <input type="text" class="input-text" value="5" size="11" maxlength="11" name="param1" id="param1"> 
      <label for="param2">Param1:</label> 
      <input type="text" class="input-text" value="5" size="11" maxlength="11" name="param2" id="param2"> 
     </div> 
     <div class="buttons"> 
      <a href="#"> 
        <img src="images/opt.png" alt=""/> Run 
      </a> 
     </div> 

div.boxx { 
    width: 500px; 
} 
div.boxx .input-text{ 
    border:1px solid #A9C7F5; 
    color: #00557F; 
    font: 12px Arial; 
    float:left; 
    width:66%; 
    margin:0 0 0.5em 0.25em; 
} 

div.boxx label{ 
    display:block; 
font: 13px Arial; 
color:#00557F; 
width:33%; 
float:left; 
text-align:left; 
padding-right: 8px; 
} 
.buttons a, .buttons button { 
    -moz-border-bottom-colors: none; 
    -moz-border-image: none; 
    -moz-border-left-colors: none; 
    -moz-border-right-colors: none; 
    -moz-border-top-colors: none; 
    background-color: #DFF4FF; 
    border-color: #EEEEEE #DEDEDE #DEDEDE #EEEEEE; 
    border-style: solid; 
    border-width: 1px; 
    color: #565656; 
    cursor: pointer; 
    font-family: Verdana,arial,sans-serif; 
    font-size: 11px; 
    font-weight: bold; 
    line-height: 130%; 
    margin: 10px 10px 0 0; 
    padding: 4px 10px 3px 7px; 
    text-decoration: none; 
} 
.buttons button { 
    overflow: visible; 
    padding: 4px 10px 3px 7px; 
    width: auto; 
} 

回答

4

標籤必須

display:inline-block; 

和寬度過大或者您的標籤或輸入框的。填充和邊距會導致寬度百分比問題,因爲它們沒有被考慮在內。

這裏是更新的CSS

div.boxx { 
width: 500px; 
} 
div.boxx .input-text{ 
border:1px solid #A9C7F5; 
color: #00557F; 
font: 12px Arial;  
width:60%; 
margin:0 0 0.5em 0.25em; 
} 

div.boxx label{ 
display:block; 
font: 13px Arial; 
color:#00557F; 
width:33%; 
float:left; 
text-align:left; 
padding-right: 8px; 
} 
.buttons a, .buttons button { 
-moz-border-bottom-colors: none; 
-moz-border-image: none; 
-moz-border-left-colors: none; 
-moz-border-right-colors: none; 
-moz-border-top-colors: none; 
background-color: #DFF4FF; 
border-color: #EEEEEE #DEDEDE #DEDEDE #EEEEEE; 
border-style: solid; 
border-width: 1px; 
color: #565656; 
cursor: pointer; 
font-family: Verdana,arial,sans-serif; 
font-size: 11px; 
font-weight: bold; 
line-height: 130%; 
margin: 10px 10px 0 0; 
padding: 4px 10px 3px 7px; 
text-decoration: none; 
} 
.buttons button { 
overflow: visible; 
padding: 4px 10px 3px 7px; 
width: auto; 
} 
+0

標籤仍然是在文本框的頂部,而不是在左側 – Gusgus

+0

我錯過了項目太寬。嘗試使用更新的css – Slick86

+0

您的uodated post解決了我的問題。 – Gusgus

1

我知道現代CSS人討厭的表,但在這種情況下,(並與衆多的div對齊問題)我一個電子書籍可愛的小桌子。

<table> 
    <tr> 
    <td>Label1: </td><td><input type="text" /></td> 
    </tr><tr> 
    <td>Label2: </td><td><input type="text" /></td> 
    </tr><tr> 
    <td>Label3: </td><td><input type="text" /></td> 
    </tr> 
</table> 
<input type="button" /> 
0

你也可以使用div和span。事情是這樣的:

<form action=""> 
    <div class="row"><span class="label">label1:</span><span class="formw"><input type="text" size="25"></span></div> 
    <div class="row"><span class="label">label2:</span><span class="formw"><input type="text" size="25"></span></div> 
    <div class="spacer">&nbsp;</div> 
</form> 

和css:

div.row { 
    clear: both; 
    padding-top: 10px; 
    } 

div.row span.label { 
    float: left; 
    width: 33%; 
    text-align: left; 
    } 

div.row span.formw input{ 
width: 100%; 
} 

div.row span.formw { 
    float: right; 
    width: 66%; 
    text-align: left; 
    }