;overflow:scr"/> ;overflow:scr">
search
HomeWeb Front-endCSS TutorialMake a div scroll horizontally using CSS

使用 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 设计了滚动条。

The above is the detailed content of Make a div scroll horizontally using CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Using Pages CMS for Static Site Content ManagementUsing Pages CMS for Static Site Content ManagementMay 13, 2025 am 09:24 AM

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

The Ultimate Guide to Linking CSS Files in HTMLThe Ultimate Guide to Linking CSS Files in HTMLMay 13, 2025 am 12:02 AM

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

CSS Flexbox vs Grid: a comprehensive reviewCSS Flexbox vs Grid: a comprehensive reviewMay 12, 2025 am 12:01 AM

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

How to Include CSS Files: Methods and Best PracticesHow to Include CSS Files: Methods and Best PracticesMay 11, 2025 am 12:02 AM

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframes CSS: The most used tricks@keyframes CSS: The most used tricksMay 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools