div居中,但如果有竖着的滚动条,div会往左偏,怎么解决?_html/css_WEB-ITnose
div居中 滚动条 偏
div居中,如果网页内容比较长,出现竖滚动条时,居中的div会受滚动条影响,往左边偏 ?怎么解决呢?原本是这样
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head> <title>Untitled Page</title> <style>html,body,div,h1,h2,h3,h4,h5,h6,ul,ol,li,dl,dt,dd,p,blockquote,pre,form,input,textarea,fieldset,table,th,td{margin:0;padding:0}body,button,input,select,textarea{font:12px/1.6 Tahoma,Verdana,Arial,"\5b8b\4f53"}body{background:#fff;color:#7d7d7d;min-width:960px}ul,ol{list-style:none}img{border:0;vertical-align:top}:focus{outline:0}.doc-hd,.doc-bd{width:960px;margin:0 auto}.doc-bd{padding-bottom:20px}.logo{background:url(http://p2.qhimg.com/t011554d7f1d248cbd5.png) no-repeat}.main{width:623px;float:left}.side{width:318px;float:right;border:1px solid #f0f0f0;border-top:0}.bd{padding:10px 15px}.doc-hd{height:52px;position:relative;z-index:999}.logo{width:222px;height:46px;float:left;margin-top:4px;margin-left:-7px}.info{float:left;margin-top:8px}.info a{color:#7d7d7d;font-size:12px}.info a:hover{color:#20a135}.info img{padding-top:4px}.nav{float:right;font:16px/25px "\5fae\8f6f\96c5\9ed1";position:absolute;right:0;top:16px}.nav li{float:left;margin-left:38px}.nav a{height:34px;color:#555;float:left;cursor:pointer;position:relative}.nav a:hover,.nav .cur{color:#20a135}.nav .hot,.nav .new{background-position:-24px -144px;width:23px;height:14px;display:block;position:absolute;right:-24px;top:-2px;*top:-3px}.nav .new{background-position:0 -144px}</style></head><body><div class="doc-hd"> <a target="_self" title="logo" class="logo" href=""></a> <ul class="nav"> <li><a target="_self" class="cur" href="#">首页</a></li> <li><a>产品列表</a></li> <li><a>公司介绍</a></li> <li><a href="#">产品展示</a></li> <li><a>个人服务</a></li> <li><a>企业服务</a></li> <li><a href="#">游戏</a></li> <li><a href="#">论坛</a></li> </ul></div></body></html>
如果加一个div出现滚动条,居中的导航条就往左偏了
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head> <title>Untitled Page</title> <style>html,body,div,h1,h2,h3,h4,h5,h6,ul,ol,li,dl,dt,dd,p,blockquote,pre,form,input,textarea,fieldset,table,th,td{margin:0;padding:0}body,button,input,select,textarea{font:12px/1.6 Tahoma,Verdana,Arial,"\5b8b\4f53"}body{background:#fff;color:#7d7d7d;min-width:960px}ul,ol{list-style:none}img{border:0;vertical-align:top}:focus{outline:0}.doc-hd,.doc-bd{width:960px;margin:0 auto}.doc-bd{padding-bottom:20px}.logo{background:url(http://p2.qhimg.com/t011554d7f1d248cbd5.png) no-repeat}.main{width:623px;float:left}.side{width:318px;float:right;border:1px solid #f0f0f0;border-top:0}.bd{padding:10px 15px}.doc-hd{height:52px;position:relative;z-index:999}.logo{width:222px;height:46px;float:left;margin-top:4px;margin-left:-7px}.info{float:left;margin-top:8px}.info a{color:#7d7d7d;font-size:12px}.info a:hover{color:#20a135}.info img{padding-top:4px}.nav{float:right;font:16px/25px "\5fae\8f6f\96c5\9ed1";position:absolute;right:0;top:16px}.nav li{float:left;margin-left:38px}.nav a{height:34px;color:#555;float:left;cursor:pointer;position:relative}.nav a:hover,.nav .cur{color:#20a135}.nav .hot,.nav .new{background-position:-24px -144px;width:23px;height:14px;display:block;position:absolute;right:-24px;top:-2px;*top:-3px}.nav .new{background-position:0 -144px}</style></head><body><div class="doc-hd"> <a target="_self" title="logo" class="logo" href=""></a> <ul class="nav"> <li><a target="_self" class="cur" href="#">首页</a></li> <li><a>产品列表</a></li> <li><a>公司介绍</a></li> <li><a href="#">产品展示</a></li> <li><a>个人服务</a></li> <li><a>企业服务</a></li> <li><a href="#">游戏</a></li> <li><a href="#">论坛</a></li> </ul></div><div style="height:1000px;border:1px solid #ff0">ddd</div></body></html>
回复讨论(解决方案)
这个是浏览器的特性,你可以通过js来获取宽度
比如
//这个就是body的宽度,那么有滚动条和没有滚动条肯定是不一样的document.body.clientWidth //当然还有其他的属性就不列举了
唉,这样真不是爽,切换页面时,感觉导航左右晃。。。不太懂css,以为有解决办法呢
唉,这样真不是爽,切换页面时,感觉导航左右晃。。。不太懂css,以为有解决办法呢
这样的话可以使用绝对定位来布局,这样就不会晃了~
CSS中设置始终出现滚动条即可。
CSS中设置始终出现滚动条即可。
楼上正解。让右侧一直有滚动条,高度不够的时候就没有bar,相比起因为滚动条的出现与否导致的左右晃动来说,可能更能接受一些吧。
body{overflow-y:scroll;}
另外一种相反的思路是,一直不出现滚动条。然后自己去判断下面的div是不是需要滚动条,如果div的高度>浏览器(页面可见)高度 - 导航栏的高度,就设置overflow:auto。
这样的问题在于,在打开页面和手动改变浏览器大小的时候,都需要去判断。这个倒是可以通过body的onResize方法来捕捉
<body onResize="resizeRightContent();">
但是还是太麻烦了,有点画蛇添足的感觉,还是让滚动条一直出现吧。。。
设置div的样式为:style="margin:auto; 就可以了

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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)

Hot Topics



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

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

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

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

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

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.

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

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