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

发布于 2024-12-27 05:50:14 字数 847 浏览 1 评论 0原文

我使用以下 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 中,在其他浏览器中类似):

在此处输入图像描述

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

在此处输入图像描述

如果我删除填充,文本会与阴影重叠,如下所示:

在此处输入图像描述

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

<图片src="https://i.sstatic.net/ke42O.png" alt="在此处输入图像描述">

我还可以在除右侧之外的所有位置添加填充,使文本显示正确,但工具栏看起来仍然相当奇怪:

在此处输入图像描述

有什么方法可以解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

咽泪装欢 2025-01-03 05:50:14

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

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;
}
​
甜心小果奶 2025-01-03 05:50:14

您只需将: padding-right: 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. :)

临风闻羌笛 2025-01-03 05:50:14

您可以将文本区域包装在 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/

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/

独行侠 2025-01-03 05:50:14

此外,您可以使用专门针对文本输入的方式,而不是使用简单的输入(以按钮为目标)

input[type="text"]

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

input[type="text"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文