简体   繁体   English

用CSS样式化表格

[英]Styling Forms with CSS

I am trying to style a form on a website I am designing with CSS. 我正在尝试使用CSS设计的网站上的表单样式。 I have tried to adjust the width of the labels and it doesn't change it. 我试图调整标签的宽度,但它没有改变。 Here is my code:  这是我的代码:

<h3>Contact Form</h3>
<div>
    <label for="form1_name">Name</label>
    <input id="form1_name" name="name" type="text" required="required" />

</div>

<div>
    <label for="form1_email">Email</label>
    <input id="form1_email" name="email" type="email" placeholder="you@company.com" required="required" />


</div>

<div>
    <label for="form1_email">Telephone</label>
    <input id="form1_telephone" name="telephone" type="text" />
    </div>


<div>
    <label for="form1_message">Comment</label>
    <textarea id="form1_message" name="message" cols="30" rows="4" required="required"></textarea>

</div>

<div>
    <input id="form1_submit" name="submit" value="Send" type="submit" /><input type="hidden" name="cms-form" value="Y29udGFjdDpwZXJjaF9mb3JtczovdGVtcGxhdGVzL2NvbnRlbnQvY29udGFjdC5odG1s" />
</div>


</form></div>

Here is my css: #contact label { width:200px !important; 这是我的CSS:#contact标签{width:200px!important; } }

Yet this doesn't change the width of my labels. 但这不会改变标签的宽度。 What am I doing wrong? 我究竟做错了什么? Here is a link to the page: http://ridgesideredangus.com/about.php 这是页面的链接: http : //ridgesideredangus.com/about.php

#contact label {display: inline-block; width: 200px;}

Labels are inline elements by default. 标签默认是内联元素。 You cannot add dimensions unless you set them to block or inline-block. 除非将尺寸设置为封闭或内嵌,否则无法添加尺寸。

As an alternate, ditch the divs altogether. 作为替代,完全divs Make your labels blocks and nest the form elements in them. 使标签块并在其中嵌套表单元素。 Set the labels to position relative so you can position the form elements in relation to them. 将标签设置为相对位置,以便可以相对于它们放置表单元素。

HTML HTML

<label >Name<input id="form1_name" name="name" type="text" required="required" /></label>

<label >Email<input id="form1_email" name="email" type="email" placeholder="you@company.com" required="required" /></label>

<label >Telephone<input id="form1_telephone" name="telephone" type="text" /></label>

CSS CSS

label {display:block;position:relative;padding:5px;}
label input {position:absolute; left:100px;}

Example

HOWEVER some argue it is preferable to explicitly relate the label to the input 但是 有人认为最好将labelinput明确相关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM