关于div的居中显示

让一个大的div在页面居中显示:

html代码如下:

<body>
        <div id="container"></div>
</body>

不完善的方法:

1、仅仅在IE浏览器中居中

<style type="text/css" media="screen">
            * {
                padding: 0px;
                margin: 0px;
            }

           /**            *仅仅在IE浏览器中居中           /
            body{
                text-align: center;
            }

            #container {
                width:1100px;
                height: 500px;
                border: 1px solid #229;

            }
   </style>

2、在IE浏览器外,其他的浏览器中居中

<style type="text/css" media="screen">
            * {
                padding: 0px;
                margin: 0px;
            }

            #container {
                width:1100px;
                height: 500px;
                border: 1px solid #229;
                margin: auto; /* 在IE浏览器外的浏览器,实现居中 */

            }
        </style>

2、在所有的浏览器中居中

<style type="text/css" media="screen">
            * {
                padding: 0px;
                margin: 0px;
            }

            #container {
                width:1100px;
                height: 500px;
                border: 1px solid #229;

                /**                 *下面三局代码,实现所有浏览器的居中                /
                position: absolute;
                left:50%;
                margin-left: -550px; /* #container宽度的一半*/

            }
        </style>
时间: 2024-08-03 14:26:06

关于div的居中显示的相关文章

设定div始终居中显示

<script type="text/javascript"> (function($){ $.fn.extend({ center:function(options){ //center插件 var options=$.extend({ //默认属性值 inside:window, //元素居于窗体中心 transition:0, //元素居中移动时的时间,单位是millisecond minX:0, //元素最小X方向距边值 minY:0, //元素最小Y方向距边值 w

怎样使一个div标签随着浏览器窗口的改变一直居中显示

在我们开发页面的过程中,经常会遇到让div居中在浏览器中显示,或使div标签在父级div中居中显示,下面就是解决方法1.在浏览器中居中显示 <style> .main{width:200px;height:200px;margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0;} </style> <div class="main"></div> 2.在父级

左边图标右边文字,在div里居中

图1 图2 css制作: 左边一个小图标,右边文字,两者之间一点间隔,再在一个div 里面居中显示.(div不设置宽度) 第一种做法: 图3 图4 .m-sn {    font-size: 12px;    color: #999;    background-color: #FFF;    line-height: 22px;    height: 22px;    padding-bottom: 24px;    text-align: center; // 这是最常用的} .m-sn s

图片大小不改动,根据屏幕大小自动把图片居中显示

background属性 background-image: url('../img/1_1.jpg'); <!-- 背景图片路径 --> background-repeat: no-repeat; <!-- 背景图片是否重复显示 --> background-position: center; <!-- 若背景图片小于div,则居中显示 --> background-attachment: fixed; <!-- 背景图片固定,不随scroll拖动而变动 --&

div中img依据不同分辨率居中显示,超出部分隐藏

在做banner居中时 碰到的问题,知道可以用背景图实现居中显示,但是内心是想深究下的,故找到几种办法收集一下,后面两种真的是奇技淫巧 来着下面两处 https://www.zhihu.com/question/39742237 https://www.v2ex.com/t/187544 flex position:absolute + negative margin background-image + background-size + background-position 4.父元素po

设置div中的div居中显示

设置div中的div居中显示 方法一. <div class='big'> <div class='small'>box1</div> </div> style样式: .big{ height:200px; width:200px; border:black solid 1px; position:absolute; left:150px; } .small{ height:100px; width:100px; background-color:green

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> DIV居中显示</title>

多个div居中显示

页面中有多个div时我们希望并排居中显示,可以通过在并排显示的div上一层再加一个div,设定宽度,然后让其居中显示达到需要的效果. 关键是要对外层div设定宽度. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"&g

css div居中显示的4种写法

Demo:http://www.feman.cn/h5/center.html 1.absolute 绝对定位 这是我们最常用的一种居中定位写法 要求必须确定div的宽高度 目前市面上的浏览器基本上都支持这种写法 <html lang="en"> <head> <meta charset="UTF-8"> <title>absolute居中定位</title> <style> *{margin: