028-86922220
建站资讯

网站建设资讯

为你提供网站建设行业资讯、网站优化知识、主机域名邮箱、网站开发常见问题等。

CSS中怎么实现DIV容器水平居中

这篇文章主要介绍“CSS中怎么实现DIV容器水平居中”,在日常操作中,相信很多人在CSS中怎么实现DIV容器水平居中问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”CSS中怎么实现DIV容器水平居中”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

专注于为中小企业提供成都网站制作、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业革吉免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

DIV CSS教程:实现DIV容器水平居中的方法

在Web标准中的页面布局是使用DIV配合CSS来实现的。这其中最常用到的就是使整个页面水平居中的效果,这是在页面布局中基本,也是最应该首先掌握的知识。不过,还是经常会有人问到这个问题,在这里我简单总结一下使用DIV和CSS实现页面水平居中的方法:

一、margin:auto0与text-aligh:center

在现代浏览器(如InternetExplorer7、Firefox、Opera等)现代浏览器实现水平居中的方法很简单,只要设定到左右两侧的空白为自动即可。意即:

ExampleSourceCode

#wrap{margin:0auto;}

上面这段代码的意思是说使wrap这个DIV到左右两侧的距离自动设置,上下为0(可以为任意)。请在现代浏览器(如InternetExplorer7、Firefox、Opera等)中运行现在的代码:

SourceCodetoRun

   52CSS.comtitle> <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/> <styletypestyletype="text/css"> DIV#wrap{   width:760px;   margin:0auto;   border:1pxsolid#333;   background-color:#ccc;  }  style> head>  <body> <DIVidDIVid="wrap"></pre><p>在Firefox等现代浏览器设定页面元素的水平居中,只要指定margin:0auto;即可</p><pre><pre> DIV#wrap{   width:760px;   margin:0auto;/*这里的0可以任意值*/   border:1pxsolid#ccc;   background-color:#999;  }  pre> DIV> body> html></pre><p>[可先修改部分代码再运行查看效果]</p><p>上面的效果很好。但是这在InternetExplorer6及改正的版本中是不起作用的,不过幸好它有自己的解决办法。在InternetExplorer中text-align属性是可继承的,即在父元素中设置后在子元素中就默认具有了该属性。因此我们可以在body标签中设置text-align属性值为center,这样页面内所有的元素都会自动居中,同时我们还要加一个hook把页面中的文字变成我们习惯的阅读方式——居左对齐。因此我们要如此来写代码:</p><p>ExampleSourceCode</p><pre>body{text-align:center;}  #wrap{text-align:left;}</pre><p>这样在InternetExplorer中我们就轻松实现了DIV的居中对齐。因此要在所有的浏览器中显示居中的效果,我们就可以这样写我们的代码:</p><p>ExampleSourceCode</p><pre>body{text-align:center;}  #wrap{text-align:left;  margin:0auto;  }</pre><p>SourceCodetoRun</p><pre><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> <head> <title>52CSS.comtitle> <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/> <styletypestyletype="text/css"> body{text-align:center;}  DIV#wrap{   text-align:left;   width:760px;   margin:0auto;   border:1pxsolid#333;   background-color:#ccc;  }  style> head>  <body> <DIVidDIVid="wrap"></pre><p>在Firefox等现代浏览器设定页面元素的水平居中,只要指定margin:0auto;即可</p><pre><pre> DIV#wrap{   width:760px;   margin:0auto;/*这里的0可以任意值*/   border:1pxsolid#ccc;   background-color:#999;  }</pre><p>在InternetExplorer6及以下的版本中我们还要做以下的设置:</p><pre> body{text-align:center;}    DIV#wrap{   text-align:left;   }  pre> DIV> body> html></pre><p>[可先修改部分代码再运行查看效果]</p><p>不过这里有一个前提,就是设置居中的元素要有固定的宽度,比如这里我们设定了为760像素。</p><p><strong>二、相对定位与负的边距</strong></p><p>对于wrap进行相对定位,然后使用负的边距抵消偏移量。这种方法比较简单还很容易实现:</p><p>ExampleSourceCode</p><pre>#wrap{  position:relative;  width:760px;  left:50%;  margin-left:-380px  }</pre><p>这段代码的意思是,设置wrap的定位是相对于其父元素body标签的,然后将其左边框移动到页面的正中间(也就是left:50%含意);***我们再从中间位置向左偏移回一半的距离来,这样就实现了水平居中了。</p><p>SourceCodetoRun</p><pre><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> <head> <title>52CSS.comtitle> <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/> <styletypestyletype="text/css"> DIV#wrap{   position:relative;   width:760px;   left:50%;   margin-left:-380px;   border:1pxsolid#333;   background-color:#ccc;  }  style> head>  <body> <DIVidDIVid="wrap"></pre><p>在所有浏览器中都有效的方法:</p><pre><pre> DIV#wrap{   position:relative;   width:760px;   left:50%;   margin-left:-380px;   border:1pxsolid#333;   background-color:#ccc;  }  pre> DIV> body> html></pre><p>[可先修改部分代码再运行查看效果]<br/>同样,在设定水平居中前你需要设定一个固定的宽度。</p><p>◆究竟选择哪个方法?</p><p>上面两个方法究竟选择哪种方法好呢?在***种方法中貌似使用了Hack技术,其实并没有,它是中规中矩的Web标准写法,完全符合规范,因此,两个种方法中完全可以随便的选取其中的任一种进行使用,他们不存在CSShack的问题。</p><p><strong>三、其它的居中方式</strong></p><p>上面所说的都是设定了具体宽度的情况下水平居中的实现。有时候我们想做一个弹性布局,或者当一个元素处于一个容器中时我们只想让它居中并不想设定一个具体的宽度。其实这并不是真正的居中布局,就像对一个100%长度的元素来说,你说它是居中对齐还是居左对齐呢?所以所有不高宽度的居中都不是真正的居中。这样的设计我们是使用的像元素的padding来设置的,实际中我们是改变了父元素的容器大小:<br/>如我们希望wrap元素长度随窗口而改变,同时又维持居中,我们就可以这样写:</p><p>ExampleSourceCode</p><pre>body{  padding:10px150px;  }</pre><p>这里,我们只需要保持父元素左右两侧的填充是相等的就可以了。</p><p>SourceCodetoRun</p><pre><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> <head> <title>52CSS.comtitle> <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/> <styletypestyletype="text/css"> body{   padding:10px150px;  }  DIV#wrap{   border:1pxsolid#333;   background-color:#ccc;  }  style> head>  <body> <DIVidDIVid="wrap"></pre><p>一种随浏览器窗口大小而改变的具有弹性的居中布局:</p><pre><pre> body{   padding:10px150px;  }   这里,我们只需要保持父元素左右两侧的填充是相等的就可以了。  pre> DIV> body> html></pre><p>到此,关于“CSS中怎么实现DIV容器水平居中”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!</p>            
            
                        <br>
            当前题目:CSS中怎么实现DIV容器水平居中            <br>
            文章网址:<a href="http://whjierui.cn/article/ipcheh.html">http://whjierui.cn/article/ipcheh.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/soijho.html">西安建设网站制作的步骤专业做网站价格是多少</a>
                </li><li>
                    <a href="/article/soipgo.html">揭秘暗网经济腾飞的“加速器”</a>
                </li><li>
                    <a href="/article/soipgi.html">网站备案代理是什么,靠不靠谱?</a>
                </li><li>
                    <a href="/article/soijep.html">云环境下,如何对网络数据进行采集?</a>
                </li><li>
                    <a href="/article/soijgh.html">大数据风控将助推网贷中介平台转型</a>
                </li>        </ul>
    </div>
