How to properly style a text area with inner shadows and scrollbar

pupeno picture pupeno · Jan 17, 2012 · Viewed 63.7k times · Source

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?

Answer

techfoobar picture techfoobar · Jan 17, 2012

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;
}
​