对表单应用样式

这篇博客详细介绍了如何使用CSS对表单进行样式设计,包括简单的表单布局、复杂的表单布局、部分标签的隐藏以及实现多列复选框的效果。内容引用自《精通CSS 高级Web标准解决方案(第2版)》第7章,通过实例代码展示了如何通过CSS控制label的显示、浮动、宽度等属性,以达到理想的表单样式。还特别讨论了如何处理屏幕阅读器的访问性和多列复选框的布局策略。
摘要由CSDN通过智能技术生成

参考《精通CSS 高级Web标准解决方案(第2版)》第7章

关于label的使用参考如下链接:https://blog.csdn.net/gnail_oug/article/details/72852150

1. 简单的表单布局

效果(这里对Name项focus)

HTML

<div id="content">
        <form>
            <fieldset>
                <legend>Your Contact Details</legend>
                <div>
                    <label for="author">Name:<em class="required">(required)</em></label>
                    <input name="author" id="author" type="text" />
                </div>
                <div>
                    <label for="email">Email Address:</label>
                    <input name="email" id="email" type="text" />
                </div>
                <div>
                    <label for="url">Web Address: </label>
                    <input name="url" id="url" type="text" />
                </div>
            </fieldset>
            <fieldset>
                <legend>Comments</legend>
                <div>
                    <label for="text">Message:<em class="required">(required)</em></label>
                    <textarea name="text" id="text"></textarea>
                </div>
            </fieldset>
            <fieldset id="remeber-me">
                <legend>Remember Me</legend>
                <div>
                    <label for="remeber-yes"><input id="remeber-yes" class="radio" name="remember" type="radio" value="yes" checked="checked"/>Yes</label>
                    <label for="remeber-no"><input id="remeber-no" class="radio" name="remember" type="radio" value="no"/>No</label>
                </div>
            </fieldset>
        </form>
    </div>

CSS

<style type="text/css">
        * {
            padding:0;
            margin:0;
        }
        #content{
            width:400px;
            padding: 50px;
        }
        fieldset {
            margin:1em 0;
            padding: 1em;
            border: 1px solid #ccc;
            background-color: #f8f8f8;
        }
        legend {
            font-weight: bold;
        }
        label {
            display: block;
            cursor: pointer;
        }
        input[type="text"],input[type="password"],textarea {
            width:20em;
            border-top:2px solid #999;
            border-left:2px solid #999;
            border-bottom: 1px solid #ccc;
            border-right:1px solid #ccc;
        }
        input[type="text"]:focus,input[type="password"]:focus,textarea:focus {
            background-color: #ffc;
        }
        input.radio,input.checkbox,input.submit {
            width:auto;
        }
        textarea {
            height:5em;
        }
        .required {
            font-size: 0.75em;
            color:#760000
        }
        #remeber-me .radio {
            margin-right: 1em;
        }
    </style>

2. 复杂的表单布局

效果

 

      对1.1的代码修改如下:适当增加#content的宽度。将label的display:block去掉,换成左浮动,为了消除浮动的包裹性,加上width:10em。此外,对label的父元素div清除浮动,这里采用overflow:hidden。

        #content{
            width:500px;
            padding: 50px;
        }
        fieldset >div {
            overflow: hidden;
        }


3. 部分标签的隐藏、多列复选框

     这一节给出两个应用:

  1. 有时候不希望每个元素都显示标签。
  2. 多列复选框

    效果

      在第2节代码的基础上,添加一个fieldset标签用于防止Personal Information。其中Place of Information是一个select标签以及相应的label,Date of Birth则为三个标签(input、select、input)以及相应的label,对于后面两个label使用text-indent负值隐藏,并将对应的label宽度清空。这里没有使用display:none的原因是这会阻止许多屏幕阅读器的访问。

     复选框采用隐式的label形式。对两列复选框分别放置在一个class为col的div中,设置div的宽度为一个较小的值(这里为8em)。这里的label沿用了前面代码的float:left、width:10em和cursor:pointer,保持这些设置不变,我们为div.col设置float:left来包围子元素,同时两个div.col也实现了左浮动,进而水平排列。div.col的父元素div沿用了前面代码form >div的overflow:hidden,因此能够对两个div.col的浮动进一步清除。

    此外,这里使用<h2>标签来模拟label,因此需要对原始label的一些样式进行复用,具体来说为float:left,width:10em,cursor:pointer,此外还要对font-size和font-weight进行设置以覆盖<h2>标签自带的样式。

   最后,我们对form中的line-height设置为2,增大行间距,更加美观。

