zoukankan      html  css  js  c++  java
  • 如何给 legend 标签设定宽度

    我们在做表单的时候经常会使用到这样的结构:

    <fieldset>
       
    <legend>哪些浏览器legend标签设定的宽度有效</legend>
       
    <input type="checkbox" value="ie6" name="width" id="ie6" checked="checked" /><label for="ie6">IE6</label>
       
    <input type="checkbox" value="ie7" name="width" id="ie7"checked="checked" /><label for="firefox">IE7</label>
       
    <input type="checkbox" value="firefox2" name="width" id="firefox2" /><label for="firefox2">Firefox2</label>
       
    <input type="checkbox" value="firefox3" name="width" id="firefox3" /><label for="firefox3">Firefox3</label>
       
    <input type="checkbox" value="opera" name="width" id="opera" checked="checked" /><label for="opera">Opera9.0+</label>
       
    <input type="checkbox" value="safari" name="width" id="safari"checked="checked" /><label for="safari">Safari3.0+</label>
    </fieldset>

    当我们使用 CSS 给 legend 标签设定固定宽度时:

    legend {
        background
    :red;
        width
    :500px;
    }

    在 IE6、IE7、Opera9.0+、Safari3.0+ 都正能常显示,而在 Firefox2 和 Firefox3 中宽度却失效。

    在这里我们不去深究为什么,只探讨解决的方法:

    我们可以通过在 legend 标签内添加一个标签,并给标签设定所需要的宽度,此宽度的单位不可为百分比(%):

    HTML 修改为:

    <fieldset>
       
    <legend><span>哪些浏览器legend标签设定的宽度有效</span></legend>
       
    <input type="checkbox" value="ie6" name="width" id="ie6" checked="checked" /><label for="ie6">IE6</label>
       
    <input type="checkbox" value="ie7" name="width" id="ie7"checked="checked" /><label for="firefox">IE7</label>
       
    <input type="checkbox" value="firefox2" name="width" id="firefox2" /><label for="firefox2">Firefox2</label>
       
    <input type="checkbox" value="firefox3" name="width" id="firefox3" /><label for="firefox3">Firefox3</label>
       
    <input type="checkbox" value="opera" name="width" id="opera" checked="checked" /><label for="opera">Opera9.0+</label>
       
    <input type="checkbox" value="safari" name="width" id="safari"checked="checked" /><label for="safari">Safari3.0+</label>
    </fieldset>

    CSS 修改为:

    legend span {
        background
    :red;
        width
    :500px;
        display
    :block;
    }

    可参考:《how to set width of LEGEND tags in FF》

    正淳 同时也提供了另外的一种解决方案,无需修改结构,仅修改样式即可:

    legend {
        background
    :red;
        text
    -indent:-600px;
        padding
    -left:600px;

       
    /*IE下还原初始方式,只设定宽度*/
       
    *width:600px;
       
    *text-indent:0;
       
    *padding-left:0;
    }

    题外话:CSS 的兼容其实并不难,多尝试多实践就可以解决,最重要的是自己要去动手,只有动手了才会有更多的收获,才会有更深的印象。

  • 相关阅读:
    [文摘20070723]最经典的爱情观
    [转]ASP .Net C# 下 Word开发资料
    [引]如何藉由使用 Visual C# . NET 處理 Word 中的事件
    简单的搭建Web系统常用的框架页面
    Gentle 简单配置方法之一种
    在 可编辑的 Div 的 光标位置 插入 文字 或 HTML
    [转]使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图
    VS2005 添加 Microsoft.Office.Tools.Word.dll 等引用
    要事第一 事不过三
    VC slider用法
  • 原文地址:https://www.cnblogs.com/c9log/p/1634494.html
Copyright © 2011-2022 走看看