Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add padding to a textarea without causing the width to increase?

Tags:

I've been having trouble setting a textarea element's width and using padding via CSS. The padding value seems to change the width of the textarea, which I'd rather it not do.

This is my HTML code:

<div id="body">     <textarea id="editor"></textarea> </div> 

And my CSS code:

#body {     height:100%;     width:100%;     display:block; }  #editor {     height:100%;     width:100%;     display:block;     padding-left:350px;     padding-right:350px; } 

However, the padding values do not appear to work as one would expect. The width of the textarea is increased by 350px in both directions, rather than defining space between the borders of the element and its content.

I've considered just centering the textarea by setting the margins at "0px auto", but I would like the user to still be able to scroll through the textarea if the mouse is hovering over one of the empty margins. For the same reason I can't add another div to act as a wrapper, as the user wouldn't be able to scroll along the empty areas but only along the margin-less textarea.

Can anybody help?

like image 676
木川 炎星 Avatar asked Nov 15 '10 01:11

木川 炎星


People also ask

How do you change padding without increasing width?

To avoid the width or height getting increased or decreased when using CSS properties like margin , padding , etc, we can use the CSS property called box-sizing and set its value to border-box on the element in CSS.

Does padding change the width?

Normally, when an element's size is set, the width and height properties determine the width and height of the element's content box. Any padding added to the element will increase the total computed width and/or height of the element—this is how the default box model works in regards to sizing the element.

Does padding add to the width of an element?

Padding and Element Width The content area is the portion inside the padding, border, and margin of an element (the box model). So, if an element has a specified width, the padding added to that element will be added to the total width of the element.

How can I fix width and height of textarea?

To prevent a text field from being resized, you can use the CSS resize property with its "none" value. After it you can use the height and width properties to define a fixed height and width for your <textarea> element.


2 Answers

The CSS box model defines "width" as the width of the content, excluding border, padding and margin.

Fortunately, CSS3 has a new box-sizing property that lets you modify this behaviour to instead include padding etc. in the specified width using:

box-sizing: border-box; 

According to the link above, most modern browsers (including IE >= 8) support this property, but they have different names in each browser.

like image 76
casablanca Avatar answered Oct 05 '22 15:10

casablanca


Specifying widths and margins/padding in '%' helps. Here is one example -

Live @ http://jsfiddle.net/ninadpachpute/V2aaa/embedded/result

#body {   background-color:#ccc;   height:100%;   width:100%;   display:block; }  textarea#editor {   border:none;   width:80%;   height:100%;   margin-left:10%;   margin-right:10%; } 
like image 40
Ninad Avatar answered Oct 05 '22 16:10

Ninad