DIV居中布局的三大实现方法分别是什么

这篇文章给大家介绍DIV居中布局的三大实现方法分别是什么,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

十多年的调兵山网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整调兵山建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“调兵山网站设计”,“调兵山网站推广”以来,每个客户项目都认真落实执行。

你对DIV居中布局的方法是否了解,这里和大家分享一下,因为现在DIV+CSS布局越来越流行了,怎么实现DIV居中好像是个难题。

关于DIV居中布局的几种实现方法

现在DIV+CSS布局越来越流行了,怎么实现DIV居中好像是个难题。本文给出关于DIV居中布局的几种实现方法,相信会对大家有所帮助。

***种方法:

body{  margin:0pxauto;  text-align:center;  }

但是如果没申明下面这句解析方法,页面就会出错.不能居中对齐!

"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

但是有时候页面并不能全部按上面规定的代码格式来编写,比如说要改多彩滚动条.

第二种方法: 

 margin-left:50%;  left:-width/2;

这里的width不是CSS中的Width,而是你的DIV的宽度例如你的div是768px宽,那么你就应该设置left:-384px。

第三种方法:使用JS来控制DIV居中.

在页面之中添加了下面的一小段代码.

script>

function.js内容:

if(window.screen.width>800){document.write(" body{margin-left:"+(window.screen.width-800)/2+"px}style>");}

经过测试.可以正常居中

补充一点:上面这段JS必须放在你的***一个CSS连接或的后面.

◆下面是DIV居中这种方法的详细讲解:

首先解释一下JS中窗口和网页的几种尺寸属性的含义

document.body.clientWidth(网页可见区域宽):是指浏览器显示网页的区域宽度,不包括浏览器的边框宽度和垂直滚动条的宽度。大小随着浏览器的窗口大小而改变。

document.body.clientHeight(网页可见区域高):是指浏览器显示网页的区域所能看到的高度,不包括浏览器的边框宽度和水平滚动条的高度。大小随着浏览器的窗口大小而改变。

document.body.scrollTop(网页被卷去的高):是指拉动垂直滚动条时网页上面被地址栏及菜单栏遮盖着的部分的高。

document.body.scrollLeft(网页被卷去的左):是指拉动水平滚动条时网页左面被左边线遮盖着的部分的宽。

现在我们来分析一下程序该如何实现DIV居中:

◆***步我们要实现的是使层在弹出时绝对居中不去考虑是否有滚动条的情况。

1.计算出层距离显示区域左边和上边的位置

注意:divId指的是所要居中的层,divId.clientWidth为其宽度!@
vardivId=document.getElementById("xxx");
varv_left=(document.body.clientWidth-divId.clientWidth)/2;
varv_top=(document.body.clientHeight-divId.clientHeight)/2;

2.把得到的值重新赋给DIV的left和top属性

divId.style.left=v_left;
divId.style.top=v_top;
说明:divId为DIV标签的id值
这样这个层就是居中显示的了。

◆第二步我们要实现的是使在拖动滚动条的情况下弹出的层也能居中。

其实很简单我们只要把拖动的宽度和高度加到前面计算出来的左边距和上边距中就OK了。
v_left+=document.body.scrollLeft;
v_top+=document.body.scrollTop;

完整代码如下:

>   弹出的层居中显示title> <metanamemetaname="generator"content="editplus"> <metanamemetaname="author"content=""> <metanamemetaname="keywords"content=""> <metanamemetaname="description"content="">  head>  <body> <tablewidthtablewidth=100%> <tr><td><spanonclickspanonclick="divcenter()">testspan>td>tr> <tr><td><spanonclickspanonclick="divcenter()">testspan>td>tr> <tr><td><spanonclickspanonclick="divcenter()">testspan>td>tr> <tr><td><spanonclickspanonclick="divcenter()">testspan>td>tr> <tr><td><spanonclickspanonclick="divcenter()">testspan>td>tr> <tr><td><spanonclickspanonclick="divcenter()">testspan>td>tr> table> <dividdivid=mxhstyle="position:absolute;left:200px;top:30px;width:200px;height:200px;  background-color:navy;border:2px">我是居中显示的了。div> body> html></pre><p>主要是这句代码起的作用:(window.screen.width-800)/2//计算页面应该留出的边距数值.800为我的DIV宽度+滚动条宽度.实际应用改为你自己的大小.</p><p>关于DIV居中布局的三大实现方法分别是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。</p>            
            
                        <br>
            新闻标题:DIV居中布局的三大实现方法分别是什么            <br>
            文章分享:<a href="http://bzwzjz.com/article/ppsegp.html">http://bzwzjz.com/article/ppsegp.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/ddphoed.html">两个php数据传递参数 php传值到另一个php</a>
                </li><li>
                    <a href="/article/ddphogs.html">java四维数组代码 java四维数组创建</a>
                </li><li>
                    <a href="/article/ddphohc.html">设计师如何用智能聊天的简单介绍</a>
                </li><li>
                    <a href="/article/ddphopg.html">关于sap系统与nc的信息</a>
                </li><li>
                    <a href="/article/ddphohh.html">安卓表格布局java代码 安卓实现表格</a>
                </li>        </ul>
    </div>
