search
HomeWeb Front-endHTML Tutorial报告一个IE很奇葩的滚动条问题--百分比计算宽度为浮点数时的滚动条显示异常_html/css_WEB-ITnose

起因:

  做项目的时候做了一个表格内容超过DIV容器自动横向滚动处理。别的浏览器都正常;但是在IE下面明明表格table和容器DIV宽度一致但是却出现了滚动条。如图

  

  然后本人做实验找了半天原因终于是找到了,是IE浏览器使用百分比计算宽度值不是整数值引起的。

 

实例1:使用百分比计算结果是整数的情况下显示正常。

<!DOCTYPE html> <html> <head>   <meta charset='utf-8'>   <title>IE8奇葩滚动条问题实例</title>   <style type="text/css">     * {       -webkit-box-sizing: border-box;       -moz-box-sizing: border-box;       box-sizing: border-box;     }     .panel{       width:1000px;     }     .scroller{       overflow-x: auto;       width:75%;       background-color: #f00;     }     .inner{       width:750px;      height: 10px;       border: 1px solid #ddd;       background-color: #ff0;     }   </style> </head> <body>   <div class="panel">     <div class="scroller" style="height:35px;">         <div class="inner"></div>     </div>   </div> </body> </html>

  .scroller的宽度=.panel的宽度*75%;结果为750,是一个整数,和.inner的宽度相同。显示正常

  

实例2:使用百分比计算结果是非整数的情况下显示异常。

    .panel{       width:<strong>1001px</strong>;     }     .scroller{       overflow-x: auto;       width:75%;       background-color: #f00;     }     .inner{       width:<strong>751px</strong>;      height: 10px;       border: 1px solid #ddd;       background-color: #ff0;     } 

  设置.panel为1001px,那么.scroller的宽度=.panel的宽度*75%,结果为750.75px。但是实际上像素最小单位为1px,所以.scroller宽度为751px。而.inner也是751。按理说不应该出现横向滚动条,但是IE却出现了

  

  难道IE对.scroller设置是750px?带着这个疑问,将.inner设置成750px,然后将页面放大看到.scroller和.inner的边距差距明显,所以.scroller显示的宽度是751px没有错。见下图

  

  而且在IE8浏览器下测试的时候发现.scroller的高度也会影响滚动条是否显示。当.scroller的高度是34-的时候效果图如下

  

  而当.scroller高度是35+的时候会显示滚动条。

  不得不吐槽IE浏览器的奇葩。

  在IE9+上也会出现滚动条问题,不过有改进的是和.scroller的高度无关了。

  

  所以我们估计IE浏览器在判断滚动条显示与否的时候是拿计算的浮动值取比较,而非拿真实显示的值去比较的。

  那么想象一下,对于IE浏览器来说现在.scroller的计算宽度为750.75px,如果我们给.inner也设置成一个浮点数呢?

  测试结果是只要设置的任何浮点数都不会显示滚动条。

  

  所以,注意:在我们通过计算得到父容器的宽度来设置子标签的宽度为等宽的时候,直接设置父容器的浮点值宽度即可。这是目前兼容各个浏览器的比较便捷的方法。

 

  题外话:在浏览器窗口缩放(ctrl+鼠标滚轮)的时候,各个浏览器在父容器宽度是浮点数的时候都会出现滚动条,这个是缩放导致失真出现的问题,不可避免,也和该讨无关。

   

  如果觉得本文不错,请点击右下方【推荐】!

Statement
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
The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

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

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment