资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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>
            URL地址:<a href="http://cdkjz.cn/article/ipcheh.html">http://cdkjz.cn/article/ipcheh.html</a>
        </div>
        <div class="g-return-wrapper clearfix">
            <a href="http://www.cdkjz.cn/" class="home">返回首页</a>
            <a href="http://www.cdkjz.cn/news/" class="column">了解更多建站资讯</a>
        </div>
    </div>
</div>
<div class="full-related-news">
    <h3 class="related-title">相关资讯</h3>
    <div class="related-news weblg">
        <ul class="clearfix">
            <li>
                    <a href="/article/ijccp.html">
                        <h2 class="title">利用Python如何将视频转换为音频-创新互联</h2>
                    </a>
                </li><li>
                    <a href="/article/ijcej.html">
                        <h2 class="title">DIV+CSS命名规范有哪些-创新互联</h2>
                    </a>
                </li><li>
                    <a href="/article/ijcsj.html">
                        <h2 class="title">微信小程序switch开关选择器如何用-创新互联</h2>
                    </a>
                </li><li>
                    <a href="/article/ijcip.html">
                        <h2 class="title">jQuery基础操作有哪些-创新互联</h2>
                    </a>
                </li><li>
                    <a href="/article/ijcic.html">
                        <h2 class="title">H5中的服务器推送事件是什么-创新互联</h2>
                    </a>
                </li><li>
                    <a href="/article/ijcce.html">
                        <h2 class="title">微服务设计关键的难点:微服务架构的数据库是如何设计的?-创新互联</h2>
                    </a>
                </li><li>
                    <a href="/article/ijcii.html">
                        <h2 class="title">Centos/Linux下如何调整分区大小-创新互联</h2>
                    </a>
                </li><li>
                    <a href="/article/ijcgj.html">
                        <h2 class="title">WordPress建站流程是什么-创新互联</h2>
                    </a>
                </li>        </ul>
    </div>
</div>
<div class="full-icontact-cover m-ft-contact">
    <div class="weblg">
        <div class="clearfix content">
            <div class="motto">
                多年建站经验
            </div>
            <div class="info">
                <h3>多一份参考,总有益处</h3>
                <h2> 联系快上网,免费获得专属《策划方案》及报价</h2>
                <div class="msg">
                    <p>咨询相关问题或预约面谈,可以通过以下方式与我们联系</p>
                    <h4>业务热线:<a href="tel:400-028-6601" rel="nofollow">400-028-6601</a> / 大客户专线   成都:<a
                            href="tel:+13518219792" rel="nofollow">13518219792</a>
                          座机:<a href="tel:02886922220" rel="nofollow">028-86922220</a>
                    </h4>
                </div>
            </div>
        </div>
        <div class="btns clearfix">
            <a href="https://wpa.qq.com/msgrd?v=3&uin=631063699&site=qq&menu=yes" target="_blank" rel="nofollow"
               class="oline">在线咨询</a>
            <a href="javascript:;" class="edit" rel="nofollow">提交需求</a>
        </div>
    </div>
</div>
<div class="footer-content">
    <div class="weblg clearfix">
        <div class="friend-links">
            <h6 class="clearfix">
                <span class="tilte">友情链接</span>
                <a class="exchagne" href="http://wpa.qq.com/msgrd?v=3&uin=631063699&site=qq&menu=yes">交换友情链接</a>
            </h6>
            <div class="link-list clearfix">
                <div class="link-slider">
                    <a href="http://www.cdxwcx.cn/tuoguan/" title="服务器托管" target="_blank">服务器托管</a><a href="http://www.huaxiangcd.com/" title="成都花箱" target="_blank">成都花箱</a><a href="https://www.cdcxhl.com/security/" title="等级保护测评" target="_blank">等级保护测评</a><a href="http://www.cdkjz.cn/fangan/energy/" title="节能环保网站建设方案" target="_blank">节能环保网站建设方案</a><a href="http://www.cdhuace.com/huace.html" title="成都画册制作" target="_blank">成都画册制作</a><a href="http://www.ghwzsj.com/" title="广东帝美豪门窗" target="_blank">广东帝美豪门窗</a><a href="https://www.cdxwcx.com/jifang/leshan.html" title="乐山服务器托管" target="_blank">乐山服务器托管</a><a href="http://www.dzzwz.com/" title="大橙子网站建设" target="_blank">大橙子网站建设</a><a href="https://www.cdxwcx.com/" title="网站建设" target="_blank">网站建设</a><a href="https://www.cdxwcx.com/" title="网站制作" target="_blank">网站制作</a>                </div>
            </div>
        </div>
    </div>
    <div class="full-foot-bottom">
        <div class="weblg clearfix">
            <p>成都网站建设公司地址:成都市青羊区太升南路288号锦天国际A座10层 建设咨询<a href="tel:400-028-6601">400-028-6601</a></p>
            <p>
                成都快上网科技有限公司-四川网站建设设计公司 | <a href="http://www.miitbeian.gov.cn/" target="_blank" rel="nofollow">蜀ICP备19037934号</a> Copyright 2020,ALL Rights Reserved cdkjz.cn | <a href="http://www.cdkjz.cn/" target="_blank">成都网站建设</a> | © Copyright 2020版权所有.</p>
            <p>专家团队为您提供<a href="http://www.cdkjz.cn/" target="_blank">成都网站建设</a>,<a href="http://www.cdkjz.cn/" target="_blank">成都网站设计</a>,成都品牌网站设计,成都营销型网站制作等服务,成都建网站就找快上网! | 成都网站建设哪家好? | <a href="###">网站建设地图</a></p>
        </div>
    </div>

</div>
<script type="text/javascript" src="../js/idangerous.swiper.min.js"></script>
<script type="text/javascript" src="../js/wow.min.js"></script>
<script type="text/javascript" src="../js/jquery.mousewheel.min.js"></script>
<script type="text/javascript" src="../js/jquery.placeholder.min.js"></script>
<script type="text/javascript" src="../js/layout.js"></script>
</body>
</html>
<script>
    $(".singlepage 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>