</div>
<div class="footer2">
    Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号 <br />友情链接:
    <a href="http://m.cdcxhl.cn/qiye/" target="_blank">成都网站建设公司 </a><a href="http://www.cxjianzhan.com/mobile/" target="_blank">企业手机网站建设 </a><a href="http://m.cdcxhl.com/" target="_blank">网站建设推广 </a><a href="http://www.cqcxhl.com/service/ds.html" target="_blank">重庆电商网站建设 </a><a href="http://www.cxjianzhan.cn/fwxm/pinpai.html" target="_blank">成都品牌网站设计 </a><a href="http://www.cxhljz.com/" target="_blank">成都做网站建设公司 </a><a href="http://www.cxhljz.cn/" target="_blank">成都网站建设 </a><a href="http://www.cdwuji.cn/" target="_blank">成都网站建设 </a><a href="http://chengdu.cdcxhl.cn/H5/" target="_blank">响应式网站设计 </a><a href="http://chengdu.cdcxhl.cn/dingzhi/" target="_blank">成都定制网站建设 </a><a href="http://www.cxjianzhan.cn/" target="_blank">网站建设开发 </a><a href="http://www.kswcd.com/service/" target="_blank">高端网站建设 </a><a href="http://www.cxjianzhan.com/baojia/" target="_blank">网站建设费用 </a><a href="http://m.cdcxhl.cn/H5/" target="_blank">响应式网站建设 </a><a href="http://chengdu.cdxwcx.cn/" target="_blank">成都网站建设 </a><a href="http://www.cxhljz.com/" target="_blank">网站设计制作 </a><a href="http://www.cxjianzhan.com/" target="_blank">网站制作公司 </a><a href="http://www.kswjz.cn/" target="_blank">成都网站建设 </a><a href="http://www.cqcxhl.com/service/foreigntrade.html" target="_blank">重庆外贸网站建设 </a><a href="http://www.bzwzjz.com/" target="_blank">专业网站设计 </a><a href="http://chengdu.xwcx.net/mobile/" target="_blank">移动手机网站制作 </a><a href="http://www.cxhljz.cn/" target="_blank">成都网站制作 </a></div>

</body>
</html>
<script src="/Public/Home/js/wow.min.js"></script>
<script>
    if (!(/msie [6|7|8|9]/i.test(navigator.userAgent))) {
        new WOW().init();
    };
</script>
<div class="sidebar">
    <ul>
        <li><a href="http://wpa.qq.com/msgrd?v=3&uin=244261566&site=www.bzwzjz.com&menu=yes" target="_blank"><img src="/Public/Home/images/right_qq.png" /></a></li>
        <li><a href="http://wpa.qq.com/msgrd?v=3&uin=1683211881&site=www.bzwzjz.com&menu=yes" target="_blank"><img src="/Public/Home/images/qq.png" /></a></li>
        <li class="tel"><a href="tel:028-86922220"><img src="/Public/Home/images/right_tel.png" /></a></li>
        <div class="wx">
            <span class="weixin"><img src="/Public/Home/images/weixin.jpg"><br> 微信扫一扫在线咨询</span>
        </div>
        <li><a class="fx" href="#hero"><img src="/Public/Home/images/right_up.png" /></a></li>
    </ul>
</div>
<script type="text/javascript">
    $(function () {
        $('.sidebar .fx').click(function () {
            $('html,body').animate({
                scrollTop: '0px'
            }, 800);
        });
    });
</script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#fancybox-manual-b").click(function () {
            $.fancybox.open({
                href: 'map.html',
                type: 'iframe',
                padding: 5
            });
        })
    });
</script>
<script>
    $(".con img").each(function(){
        var src = $(this).attr("src");    //获取图片地址
        var str=new RegExp("http");
        var result=str.test(src);
        if(result==false){
            var url = "https://www.cdcxhl.com"+src;    //绝对路径
            $(this).attr("src",url);
        }
    });
    window.onload=function(){
        document.oncontextmenu=function(){
            return false;
        }
    }
</script>