css里上下居中怎么弄?


当前第2页 返回上一页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<style>

    #father {

        width: 500px;

        height: 300px;

        background-color: skyblue;

        position: relative;

}

  

    #son {

        height: 100px;

        background-color: green;

        position: absolute;

        top: 50%;

        margin-top: -50px;

}

</style>

  

<div id="father">

    <div id="son">我是块级元素</div>

</div>

不定高度:利用css3新增属性transform: translateY(-50%);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<style>

    #father {

        width: 500px;

        height: 300px;

        background-color: skyblue;

        position: relative;

}

  

    #son {

        width: 100px;

        background-color: green;

        position: absolute;

        left: 50%;

        transform: translateY(-50%);

}

</style>

  

<div id="father">

    <div id="son">我是块级元素</div>

</div>

效果:

1556519576485117.jpg

方案二:使用flexbox布局实现(高度定不定都可以)

使用flexbox布局,只需要给待处理的块状元素的父元素添加属性 display: flex; align-items: center;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<style>

    #father {

        width: 500px;

        height: 300px;

        background-color: skyblue;

        display: flex;

        align-items: center;

    }

  

    #son {

        width: 100px;

        height: 100px;

        background-color: green;

    }

</style>

  

<div id="father">

    <div id="son">我是块级元素</div>

</div>

效果:

1556519592845736.jpg

相关教程:CSS视频教程

以上就是css里上下居中怎么弄?的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

css outline属性怎么用?

clevercss是什么?

css怎么设置鼠标手势

css什么时候用class和id

css如何实现底部tapbar栏效果

编写css文件要写head吗

css animation-duration属性怎么用

css中什么是块元素

详解纯css实现文字渐变色的两种方式

css中实现背景透明的三种方式

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




打赏

取消

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

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

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

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

评论

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