25

I'm putting an inner-shadow on all my controls, inputs and textareas by using the following CSS:

input {
  padding: 7px;
  -webkit-box-shadow: inset 2px 2px 2px 0px #dddddd;
  -moz-box-shadow: inset 2px 2px 2px 0px #dddddd;
  box-shadow: inset 2px 2px 2px 0px #dddddd;
}

and, with some other styling, looks like this (in Firefox, and similar in other browsers):

enter image description here

But the padding that helps separate the content from the inner shadow breaks the textarea around the scrollbar:

enter image description here

and if I remove the padding, the text overlaps the shadow, like this:

enter image description here

I can add padding only to the left, solving the overlap with the left shadow but not with the top shadow, while having the scrollbar look good:

enter image description here

I can also add padding everywhere but on the right side, having the text displayed correctly but the toolbar still looks rather odd:

enter image description here

Is there any way to solve this?

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

4 Answers4

20

Getting the padding property to affect only the content but not the scrollbar is not possible using standard textarea elements. For that you can use a contenteditable DIV. For example, check this fiddle: http://jsfiddle.net/AQjN7/

HTML:

<div class="outer" contenteditable="true">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>

CSS:

.outer {
    -webkit-box-shadow: inset 2px 2px 2px 0px #dddddd;
    -moz-box-shadow: inset 2px 2px 2px 0px #dddddd;
    box-shadow: inset 2px 2px 2px 0px #dddddd;
    height: 200px;
    margin: 10px;
    overflow: auto;
    padding: 7px 10px;
    width: 300px;    
    font-family: tahoma;
    font-size: 13px;
    border: 1px solid #aaa;
}
​
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • I see... oh well... before doing that, I should probably decide whether plain text is ever going to be used, or whether I should go for some rich text editor anyway. Do you know what's the browser support for contenteditable? where is it going to fail? – Pablo Fernandez Jan 17 '12 at 14:48
  • It has pretty good support in all modern browsers including FF, Chrome as well as IE. However browsers as sick as IE7 can break anything. :) – techfoobar Jan 17 '12 at 14:50
  • If you want to post the textarea data with a normal html form, data in a contenteditable div won't be posted. It will be with a textarea. Still a neat idea. – timing Jan 17 '12 at 15:58
  • You can do that by adding a hidden input field and copying the text value from the div prior to submit (i.e onsubmit returning true). – techfoobar Jan 17 '12 at 17:26
  • This solution worked for me on Chrome and Firefox on Mac OS X as well as IE 9, 8, 7 and even 6. I mean, trying out the look and feel, not sure if it messes up the form for example. – Pablo Fernandez Feb 10 '12 at 10:44
  • The trouble with contenteditable is that you can copy+paste (or select+drag) arbitrary formatted HTML into it, which is [sometimes a nice feature](http://www.tinymce.com/tryit/full.php), but not if you're trying to replace a plain textarea. – Boann Jul 28 '12 at 04:43
0

Additionally - rather than using simply input, which will target buttons, you can target text inputs specifically with

input[type="text"]
Tapefreak
  • 982
  • 1
  • 13
  • 16
0

You can just add: padding-right: 0; to the textarea selector and it will line up your scrollbar with the side of the element. :)

Kyle
  • 65,599
  • 28
  • 144
  • 152
0

You could wrap the textarea in a div:

<div class="textarea-wrapper">
    <textarea>
</div>

css:

.textarea-wrapper{
    box-shadow: inset 2px 2px 2px 0px #ddd;
}
textarea {
    padding: 10px;
    background: transparent;
    border: none;
}

A jsFiddle here: http://jsfiddle.net/rXpPy/

timing
  • 6,340
  • 1
  • 17
  • 16