我正在做一个Web项目,我需要定制容器元素的外观和行为.具体地说,我想从容器中删除滚动条,并防止用户 Select 其中的文本.我try 了各种CSS属性和技术,但我还没有找到一个在不同浏览器中可靠工作的解决方案.

以下是我迄今为止try 的:

<div class="custom-container">
  <!-- Content goes here -->
</div>

CSS:

.custom-container {
  width: 300px;
  height: 200px;
  overflow: hidden; /* Hides scroll bars, but still allows scrolling */
  user-select: none; /* Prevents text selection, but doesn't work consistently */
}

虽然设置为overflow: hidden可以成功隐藏滚动条,但它仍然允许用户使用鼠标或touch 手势滚动内容.此外,user-select: none属性阻止文本 Select ,但并不是所有浏览器都统一支持它.

Could someone suggest a reliable CSS-based solution to achieve both hiding scroll bars and preventing text selection within the container element? Any insights or alternative approaches would be greatly appreciated!

推荐答案

you can use a combination of CSS properties and some additional techniques. Here's how you can modify your CSS:

.custom-container {
  width: 300px;
  height: 200px;
  overflow: hidden; /* Hides scroll bars */
}

.custom-container::-webkit-scrollbar {
  display: none; /* Hide scrollbars on WebKit browsers (Chrome, Safari, etc.) */
}

.custom-container {
  -ms-overflow-style: none; /* Hide scrollbars on Internet Explorer */
  scrollbar-width: none; /* Hide scrollbars on Firefox */
}

.custom-container {
  user-select: none; /* Prevent text selection */
  -moz-user-select: none; /* For Firefox */
  -webkit-user-select: none; /* For WebKit browsers */
  -ms-user-select: none; /* For Internet Explorer */
}

Explanation:

  1. overflow: hidden;:这个属性隐藏滚动条,但仍然 允许用户使用鼠标或touch 手势滚动内容.
  2. ::-webkit-scrollbar:这个伪元素用于样式 WebKit浏览器上的滚动条.设置display: none;隐藏了 滚动条.
  3. -ms-overflow-style: none;:此属性特定于互联网 资源管理器和边缘,它隐藏了滚动条.
  4. scrollbar-width: none;章:这个财产,连同 -moz-scrollbar Select 器,隐藏Firefox上的滚动条.
  5. user-select: none;:此属性阻止在 容器.其他特定于供应商的前缀用于 跨浏览器兼容性.

Html相关问答推荐

如何增加行背景的宽度而不影响上面的内容?""' HTML CSS

如何防止SVG图标出现断行?

如何从ThymeLeaf模板中分离CSS

如何使用html和css将两个项目垂直对齐

获得一个css框,以便在页面太小时不再粘在页面的角落.

在一行中对齐文本和图标

assets资源 净值元素不会扩展到涵盖子项

子元素的单一背景

如何在R rmarkdown中创建循环中的分页表?

在css中是否可以在遮罩图像中结合线性渐变和径向渐变?

禁用所有行,但在16角中单击编辑按钮的行除外

顶部有文字的图像的悬停效果

带有数据 URI 的音频在 Chrome 中失败

如何使用 CSS 应用带有笔划的文本阴影?

如何使用 Bootstrap Grid 系统修复大屏幕上图片和进度条之间的空白?

将图像的高度限制为固定的纵横比,并在超过时使用 object-fit

按钮显示:内联不换行

CSS 中的 fingerprint scanner 动画

使用 CSS flexbox 和溢出顶部而不是底部

所有幻灯片上的 Quarto RevealJS 标题