css怎么让div垂直居中


当前第2页 返回上一页

1

2

3

4

5

6

7

8

9

.something-semantic {

   display: table;

   width: 100%;

}

.something-else-semantic {

   display: table-cell;

   text-align: center;

   vertical-align: middle;

}

方法三,终极解决方法:

以上2中方法可能都有其局限性,我介绍的第三中方法是比较成熟的不是固定高宽div的垂直居中的方法!但是方法是css3的写法,想兼容IE8的童鞋们,建议用上面的方法!

方法和我们固定高宽的差不多,但是不用margin我们用的是 translate()

demo如下:

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 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

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

    <title>haorooms不固定高度div写法</title>

 

    <style>

.center {

  position: fixed;

  top: 50%;

  left: 50%;

  background-color: #000;

  width:50%;

  height: 50%;

-webkit-transform: translateX(-50%) translateY(-50%);

}

 

 

    </style>

</head>

<body>

    <div></div>

</body>

</html>

我上面的css只是针对webkit内核的浏览器,其他内核浏览器写法如下:

1

2

3

4

-webkit-transform: translateX(-50%) translateY(-50%);

-moz-transform: translateX(-50%) translateY(-50%);

-ms-transform: translateX(-50%) translateY(-50%);

transform: translateX(-50%) translateY(-50%);

有些弹出层的样式,也可以用这个方法居中

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

position: fixed;

top: 50%;

left: 50%;

width: 50%;

max-width: 630px;

min-width: 320px;

height: auto;

z-index: 2000;

visibility: hidden;

-webkit-backface-visibility: hidden;

-moz-backface-visibility: hidden;

backface-visibility: hidden;

-webkit-transform: translateX(-50%) translateY(-50%);

-moz-transform: translateX(-50%) translateY(-50%);

-ms-transform: translateX(-50%) translateY(-50%);

transform: translateX(-50%) translateY(-50%);

(学习视频分享:css视频教程)

以上就是css怎么让div垂直居中的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

css text-outline属性怎么用

css基础语法之css的3种引入

css/html怎样让段落空出一行

如何在网页中引入外部js文件和css样式文件

前端怎么学

css可以干些什么

css font-stretch属性怎么用

css中可以实现旋转效果的属性是什么

css background-color属性怎么用?

css如何修改默认滚动条样式

更多相关阅读请进入《css》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...