css设置元素在页面居中的的各种方式

方法一:margin + width

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<!Doctype html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>

<title>css布局水平居中margin + width</title>

<style type="text/css">

*{margin:0;padding:0;}

.box{

width:100px;

margin:0 auto;

background:red;

}

</style>

</head>

<body>

<div class="box">Demo</div>

</body>

</html>

       这个水平居中方法,我们最熟悉了,也是最常用的,width可以固定px也可以使用百分比

方法二:table + margin

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<!Doctype html>

<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>

    <title>css布局水平居中table + margin</title>

    <style type="text/css">

        *{margin:0;padding:0;}

        .box{

            display: table;

            margin: 0 auto;

            background:red;

        }

    </style>

</head>

<body>

    <div class="box">Demo</div>

</body>

</html>

       说明:display: table在表现上类似block元素,但是宽度为内容宽。无需设置父元素样式 (支持 IE 8 及其以上版本)兼容 IE 8 一下版本需要调整为<table>

方法三:inline-block + text-align

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<!Doctype html>

<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>

    <title>css布局水平居中inline-block + text-align</title>

    <style type="text/css">

        *{margin:0;padding:0;}

        .content{

            text-align:center;

        }

        .box{

            display:inline-block;

            background: red;

        }

    </style>

</head>

<body>

<div class="content">

     <div class="box">Demo</div>

</div>

</body>

</html>

       说明:兼容性佳(甚至可以兼容 IE 6 和 IE 7)

方法四:absolute + margin-left

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<!Doctype html>

<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>

    <title>css布局水平居中absolute + margin-left</title>

    <style type="text/css">

        *{margin:0;padding:0;}

        .content{

            position: relative;

        }

        .box{

            position: absolute;

            left: 50%;

            width: 100px;

            margin-left: -50px;

            background: red;

        }

    </style>

</head>

<body>

<div class="content">

     <div class="box">Demo</div>

</div>

</body>

</html>

       说明:宽度固定相比于使用transform ,有兼容性更好

方法五:absolute + transform

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<!Doctype html>

<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>

    <title>css布局水平居中absolute + transform</title>

    <style type="text/css">

        *{margin:0;padding:0;}

        .content{

            position: relative;

        }

        .box{

            position: absolute;

            left: 50%;

            transform: translateX(-50%);

            background: red;

        }

    </style>

</head>

<body>

<div class="content">

     <div class="box">Demo</div>

</div>

</body>

</html>

        说明:绝对定位脱离文档流,不会对后续元素的布局造成影响。transform为 CSS3 属性,有兼容性问题

方法六:flex + justify-content

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<!Doctype html>

<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>

    <title>css布局水平居中flex + justify-content</title>

    <style type="text/css">

        *{margin:0;padding:0;}

        .content{

            display: flex;

            justify-content: center;

        }

        .box{

            width: 100px;

            background: red;

        }

    </style>

</head>

<body>

<div class="content">

    <div class="box">Demo</div>

</div>

</body>

</html>

       说明:只需设置父节点属性,无需设置子元素flex有兼容性问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值