textarea's rows, and cols attribute in CSS

后端 未结 5 1286
南方客
南方客 2020-12-07 13:37

I\'d like to set the textarea\'s rows and cols attributes via CSS.

How would I do this in CSS?

5条回答
  •  天涯浪人
    2020-12-07 14:20

    I just wanted to post a demo using calc() for setting rows/height, since no one did.

    body {
      /* page default */
      font-size: 15px;
      line-height: 1.5;
    }
    
    textarea {
      /* demo related */
      width: 300px;
      margin-bottom: 1em;
      display: block;
      
      /* rows related */
      font-size: inherit;
      line-height: inherit;
      padding: 3px;
    }
    
    textarea.border-box {
      box-sizing: border-box;
    }
    
    textarea.rows-5 {
      /* height: calc(font-size * line-height * rows); */
      height: calc(1em * 1.5 * 5);
    }
    
    textarea.border-box.rows-5 {
      /* height: calc(font-size * line-height * rows + padding-top + padding-bottom + border-top-width + border-bottom-width); */
      height: calc(1em * 1.5 * 5 + 3px + 3px + 1px + 1px);
    }

    height is 2 rows by default

    height is 5 now

    border-box height is 5 now

    If you use large values for the paddings (e.g. greater than 0.5em), you'll start to see the text that overflows the content(-box) area, and that might lead you to think that the height is not exactly x rows (that you set), but it is. To understand what's going on, you might want to check out The box model and box-sizing pages.

提交回复
热议问题