Home Web Front-end HTML Tutorial CSS中隐藏滚动条的简单实现方法_html/css_WEB-ITnose

CSS中隐藏滚动条的简单实现方法_html/css_WEB-ITnose

Jun 24, 2016 am 11:33 AM

xhtml中隐藏滚动条

在用ie6浏览有框架的xhtml页面的时候,默认会水平和垂直滚动条会一起出现,这是ie6的一个

bug,在firefox上是正常的,出现的原因是其对XHTML 1.0 transitional doctype的解释缺陷.

对于这个bug一般有3种解决方案,

方法1:

代码:

程序代码

html { overflow-y: scroll; }

原理:强制显示ie的垂直滚动条,而忽略水平滚动条

优点:完全解决了这个问题, 允许你保持完整的XHTML doctype.

缺点:即使页面不需要垂直滚动条的时候也会出现垂直滚动条。

方法2:(推荐采用)

代码:

程序代码

html { overflow-x: hidden; overflow-y: auto; }

原理:隐藏横向滚动,垂直滚动根据内容自适应

优点:在视觉上解决了这个问题.在不必要的时候, 未强制垂直滚动条出现.

缺点:只是隐藏了水平滚动条,如果页面真正需要水平滚动条的时候,

屏幕以外的内容会因为用户无法水平滚动,而看不到。

方法3:

代码:

程序代码

body { margin-right: -15px; margin-bottom: -15px; }

原理:这会在margin的水平和垂直方向上添加一个负值, IE添加了该精确数值后, 便会去除对滚动条的需求假象.

优点:在视觉上解决了这个问题.,垂直滚动根据内容自适应

缺点:由于"人为创建"了15px的外边距(margin), 所以无法使用该填充过的屏幕区域.

------------------------------------

设置样式

在原来的html的时候,我们可以这样定义整个页面的滚动条

程序代码

body{scrollbar-3dlight-color:#D4D0C8; /*- 最外左 -*/scrollbar-highlight-color:#fff; /*- 左二 -*/scrollbar-face-color:#E4E4E4; /*- 面子 -*/scrollbar-arrow-color:#666; /*- 箭头 -*/scrollbar-shadow-color:#808080; /*- 右二 -*/scrollbar-darkshadow-color:#D7DCE0; /*- 右一 -*/scrollbar-base-color:#D7DCE0; /*- 基色 -*/scrollbar-track-color:#;/*- 滑道 -*/}
Copy after login

  

但是同样的代码,我们应用在 xhtml下就不起作用了,我相信好多好朋友也遇到过同样的问题

那么怎么才能在xhtml下应用滚动条样式呢?看下列代码

程序代码

html{scrollbar-3dlight-color:#D4D0C8; /*- 最外左 -*/scrollbar-highlight-color:#fff; /*- 左二 -*/scrollbar-face-color:#E4E4E4; /*- 面子 -*/scrollbar-arrow-color:#666; /*- 箭头 -*/scrollbar-shadow-color:#808080; /*- 右二 -*/scrollbar-darkshadow-color:#D7DCE0; /*- 右一 -*/scrollbar-base-color:#D7DCE0; /*- 基色 -*/scrollbar-track-color:#;/*- 滑道 -*/}
Copy after login

  

这段代码和上一段唯一的不同就是在css定义的元素上,一个是body一个是html。我们再测试一下,把html页面的

"body"修改成"html"测试一下,发现依然可以实现效果。那到底是为什么呢?

从字面上来看,xhtml比html多一个x,那么这个x其实也就是xml,为什么要加一个xml在里面?其实最根本的原因就是要让html更加结构化标准化(因为html实在是太烂)。

我们在html里面定义的是body,因为html不是很标准所以这样可以生效,而在xhtml里面这样就不行了,

我看看那个图很明显,body标签本身不是根元素,只有html才是根元素,而页面的滚动条也是属于根元素的,所以这就是我们为什么定义body没有效果的原因,因为我们定义的只是一个子原素。ok,我们知道了原理,来做一个试验如果把定义"body"或"xhtml"换成"*",

程序代码

*{scrollbar-3dlight-color:#D4D0C8; /*- 最外左 -*/scrollbar-highlight-color:#fff; /*- 左二 -*/scrollbar-face-color:#E4E4E4; /*- 面子 -*/scrollbar-arrow-color:#666; /*- 箭头 -*/scrollbar-shadow-color:#808080; /*- 右二 -*/scrollbar-darkshadow-color:#D7DCE0; /*- 右一 -*/scrollbar-base-color:#D7DCE0; /*- 基色 -*/scrollbar-track-color:#;/*- 滑道 -*/}
Copy after login

  

在html和xhtml都通过,因为*就是定义页面上的任何标签当然也包括了“html”这个标签。

(ps:其实与其说是html与xhtml的区别到不如说是有无XHTML 1.0 transitional doctype的区别,但是如果你把页面的XHTML 1.0 transitional doctype去掉的话,那么这个页面就没有doctype,默认的显示方式就是html4.01,不过你要把XHTML 1.0 transitional doctype修改成HTML 4.01 doctype同样页面定义body也不会有效果的,虽然这个页面的标准是html 4.01)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles