textarea's rows, and cols attribute in CSS

后端 未结 5 1254
南方客
南方客 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 13:55

    I don't think you can. I always go with height and width.

    textarea{
    width:400px;
    height:100px;
    }
    

    the nice thing about doing it the CSS way is that you can completely style it up. Now you can add things like:

    textarea{
    width:400px;
    height:100px;
    border:1px solid #000000;
    background-color:#CCCCCC;
    }
    
    0 讨论(0)
  • 2020-12-07 14:08

    width and height are used when going the css route.

    <!DOCTYPE html>
    <html>
        <head>
            <title>Setting Width and Height on Textareas</title>
            <style>
                .comments { width: 300px; height: 75px }
            </style>
        </head>
        <body>
            <textarea class="comments"></textarea>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-12-07 14:13

    As far as I know, you can't.

    Besides, that isnt what CSS is for anyway. CSS is for styling and HTML is for markup.

    0 讨论(0)
  • 2020-12-07 14:20
    <textarea rows="4" cols="50"></textarea>
    

    It is equivalent to:

    textarea {
        height: 4em;
        width: 50em;
    }
    

    where 1em is equivalent to the current font size, thus make the text area 50 chars wide. see here.

    0 讨论(0)
  • 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);
    }
    <p>height is 2 rows by default</p>
    
    <textarea>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea>
    
    <p>height is 5 now</p>
    
    <textarea class="rows-5">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea>
    
    <p>border-box height is 5 now</p>
    
    <textarea class="border-box rows-5">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea>

    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.

    0 讨论(0)
提交回复
热议问题