<fieldset>
                <legend>Personal Information</legend>
                <div>
                    <label for="placeOfBirth">Place of Birth:</label>
                    <select name="placeOfBirth" id="placeOfBirth">
                        <option>China</option>
                        <option>USA</option>
                        <option>Russia</option>
                    </select>
                </div>
                <div>
                    <label for="dateOfBirth">Date of Birth:</label>
                    <input name="dateOfBirth" id="dateOfBirth" type="text"/>
                    <label for="monthOfBirth" id="monthOfBirthLabel">Month of Birth:</label>
                    <select name="monthOfBirth" id="monthOfBirth">
                        <option value="1">January</option>
                        <option value="2">February</option>
                        <option value="3">March</option>
                    </select>
                    <label for="yearOfBirth" id="yearOfBirthLabel">Year of Birth:</label>
                    <input name="yearOfBirth" id="yearOfBirth" type="text"/>
                </div>
                <div id="favoriteColor">
                    <h2>Favorite color:</h2>
                    <div class="col">
                        <label><input name="red" id="red" type="checkbox" class="checkbox"/>red</label>
                        <label><input name="yellow" id="yellow" type="checkbox" class="checkbox"/>yellow</label>
                        <label><input name="pink" id="pink" type="checkbox" class="checkbox"/>pink</label>
                        <label><input name="green" id="green" type="checkbox" class="checkbox"/>green</label>
                    </div>
                    <div class="col">
                        <label><input name="orange" id="orange" type="checkbox" class="checkbox"/>orange</label>
                        <label><input name="purple" id="purple" type="checkbox" class="checkbox"/>purple</label>
                        <label><input name="blue" id="blue" type="checkbox" class="checkbox"/>blue</label>
                        <label><input name="other" id="other" type="checkbox" class="checkbox"/>other</label>
                    </div>
                </div>
            </fieldset>
 CSS:
#content{
            width:500px;
            padding: 50px;
            line-height: 2;
        }
input#dateOfBirth {
            width:3em;
            margin-right: 0.5em;
        }
        input#yearOfBirth {
            width: 5em;
        }
        select#monthOfBirth {
            width:10em;
            margin-right: 0.5em;
        }
        #monthOfBirthLabel,#yearOfBirthLabel {
            text-indent: -1000em;
            width:0;
        }
        .col {
            width:8em;
            float:left;
        }
        #favoriteColor h2 {
            width:10em;
            font-size: 1em;
            font-weight: normal;
            float:left;
            cursor: pointer;
        }

4. 完整效果及代码

完整效果

HTML

 

<div id="content">
        <form>
            <fieldset>
                <legend>Your Contact Details</legend>
                <div>
                    <label for="author">Name:<em class="required">(required)</em></label>
                    <input name="author" id="author" type="text" />
                </div>
                <div>
                    <label for="email">Email Address:</label>
                    <input name="email" id="email" type="text" />
                </div>
                <div>
                    <label for="url">Web Address: </label>
                    <input name="url" id="url" type="text" />
                </div>
            </fieldset>
            <fieldset>
                <legend>Comments</legend>
                <div>
                    <label for="text">Message:<em class="required">(required)</em></label>
                    <textarea name="text" id="text"></textarea>
                </div>
            </fieldset>
            <fieldset id="remeber-me">
                <legend>Remember Me</legend>
                <div>
                    <label for="remeber-yes"><input id="remeber-yes" class="radio" name="remember" type="radio" value="yes" checked="checked"/>Yes</label>
                    <label for="remeber-no"><input id="remeber-no" class="radio" name="remember" type="radio" value="no"/>No</label>
                </div>
            </fieldset>
            <fieldset>
                <legend>Personal Information</legend>
                <div>
                    <label for="placeOfBirth">Place of Birth:</label>
                    <select name="placeOfBirth" id="placeOfBirth">
                        <option>China</option>
                        <option>USA</option>
                        <option>Russia</option>
                    </select>
                </div>
                <div>
                    <label for="dateOfBirth">Date of Birth:</label>
                    <input name="dateOfBirth" id="dateOfBirth" type="text"/>
                    <label for="monthOfBirth" id="monthOfBirthLabel">Month of Birth:</label>
                    <select name="monthOfBirth" id="monthOfBirth">
                        <option value="1">January</option>
                        <option value="2">February</option>
                        <option value="3">March</option>
                    </select>
                    <label for="yearOfBirth" id="yearOfBirthLabel">Year of Birth:</label>
                    <input name="yearOfBirth" id="yearOfBirth" type="text"/>
                </div>
                <div id="favoriteColor">
                    <h2>Favorite color:</h2>
                    <div class="col">
                        <label><input name="red" id="red" type="checkbox" class="checkbox"/>red</label>
                        <label><input name="yellow" id="yellow" type="checkbox" class="checkbox"/>yellow</label>
                        <label><input name="pink" id="pink" type="checkbox" class="checkbox"/>pink</label>
                        <label><input name="green" id="green" type="checkbox" class="checkbox"/>green</label>
                    </div>
                    <div class="col">
                        <label><input name="orange" id="orange" type="checkbox" class="checkbox"/>orange</label>
                        <label><input name="purple" id="purple" type="checkbox" class="checkbox"/>purple</label>
                        <label><input name="blue" id="blue" type="checkbox" class="checkbox"/>blue</label>
                        <label><input name="other" id="other" type="checkbox" class="checkbox"/>other</label>
                    </div>
                </div>
            </fieldset>
            <p>
                <button id="submit" type="submit">Book Now >></button>
            </p>
        </form>
    </div>
