Table of Contents
回复讨论(解决方案)
Home Web Front-end HTML Tutorial div居中,但如果有竖着的滚动条,div会往左偏,怎么解决?_html/css_WEB-ITnose

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

Jun 21, 2016 am 09:45 AM

本帖最后由 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>
Copy after login

如果加一个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>
Copy after login


回复讨论(解决方案)

这个是浏览器的特性,你可以通过js来获取宽度
比如

//这个就是body的宽度,那么有滚动条和没有滚动条肯定是不一样的document.body.clientWidth //当然还有其他的属性就不列举了
Copy after login

唉,这样真不是爽,切换页面时,感觉导航左右晃。。。不太懂css,以为有解决办法呢

唉,这样真不是爽,切换页面时,感觉导航左右晃。。。不太懂css,以为有解决办法呢
这样的话可以使用绝对定位来布局,这样就不会晃了~

CSS中设置始终出现滚动条即可。

CSS中设置始终出现滚动条即可。

楼上正解。让右侧一直有滚动条,高度不够的时候就没有bar,相比起因为滚动条的出现与否导致的左右晃动来说,可能更能接受一些吧。

body{overflow-y:scroll;}
Copy after login

另外一种相反的思路是,一直不出现滚动条。然后自己去判断下面的div是不是需要滚动条,如果div的高度>浏览器(页面可见)高度 - 导航栏的高度,就设置overflow:auto。

这样的问题在于,在打开页面和手动改变浏览器大小的时候,都需要去判断。这个倒是可以通过body的onResize方法来捕捉

<body onResize="resizeRightContent();">
Copy after login

但是还是太麻烦了,有点画蛇添足的感觉,还是让滚动条一直出现吧。。。

设置div的样式为:style="margin:auto; 就可以了

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