搜索
首页web前端css教程使用 CSS 使 div 水平滚动

使用 CSS 使 div 水平滚动

在 CSS 中,我们可以通过设置适当的“over-flow”属性值来使 div 水平滚动。

首先,让我们了解为什么我们需要使 div 水平滚动。例如,父div元素的宽度为500px,或者屏幕尺寸为500px。现在,div 元素的内容是 1500px。因此,如果我们不让父 div 水平滚动,就会破坏应用程序的 UI。因此,我们可以使其可滚动,用户可以滚动查看不可见的内容。

语法

用户可以按照以下语法使 div 水平滚动。

.scroll { 
   width:<Width_of_div_element>; 
   overflow: scroll; 
}

在上面的语法中,我们使用了“width”和“overflow”属性来使 div 水平滚动。如果我们不指定元素的宽度,“overflow”属性不会受到影响。

示例 1

在下面的示例中,我们创建了 HTML div 并添加了一些文本内容。在 CSS 中,我们为 div 元素指定了“500px”固定宽度。此外,我们还设置了“scroll”作为溢出属性的值。

在输出中,用户可以观察到,当文本宽度大于类名为“scroll”的 div 的宽度时,div 元素中出现水平滚动条。

<html>
<head>
   <style>
      .scroll {
         margin: 5px;
         padding: 5px;
         border: 2px solid blue;
         width: 500px;
         overflow: scroll;
         white-space: nowrap;
         height: 50px;
      }
   </style>
</head>
<body>
   <h3 id="Using-the-i-overflow-x-scroll-i-to-make-a-div-horizontally-scrollable-using-CSS">Using the <i> overflow-x: scroll </i> to make a div horizontally scrollable using CSS</h3>
   <div class = "scroll">
      The United States of America (USA) has the largest GDP in the world. In 2020, the nominal GDP of the United
      States was $21.43 trillion. China is the second-largest economy in the world, with a nominal GDP of $14.34
      trillion in 2020. Other countries with large GDPs include Japan, Germany, and the United Kingdom.
   </div>
</body>
</html>

示例 2

在下面的示例中,我们使用了“overflow: auto”CSS 属性来使 div 水平滚动。此外,我们还为 div 元素指定了固定宽度。 ‘auto’和‘scroll’值之间的主要区别是,当我们使用‘scroll’时,div始终保持可滚动,而当我们使用‘auto’时,div元素在发生溢出时变得可滚动。

<html>
<head>
   <style>
      .scroll {
         border: 2px solid green;
         width: 500px;
         overflow: auto;
         white-space: nowrap;
         height: 50;
      }
   </style>
</head>
<body>
   <h3 id="Using-the-i-overflow-auto-i-to-make-a-div-horizontally-scrollable-using-CSS">Using the <i> overflow: auto </i> to make a div horizontally scrollable using CSS</h3>
   <div class = "scroll">
      This is a sample text to put in the HTML div element. You can scroll this div horizontally.
   </div>
</body>
</html>

示例 3

在下面的示例中,我们使用了“overflow-x: auto”CSS 属性来使 div 水平滚动。我们已在父 div 内添加了单个子 div。

在输出中,用户可以观察到,由于我们使用了“auto”值,最初外部 div 是不可滚动的。当我们点击“添加 div”按钮时,它会使用 JavaScript 将子 div 添加到父 div,并且当您添加 5 到 7 个子 div 时,它会自动变得可滚动。

<html>
<head>
   <style>
      .scroll { border: 2px solid green; width: 500px; overflow-x: auto; white-space: nowrap; display: flex; flex-direction: row; }
      .inner { max-width: 250px; height: 100px; background-color: red; border: 2px dotted blue; margin: 3px; }
   </style>
</head>
<body>    
   <h3 id="Using-the-i-overflow-x-auto-i-to-make-a-div-horizontally-scrollable-using-CSS">Using the <i> overflow-x: auto </i> to make a div horizontally scrollable using CSS</h3>
   <div class = "scroll">
      <div class = "inner">
         Inner Div
      </div>
   </div>
   <button onclick = "addDiv()"> Add div </button>