CSS

<style type="text/css">
        * {
            padding:0;
            margin:0;
        }
        #content{
            width:500px;
            padding: 50px;
            line-height: 2;
        }
        fieldset {
            margin:1em 0;
            padding: 1em;
            border: 1px solid #ccc;
            background-color: #f8f8f8;
        }
        legend {
            font-weight: bold;
        }
        label {
            float:left;
            width:10em;
            cursor: pointer;
        }
        input[type="text"],input[type="password"],textarea {
            width:20em;
            border-top:2px solid #999;
            border-left:2px solid #999;
            border-bottom: 1px solid #ccc;
            border-right:1px solid #ccc;
        }
        input[type="text"]:focus,input[type="password"]:focus,textarea:focus {
            background-color: #ffc;
        }
        input.radio,input.checkbox,input.submit {
            width:auto;
        }
        textarea {
            height:5em;
        }
        .required {
            font-size: 0.75em;
            color:#760000
        }
        #remeber-me .radio {
            margin-right: 1em;
        }
        fieldset > div {
            overflow: hidden;
        }
        input#dateOfBirth {
            width:3em;
            margin-right: 0.5em;
        }
        input#yearOfBirth {
            width: 5em;
        }
        select#monthOfBirth {
            width:10em;
            margin-right: 0.5em;
        }
        #monthOfBirthLabel,#yearOfBirthLabel {
            text-indent: -1000em;
            width:0;
        }
        .col {
            width:8em;
            float:left;
        }
        #favoriteColor h2 {
            width:10em;
            font-size: 1em;
            font-weight: normal;
            float:left;
            cursor: pointer;
        }
        button#submit {
            width:200px;height:50px;
            border:1px solid #989898;
            -moz-border-radius: 6px;
            -webkit-border-radius: 6px;
            border-radius: 6px;
            -moz-box-shadow: 2px 2px 2px #ccc;
            -webkit-box-shadow: 2px 2px 2px #ccc;
            box-shadow: 2px 2px 2px #ccc;
            color:#fff;
            font-size: 16px;
            font-weight: bold;
            text-shadow: 1px 1px 1px #666;
            background-color: #8cca12;
            cursor: pointer;
        }
    </style>
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好看的form表单CSS样式可以通过以下几个方面来实现: 1. 字体样式应用:可以通过CSS的字体样式属性来改变表单元素的字体样式,包括字体族科、字体风格、字体变形、字体加粗、字体大小等。可以根据需要给不同的表单元素设置不同的字体样式,比如按钮、文本框、下拉选择框等。 2. 设置宽高:可以使用CSS的宽度和高度属性来调整表单元素的大小。通过设置适当的宽度和高度,可以使表单元素看起来更加整齐美观。可以给整个表单容器设置宽度和高度,同时也可以给各个表单元素设置宽度和高度。 3. 边框样式:可以使用CSS的边框样式属性来给表单元素添加边框效果,通过调整边框的颜色、粗细和样式,可以改变表单元素的外观。可以给文本框、按钮等表单元素添加边框样式,使其更加突出。 4. 背景样式:可以使用CSS的背景样式属性来给表单元素添加背景效果,包括背景颜色和背景图片。通过设置适当的背景颜色或者背景图片,可以增加表单元素的视觉效果。可以给按钮、文本框等表单元素设置背景颜色或者背景图片,使其更加吸引人。 需要注意的是,以上只是一些常见的CSS样式应用,实际上可以根据具体需求和创意来设计出更加独特和个性化的表单样式。可以通过调整各种CSS属性来实现不同的效果,以达到满足用户需求和提升用户体验的目的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [表单花样样式](https://blog.csdn.net/weixin_30652879/article/details/97455140)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Form 表单详解:案例、CSS 美化](https://blog.csdn.net/hongshuteng/article/details/127401803)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值