2011-04-16 91 views
2

我是初学CSS的人,我想知道使用CSS只在表单中定位元素的最佳技巧。我不能让我的两个按钮,彼此旁边大概对齐......使用CSS表单定位元素

这里是我的HTML代码:

<form action="creat_fam.php" method ="post" > 
       <div id="formWrapper"> 
        <label for="fam_name">Family Name : </label> 
        <input type="text" placeholder="Family Name" name="fam_name" required> 
        <br/> 

        <label for="people_count">People count : </label> 
        <input type="number" name="people_count" min="1" value="1" required> 
        <br/> 

        <label for="location">Location : </label> 
        <input type="text" name="location" placeholder="location" required> 
        <br/> 

        <input type="reset" value="Reset"> 
        <input type="submit" name="submit" value="Done"> 
       </div> 
      </form> 

我的CSS代码:

#formWrapper { 
width : 400px; 
padding : 15px; 
-webkit-box-sizing : border-box; 
} 

#formWrapper label { 
float:left; 
margin-bottom : 15px; 
line-height: 25px; 
} 

#formWrapper input[type="text"] , #formWrapper input[type="number"] { 
float :right; 
margin-bottom : 10px; 
padding : 2px; 
width : 250px; 
height: 25px; 
border:1px solid gray; 
} 

#formWrapper input[name="submit"] , #formWrapper input[type="reset"] { 
margin : 0; 
width : 90px; 
height: 40px; 
border : none; 
border-radius : 4px; 
background : -webkit-gradient(linear , 0 0, 0 100% , from(#7f7f7f) ,to(#535353)); 
color : lightgray; 
font-weight: bold; 
cursor : pointer; 
} 

和所有我得到是这样的:提前

enter image description here

感谢。

回答

1

虽然inputs是内联元素,但由于输入为location,所以第二个按钮将转到下一行。 location输入占用第一个按钮旁边的一些不可见空间(margin-bottom),导致第二个按钮打包。

1

float:left;添加到#formWrapper input[name="submit"] , #formWrapper input[type="reset"] { }将水平对齐框,我会把按钮放在他们自己的div中,并与边缘玩耍,以便他们对齐。

0

我意识到这是一个较老的问题,但它在搜索结果中很突出,所以希望这可以帮助其他人。

使用开发人员工具,可以看到容器将显示比集合字段的总高度小得多。为了避免调整页边距以适应页面的需要,可以按照Jack的建议将按钮包装在自己的div中(例如#buttonsDiv),而是将clear:的CSS应用于div。

对按钮执行的任何格式设置都将独立于上述内容应用,从而使按钮可以毫不费力地彼此相邻放置。