【问题标题】:Textarea in Fieldset leaves a bottom gapFieldset 中的 Textarea 留下底部间隙
【发布时间】:2017-03-02 09:42:16
【问题描述】:

我试图弄清楚为什么 Chrome 在 fieldset 标记中使用 textarea 时会留下底部空白。

如果你用 Chrome 看下面的小提琴,你会希望明白我的意思

https://jsfiddle.net/552aamop/1/

小提琴 CSS 包含 Eric Meyers 标准重置,我已经重置了 input 和 textarea 标签的边距和填充。 HTML 的元素之间不包含空格。

<form>
<fieldset><input type='text'/></fieldset>
<fieldset><input type='text'/></fieldset>
<fieldset><textarea>Hello</textarea></fieldset>
<fieldset><textarea>Hello</textarea></fieldset>
</form>  

Firefox 和 IE 似乎在字段集之间呈现没有间隙。 Chrome 会留下空隙,但只有当字段集包含文本区域时才会留下空隙。知道为什么吗?

我使用的是 Chrome 版本 56.0.2924.87。

【问题讨论】:

    标签: html css google-chrome textarea fieldset


    【解决方案1】:

    当您在基地使用font: inherit; 时。 Chrome 正在添加其默认行高。 Firefox 的默认行高似乎为 1.1,但 Chrome 的默认行高为 1.2。

    简写:font: font-style font-variant font-weight font-size/line-height font-family;

    这将解决您的问题。

    fieldset{ line-height:0}
    

    查看此fiddle

    【讨论】:

    • 谢谢,这对我来说效果最好。我将 line-height 声明移到了文本区域标签中,以免影响任何其他元素。
    【解决方案2】:

    字段集不会在它们之间留下空隙——相反,里面的文本区域并没有完全填满它们。这样做的原因是 textarea 具有“display: inline-block”的默认属性,这意味着它的高度将由里面的内容定义。我认为另一部分是 Chrome 给它一个最小高度。

    所以我建议您将 CSS 属性“display:block”获取 Textarea,以便它填充其容器的高度。

    【讨论】:

    • 是的,这确实解决了这个问题,但我希望文本区域旁边有一个不浮动的标签标签,所以这不是我正在做的网站的解决方案,感谢您的关注。跨度>
    【解决方案3】:

    在其他答案的基础上,将vertical-align:middle 赋予 textarea 元素也可以。

    /* http://meyerweb.com/eric/tools/css/reset/
       v2.0 | 20110126
       License: none (public domain)
    */
    html, body, div, span, applet, object, iframe,
    h1, h2, h3, h4, h5, h6, p, blockquote, pre,
    a, abbr, acronym, address, big, cite, code,
    del, dfn, em, img, ins, kbd, q, s, samp,
    small, strike, strong, sub, sup, tt, var,
    b, u, i, center,
    dl, dt, dd, ol, ul, li,
    fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td,
    article, aside, canvas, details, embed,
    figure, figcaption, footer, header, hgroup,
    menu, nav, output, ruby, section, summary,
    time, mark, audio, video {
    	margin: 0;
    	padding: 0;
    	border: 0;
    	font-size: 100%;
    	font: inherit;
    	vertical-align: baseline;
    }
    /* HTML5 display-role reset for older browsers */
    article, aside, details, figcaption, figure,
    footer, header, hgroup, menu, nav, section {
    	display: block;
    }
    body {
    	line-height: 1;
    }
    ol, ul {
    	list-style: none;
    }
    blockquote, q {
    	quotes: none;
    }
    blockquote:before, blockquote:after,
    q:before, q:after {
    	content: '';
    	content: none;
    }
    table {
    	border-collapse: collapse;
    	border-spacing: 0;
    }
    
    input,
    textarea {
      margin:0;
      padding:0;
      /* this works as well */
      vertical-align:middle;
    }
    <form>
      <fieldset><input type='text' /></fieldset>
      <fieldset><input type='text' /></fieldset>
      <fieldset><textarea>Hello</textarea></fieldset>
      <fieldset><textarea>Hello</textarea></fieldset>
    </form>

    【讨论】:

      【解决方案4】:

      使用 display: flex; 所有浏览器都使用相同的输出

      fieldset{
          display: flex;
      }
      

      input, textarea{
              display: flex;
          }
      

      /* http://meyerweb.com/eric/tools/css/reset/
         v2.0 | 20110126
         License: none (public domain)
      */
      html, body, div, span, applet, object, iframe,
      h1, h2, h3, h4, h5, h6, p, blockquote, pre,
      a, abbr, acronym, address, big, cite, code,
      del, dfn, em, img, ins, kbd, q, s, samp,
      small, strike, strong, sub, sup, tt, var,
      b, u, i, center,
      dl, dt, dd, ol, ul, li,
      fieldset, form, label, legend,
      table, caption, tbody, tfoot, thead, tr, th, td,
      article, aside, canvas, details, embed,
      figure, figcaption, footer, header, hgroup,
      menu, nav, output, ruby, section, summary,
      time, mark, audio, video {
      	margin: 0;
      	padding: 0;
      	border: 0;
      	font-size: 100%;
      	font: inherit;
      	vertical-align: baseline;
      }
      /* HTML5 display-role reset for older browsers */
      article, aside, details, figcaption, figure,
      footer, header, hgroup, menu, nav, section {
      	display: block;
      }
      body {
      	line-height: 1;
      }
      ol, ul {
      	list-style: none;
      }
      blockquote, q {
      	quotes: none;
      }
      blockquote:before, blockquote:after,
      q:before, q:after {
      	content: '';
      	content: none;
      }
      table {
      	border-collapse: collapse;
      	border-spacing: 0;
      }
      
      input,
      textarea {
        margin:0;
        padding:0;
      }
      fieldset{
          display: flex;
      }
      <fieldset><input type='text'/></fieldset>
      <fieldset><input type='text'/></fieldset>
      <fieldset><textarea>Hello</textarea></fieldset>
      <fieldset><textarea>Hello</textarea></fieldset>

      【讨论】:

      • 嗨,Flex 并不是一个真正的选项,它还没有完全支持。
      【解决方案5】:

      问题:

      每个浏览器都有一种显示元素的自定义方式。 Chrome 设置 display: block 和一些 webkit 边距,但覆盖它们不起作用。

      fieldset {
          display: block;
          -webkit-margin-start: 2px;
          -webkit-margin-end: 2px;
          -webkit-padding-before: 0.35em;
          -webkit-padding-start: 0.75em;
          -webkit-padding-end: 0.75em;
          -webkit-padding-after: 0.625em;
          min-width: -webkit-min-content;
          border-width: 2px;
          border-style: groove;
          border-color: threedface;
          border-image: initial;
      }
      

      解决方案:

      fieldset {
              display: flex;
          }
      

      【讨论】:

        【解决方案6】:

        你应该在你的 css 中添加以下语句。

        <style> fieldset {padding: 0px;} </style>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-12
          • 2021-06-25
          • 1970-01-01
          • 1970-01-01
          • 2014-05-03
          相关资源
          最近更新 更多