Align controls properly

狂风中的少年 提交于 2019-12-07 07:46:02

问题


I want to adjust the appearance on same controls on the website which I am working on, but it seems that is not going good. I want to use CSS to properly align the controls. I want to have the checkbox and the label aligned left and then little bit room, then textbox is coming. Also I want all the textboxes to be aligned same vertically. How can I do that with css without using tables.

Thanks in advance for your help, Laziale


回答1:


CSS

div{margin-bottom:2px;}
input[type="checkbox"]{display:block; float:left; margin-right:2px;}
label{display:block; float:left; width:150px;}

HTML

<div><input type="checkbox" /><label>Address</label><input type="text" /></div>
<div><input type="checkbox" /><label>State</label><input type="text" /></div>
<div><input type="checkbox" /><label>City</label><input type="text" /></div>
<div><input type="checkbox" /><label>ZIP</label><input type="text" /></div>
<div><input type="checkbox" /><label>Contact Person</label><input type="text" /></div>
<div><input type="checkbox" /><label>Contact Person</label><input type="text" /></div>



回答2:


Seriously, for you need, you should use tables. you'll have a lot more html and CSS trying the achieve this anyway.

The whole drive to not use tables in Html is for layout of pages and such where you may want to rearrange your page layout (move things around) using just CSS. but I doubt you'd want to rearrange the way your form is laid out.




回答3:


Simplest way: Specify a width and float:left for the input containers (labels) http://jsfiddle.net/tCcff/2/

<style type="text/css">
label {
    float: left;    
    width: 8em;    
}

label.text {
  width: 7em;
}
</style>
<div>   
    <label> <input type='checkbox' /> Short </label> <label class='text'>Field Label</label><input type='text' />
</div>
<div>   
    <label> <input type='checkbox' /> Long Label here </label> <label class='text'>Text2</label><input type='text' />
</div>
<div>   
  <label> <input type='checkbox' /> Label here </label> <label  class='text'>Text Three-----</label> <input type='text' />
</div>

This article is a good reference for CSS based form layout: http://articles.sitepoint.com/article/fancy-form-design-css/3



来源:https://stackoverflow.com/questions/4800303/align-controls-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!