</div>
<footer>
    <div class="foot-top">
        <ul>
            <li>
                <div class="title">关于美图云海</div>
                <div class="tbox">
                    <div class="txt">
                        美图云海专注于网站建设、小程序开发,
                        <br /> 用心做好每一个网站,懂您所需、做您所想!
                        <br /> 我们比其他网络公司做的更好、做的更多,
                        <br /> 为客户创造更大的价值,让客户更省心!
                    </div>
                    <a rel="nofollow" href="javascript:;" class="more">MORE</a>
                </div>
            </li>
            <li>
                <div class="title">相关专题</div>
                <div class="tbox">
                    <a href="javascript:;" class="link">企业官网定制</a>
                    <a href="javascript:;" class="link">小程序开发</a>
                    <a href="javascript:;" class="link">品牌网站设计</a>
                    <a href="javascript:;" class="link">网站建设标签</a>
                    <a href="javascript:;" class="link">乐山网站建设</a>
                    <a href="javascript:;" class="link">高端网站设计</a>
                    <a href="javascript:;" class="link">公司做网站</a>
                </div>
            </li>
            <li>
                <div class="title">凭什么选择我们</div>
                <div class="tbox">
                    <a class="link">专业设计团队</a>
                    <a class="link">快速响应服务</a>
                    <a class="link">7个软件著作权</a>
                    <a class="link">已服务3000+客户</a>
                    <a class="link">项目检测具体全面</a>
                    <a class="link">技术研发能力强劲</a>
                    <a class="link">深度符合SEO优化</a>
                    <a class="link">15项设计奖项</a>
                    <a class="link">完善的制作流程</a>
                    <a class="link">售后服务让您省心</a>
                </div>
            </li>
            <li>
                <div class="title">网站设计案例</div>
                <div class="tbox">
                    <ul>
                        <li>
                            <a href="javascript:;" target="_blank">
                                <div class="img"><img src="/Public/Home/images/gebaili.jpg" alt="哥百利" />
                                </div>
                                <div class="tboxs">
                                    <div class="t1">哥百利</div>
                                    <div class="t2">家具研发、设计、生产、服务为一体的专业实木家具订做企业</div>
                                </div>
                            </a>
                        </li>
                        <li>
                            <a href="javascript:;" target="_blank">
                                <div class="img"><img src="/Public/Home/images/cdshujin.jpg" alt="蜀锦在线" /></div>
                                <div class="tboxs">
                                    <div class="t1">蜀锦在线</div>
                                    <div class="t2">汽车行业网站建设</div>
                                </div>
                            </a>
                        </li>
                    </ul>
                </div>
            </li>
        </ul>
    </div>
    <div class="foot-center">
        <ul>
            <li>
                <div class="f-ewm"><img alt="美图云海微信公众号" src="/Public/Home/images/ewm.jpg" /></div>
                <div class="tbox ewm">
                    <div class="t1">扫一扫关注</div>
                    <div class="t2">专业团队为您解答</div>
                </div>
            </li>
            <li>
                <div class="tbox tel">
                    <div class="t1">电话/邮箱</div>
                    <div class="t2">400-028-6601 / 028-86922220<br>631063699@qq.com</div>
                </div>
            </li>
            <li>
                <div class="tbox sz">
                    <div class="t1">成都(总部)</div>
                    <div class="t2">成华区 双林路22号仁禾商务楼5F<br> 大客户专线:13518219792
                    </div>
                </div>
            </li>
            <li>
                <div class="tbox gz">
                    <div class="t1">网站建设(乐山站)</div>
                    <div class="t2">
                        乐山市市中区瑞祥路一段1507号
                        <br /> 028-86922220
                    </div>
                </div>
            </li>
        </ul>
    </div>
    <div class="foot-button">
        <div class="link-box" style="width:100%;float:none;">
            <div class="a-box"></div>
            <div style="border-top:1px solid #ebebeb;font-size:12px;color:#666666;line-height:2;padding-top:20px;margin-top:20px;">
                业务范围包括企业网站建设、商城系统开发、品牌网站设计、旅游网站制作、英文外贸网站、教育培训门户网站开发、微信手机移动端开发、响应式网站建设、微信小程序开发、APP定制和其他类型网站定制等。
                <br>服务区域包括成都市锦江区、青羊区、武侯区、金牛区、成华区、龙泉驿、温江、新都、高新区、成都市以及全国各地接受异地服务商的公司企业或者机构。
                <br>
                <div class="a-box"><span><b>友情链接</b></span>
                    <a href="http://www.scxuyong.com/" title="叙永网站建设" target="_blank">叙永网站建设</a><a href="https://www.cdcxhl.com/mianfei/jianzhan/chengdu.html" title="成都免费建站" target="_blank">成都免费建站</a><a href="http://www.cdkjz.cn/fangan/" title="企业网站设计" target="_blank">企业网站设计</a><a href="http://www.zzjierui.cn/" title="广安网站建设" target="_blank">广安网站建设</a><a href="http://www.dmvi.cn/ser/huace/" title="成都宣传画册设计" target="_blank">成都宣传画册设计</a><a href="https://www.cdxwcx.com/wangzhan/app.html" title="移动APP" target="_blank">移动APP</a><a href="http://chengdu.cdcxhl.cn/qiye/" title="企业网站建设公司" target="_blank">企业网站建设公司</a><a href="http://www.ptruijie.cn/" title="新都网站建设" target="_blank">新都网站建设</a><a href="http://www.dgyishan.com/" title="酒柜书柜鞋柜定制" target="_blank">酒柜书柜鞋柜定制</a><a href="http://www.cqcxhl.com/" title="网站制作" target="_blank">网站制作</a>                </div>
            </div>
            <div class="copyright">©2025 青羊区美图云海设计工作室(个体工商户)乐山站   蜀ICP备19037934号</div>
        </div>
    </div>
</footer>
<div class="fixed-contact-wrap show">
    <ul class="item-list clearfix">
        <li class="phone">
            <a rel="nofollow" target="_blank" href="tel:028-86922220"><i
                    class="icon"></i><strong>028-86922220</strong></a>
        </li>
        <li class="qq">
            <a rel="nofollow" target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=244261566&site=qq&menu=yes"><i
                    class="icon"></i><strong> 244261566</strong></a>
        </li>
        <li class="back-top">
            <a href="#" rel="nofollow" class="back-to-top"><i class="icon"></i><strong> 回到顶部</strong></a>
        </li>
    </ul>
</div>
<script type="text/javascript">
    //右侧联系我们悬浮窗
    $(".fixed-contact-wrap").hover(function () {
        $(this).addClass("active");
    }, function () {
        $(this).removeClass("active");
    })
    function show_phone_menu() {
        $(".right-side ul").toggle();
    }
</script>

</body>
</html>

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