search
HomeWeb Front-endHTML Tutorialdiv居中,但如果有竖着的滚动条,div会往左偏,怎么解决?_html/css_WEB-ITnose

本帖最后由 zlbpolly 于 2013-08-07 13:54:46 编辑

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; 就可以了

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 Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

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

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version