</body>
<script>
   function addDiv() {
      // add the div element to the scroll div
      let scroll = document.querySelector('.scroll');
      let new_div = document.createElement('div');
      new_div.classList.add('inner');
      new_div.innerHTML = 'Inner Div';
      scroll.appendChild(new_div);
   }
</script>
</html>

示例 4

在下面的示例中,我们使用 CSS 设计了滚动条。首先,我们使 div 可以水平滚动以显示滚动条。之后,我们设置滚动条的背景颜色。此外,我们还设计了滚动条轨道和滑块。

在输出中,用户可以观察到好看的滚动条,并根据需求进行设计。

<html>
<head>
   <style>
      .scroll {
         width: 500px;
         overflow-x: auto;
         overflow-y: hidden;
         white-space: nowrap;
         border: 1px solid red;
         scrollbar-width: thin;
         scrollbar-color: grey;
      }
      /* styling the scroll bar */
      .scroll::-webkit-scrollbar { width: 10px; height: 10px }
      .scroll::-webkit-scrollbar-track { background: darkgray;}
      .scroll::-webkit-scrollbar-thumb { background: grey; border-radius: 10px; }
   </style>
</head>
<body>
   <h3 id="Using-the-i-overflow-x-auto-i-to-make-a-div-horizontally-scrollable-using-CSS">Using the <i> overflow-x: auto </i> to make a div horizontally scrollable using CSS</h3>
   <div class = "scroll">
      This div is horizontally scrollable. How are you? Do you like CSS? Tutorials Point is a leading Ed Tech company striving to provide the best learning material on technical and non-technical subjects. 
   </div>
</body>
</html>

用户学会了使用 CSS 的“overflow”属性使 div 水平滚动。在第一个示例中,我们使用了“overflow:scroll”CSS 属性。在第二个示例中,我们使用了“overflow: auto”CSS 属性。在第三个示例中,我们使用了“overflow-x: auto”CSS 属性;在最后一个示例中,我们使用 CSS 设计了滚动条。

以上是使用 CSS 使 div 水平滚动的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
@rules具有多少特异性,例如@keyframes和@media?@rules具有多少特异性,例如@keyframes和@media?Apr 18, 2025 am 11:34 AM

前几天我得到了这个问题。我的第一个想法是:奇怪的问题!特异性是关于选择者的,而在符号不是选择器,那么...无关紧要?

您可以嵌套@Media和@support查询吗?您可以嵌套@Media和@support查询吗?Apr 18, 2025 am 11:32 AM

是的,您可以,而且它并不重要。不需要CSS预处理器。它在常规CSS中起作用。

快速吞噬缓存破坏快速吞噬缓存破坏Apr 18, 2025 am 11:23 AM

您应该肯定会在CSS和JavaScript(以及图像和字体以及其他内容)等资产上设置遥远的高速缓存标头。告诉浏览器

寻找可以监视CSS质量和复杂性的堆栈寻找可以监视CSS质量和复杂性的堆栈Apr 18, 2025 am 11:22 AM

许多开发人员写了如何维护CSS代码库的文章,但并没有很多关于如何测量该代码库质量的文章。当然,我们有

数据学家用于建议不执行值的值数据学家用于建议不执行值的值Apr 18, 2025 am 11:08 AM

您是否曾经有一种需要接受简短而任意的文本的表格?喜欢名字或其他。那完全是用的。有很多

苏黎世的最初会议苏黎世的最初会议Apr 18, 2025 am 11:03 AM

我很高兴能前往瑞士苏黎世参加前界(Love the Name and URL!)。我以前从未去过瑞士,所以我很兴奋

使用CloudFlare工人建立全栈无服务器应用程序使用CloudFlare工人建立全栈无服务器应用程序Apr 18, 2025 am 10:58 AM

我在软件开发方面最喜欢的发展之一是无服务器的出现。作为一个倾向于陷入细节的开发人员

在NUXT应用程序中创建动态路由在NUXT应用程序中创建动态路由Apr 18, 2025 am 10:53 AM

在这篇文章中,我们将使用我构建和部署的电子商务商店演示来进行Netlify,以展示如何为传入数据制作动态路线。这是一个公平的

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用