Make a div scroll horizontally using CSS
在 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>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>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>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>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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.

accent-color is an attribute used in CSS to customize the highlight colors of form elements such as checkboxes, radio buttons and sliders; 1. It directly changes the default color of the selected state of the form control, such as changing the blue check mark of the checkbox to red; 2. Supported elements include input boxes of type="checkbox", type="radio" and type="range"; 3. Using accent-color can avoid complex custom styles and extra DOM structures, and maintain native accessibility; 4. It is generally supported by modern browsers, and old browsers need to be downgraded; 5. Set accent-col

Thevertical-alignpropertyinCSSalignsinlineortable-cellelementsvertically.1.Itadjustselementslikeimagesorforminputswithintextlinesusingvalueslikebaseline,middle,super,andsub.2.Intablecells,itcontrolscontentalignmentwithtop,middle,orbottomvalues,oftenu

UseautomatedtoolslikePurgeCSSorUnCSStoscanandremoveunusedCSS;2.IntegratepurgingintoyourbuildprocessviaWebpack,Vite,orTailwind’scontentconfiguration;3.AuditCSSusagewithChromeDevToolsCoveragetabbeforepurgingtoavoidremovingneededstyles;4.Safelistdynamic

To change the text color in CSS, you need to use the color attribute; 1. Use the color attribute to set the text foreground color, supporting color names (such as red), hexadecimal codes (such as #ff0000), RGB values (such as rgb(255,0,0)), HSL values (such as hsl(0,100%,50%)), and RGBA or HSLA with transparency (such as rgba(255,0,0,0.5)); 2. You can apply colors to any element containing text, such as h1 to h6 titles, paragraph p, link a (note the color settings of different states of a:link, a:visited, a:hover, a:active), buttons, div, span, etc.; 3. Most

First, use JavaScript to obtain the user system preferences and locally stored theme settings, and initialize the page theme; 1. The HTML structure contains a button to trigger topic switching; 2. CSS uses: root to define bright theme variables, .dark-mode class defines dark theme variables, and applies these variables through var(); 3. JavaScript detects prefers-color-scheme and reads localStorage to determine the initial theme; 4. Switch the dark-mode class on the html element when clicking the button, and saves the current state to localStorage; 5. All color changes are accompanied by 0.3 seconds transition animation to enhance the user

Backdrop-filter is used to apply visual effects to the content behind the elements. 1. Use backdrop-filter:blur(10px) and other syntax to achieve the frosted glass effect; 2. Supports multiple filter functions such as blur, brightness, contrast, etc. and can be superimposed; 3. It is often used in glass card design, and it is necessary to ensure that the elements overlap with the background; 4. Modern browsers have good support, and @supports can be used to provide downgrade solutions; 5. Avoid excessive blur values and frequent redrawing to optimize performance. This attribute only takes effect when there is content behind the elements.

User agent stylesheets are the default CSS styles that browsers automatically apply to ensure that HTML elements that have not added custom styles are still basic readable. They affect the initial appearance of the page, but there are differences between browsers, which may lead to inconsistent display. Developers often solve this problem by resetting or standardizing styles. Use the Developer Tools' Compute or Style panel to view the default styles. Common coverage operations include clearing inner and outer margins, modifying link underscores, adjusting title sizes and unifying button styles. Understanding user agent styles can help improve cross-browser consistency and enable precise layout control.
