html - 如何使用内部阴影和滚动条正确设置文本区域的样式

标签 html css textarea shadow

我使用以下 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;
}

加上一些其他样式,看起来像这样(在 Firefox 中,在其他浏览器中类似):

enter image description here

但是有助于将内容与内部阴影分开的填充破坏了滚动条周围的文本区域:

enter image description here

如果我删除填充,文本会覆盖阴影,如下所示:

enter image description here

我可以只在左侧添加填充,解决与左阴影重叠但不与顶部阴影重叠的问题,同时让滚动条看起来不错:

enter image description here

我还可以在除右侧以外的任何地方添加填充,使文本显示正确,但工具栏看起来仍然很奇怪:

enter image description here

有什么办法可以解决吗?

最佳答案

使用标准文本区域元素不可能使 padding 属性只影响内容而不影响滚动条。为此,您可以使用 contenteditable DIV。例如,检查这个 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;
}
​

关于html - 如何使用内部阴影和滚动条正确设置文本区域的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8895332/

相关文章:

javascript - 为文本区域内的线条着色

javascript - 检查 textarea 中的值是否为 ascii

jquery - 使用 Jquery 查找具有特定属性的文本区域的数量

html - 如何轻松覆盖 Bootstrap CSS

html - Vue.js--CSS 不适用于 v-html 元素

javascript - 动态生成的输入框调整大小并刷新位置和大小

html - SVG 在 Firefox 18 中不工作

html - 需要创建这个 HTML/CSS 网格布局

javascript - 如何自动检测当前表单元素具有相同的类名

css - 为什么这个 div 元素没有延伸到屏幕顶部?