尝试将无线电输入(+标签)添加到示例表单

时间:2008-10-24 08:55:21

标签: html css xhtml

<style type="text/css">
body {
    font-family:Helvetica, sans-serif; 
    font-size:12px;
}
p,
h1,
form,
button {
  border: 0;
  margin: 0;
  padding: 0;
}
.spacer {
  clear: both;
  height: 1px;
}
/* ----------- My Form ----------- */
.myform {
    margin: 0 auto;
    width: 400px;
    padding: 14px;
}
    /* ----------- basic ----------- */
    #basic {
        border: solid 2px #DEDEDE;
    }
    #basic h1 {
        font-size: 14px;
        font-weight: bold;
        margin-bottom: 8px;
    }
    #basic p {
        font-size: 11px;
        color: #666666;
        margin-bottom: 20px;
        border-bottom: solid 1px #dedede;
        padding-bottom: 10px;
    }
    #basic label {
        display: block;
        font-weight: bold;
        text-align: right;
        width: 140px;
        float: left;
    }
    #basic .small{
        color:#666666;
        display:block;
        font-size:11px;
        font-weight:normal;
        text-align:right;
        width:140px;
    }
    #basic input{
        float:left;
        width:200px;
        margin:2px 0 30px 10px;
    }
    #basic button{ 
        clear:both;
        margin-left:150px;
        background:#888888;
        color:#FFFFFF;
        border:solid 1px #666666;
        font-size:11px;
        font-weight:bold;
        padding:4px 6px;
    }
</style>

<div id="basic" class="myform">
  <form id="form1" name="form1" method="post" action="">
    <h1>Sign-up form</h1>
    <p>This is the basic look of my form without table</p>
    <label>Name
        <span class="small">Add your name</span>
    </label>
    <input type="text" name="textfield" id="textfield" />

    <label>Email
    <span class="small">Add a valid address</span>
    </label>
    <input type="text" name="textfield" id="textfield" />

    <label>Email
    <span class="small">Add a valid address</span>
    </label>
    <!-- Problem --->
    <input type="radio" name="something" id="r1" class="radio" value="1" /><label for="r1">One</label>
    <input type="radio" name="something" id="r2" class="radio" value="2" /><label for="r2">Two</label>
    <!-- Problem --->
    <button  type="submit">Sign-up</button>
    <div class="spacer"></div>


  </form>
</div>

我得到了这个示例表单,但是我不能添加单选按钮而不会搞砸它们。

6 个答案:

答案 0 :(得分:2)

你的表格输入都是相同宽度(200px)并向左浮动,这也适用于单选按钮。

这样的事情会让你朝着正确的方向前进

<style type="text/css">
body{
    font-family:Helvetica, sans-serif; 
    font-size:12px;
}
p, h1, form, button{border:0; margin:0; padding:0;}
.spacer{clear:both; height:1px;}
/* ----------- My Form ----------- */
.myform{
    margin:0 auto;
    width:400px;
    padding:14px;
}
    /* ----------- basic ----------- */
    #basic{
        border:solid 2px #DEDEDE;
    }
    #basic h1 {
        font-size:14px;
        font-weight:bold;
        margin-bottom:8px;
    }
    #basic p{
        font-size:11px;
        color:#666666;
        margin-bottom:20px;
        border-bottom:solid 1px #dedede;
        padding-bottom:10px;
    }
    #basic label{
        font-weight:bold;
        text-align:right;
        width:140px;
        float:left;
    }
    #basic .small{
        color:#666666;
        display:block;
        font-size:11px;
        font-weight:normal;
        text-align:right;
        width:140px;
    }
    #basic input{
        float:left;
        width:200px;
        margin:2px 0 30px 10px;
    }
    #basic button{ 
        clear:both;
        margin-left:150px;
        background:#888888;
        color:#FFFFFF;
        border:solid 1px #666666;
        font-size:11px;
        font-weight:bold;
        padding:4px 6px;
    }
    #basic input.radio{
        width:50px;
        margin:2px 0 30px 10px;
    }
    #basic label.radio {
    width:40px;
    text-align:left;
    }
</style>

<div id="basic" class="myform">
  <form id="form1" name="form1" method="post" action="">
    <h1>Sign-up form</h1>
    <p>This is the basic look of my form without table</p>
    <label>Name
        <span class="small">Add your name</span>
    </label>
    <input type="text" name="textfield" id="textfield" />

    <label>Email
    <span class="small">Add a valid address</span>
    </label>
    <input type="text" name="textfield" id="textfield" />

    <label>Email
    <span class="small">Add a valid address</span>
    </label>
    <!-- Problem --->
    <input type="radio" name="r1" id="r1" class="radio" value="1" /><label class="radio" for="r1">One</label>
    <input type="radio" name="r2" id="r2" class="radio" value="2" /><label class="radio" for="r2">Two</label>
    <!-- Problem --->
    <button  type="submit">Sign-up</button>
    <div class="spacer"></div>


  </form>
</div>

答案 1 :(得分:2)

(在样式标签内) 添加这些新的样式规则:

#basic input.radio
{
    width:20px;

}
#basic label.radiolabel
{
    width:40px;
    text-align:left;
    line-height:24px;
}
你的HTML中的

为每个标签添加一个新类,如下所示:

<!-- Problem --->    
<input type="radio" name="textfield" id="r1" class="radio" value="1" />
<label for="r1" class="radiolabel">One</label>    
<input type="radio" name="textfield" id="r2" class="radio" value="2" />
<label for="r2" class="radiolabel">Two</label>    
<!-- Problem --->

编辑:为标签的样式添加了行高:)

答案 2 :(得分:1)

根据规范,允许将表单输入元素放在引用它们的标签内,例如,

<label for="input">Label: <input type="radio" id="input" name="input" /></label>

看看是否能让造型更容易。 (它应该。)

答案 3 :(得分:0)

您将它们命名为与文本输入相同 - 将name="textfield"更改为name="my_radio"

您也没有输入电子邮件地址,因此您的标签与输入不同步

答案 4 :(得分:0)

你的样式#basic输入给表格中的所有输入字段宽度为200px,包括你的单选按钮,创建一个像#basic input.radio这样的新样式,并给单选按钮一个单独的宽度,如20px(确保你把它放在#basic输入样式之后,你还必须为与单选按钮链接的标签指定一个较小的宽度,为它们添加一个类,并将它们的宽度减小到40px。

然后,单选按钮及其相关标签应相互对齐。

答案 5 :(得分:0)

要解决布局问题,请添加以下CSS:

#basic input.radio { width:20px; }

这会修复单选按钮的宽度 - 它们从

获得宽度
#basic input { width:200px;}

正在使所有输入宽200px。

总的来说,您的ID和类的应用并不真正促进重用,但这是另一个问题。