css实现图片在div中居中的方法有哪些


本文摘自PHP中文网,作者V,侵删。

本文为大家介绍了图片在div中居中的四种方法,希望对大家有所帮助。

方法一:

(推荐教程:css视频教程)

html:

1

2

3

4

5

6

<div class="title">

          <div class="flag"></div>

          <div class="content">

              <img src="img_p1_title.png">

          </div>

      </div>

css

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

.title {

   width: 100%;

    font-size: 0;

    height: 10%;

}

.title .content img {

    width: 35%;

}

/*--主要的--*/

.content{

    display: inline-block;

    vertical-align: middle;

  }

.flag{

    display: inline-block;

    vertical-align: middle;

    height: 100%;

    width: 0;

  }

方法二:

html

1

2

<div class="title">

<img src="img_p1_title.png">

css

1

2

3

4

5

6

7

8

9

.title {

  display: flex;

  justify-content: center;

  align-items: center;

}

 

.title img {

  width: 35%;

}//该方法有些旧系统不支持

方法三:

html

1

2

<div class="title">

<span>第三种方法</span>

css

1

2

3

4

5

6

7

8

9

10

11

12

.title {

   height: 15%;

   font-size: 18px;

   position: relative;

}

 

.title span {

    position: absolute;

    top: 50%;

    left: 50%;

    transform: translate(-50%, -50%);

}

方法四:

html

1

2

<div class="title">

<span>第四种方法</span>

css

1

2

3

4

5

6

7

.title {

    width: 200px;

    height: 200px;

    vertical-align: middle;

    display: table-cell;

    text-align: center;

}

相关推荐:CSS教程

以上就是css实现图片在div中居中的方法有哪些的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

css如何控制输出字数

css中内容过长怎么解决

dreamweaver网页制作使用css样式嵌套方法

css中的animation是什么

css什么是圣杯布局?

css的rem是什么

css text-wrap属性怎么用

css怎么给按钮加图标

vue中如何集成css框架?方法介绍

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

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




打赏

取消

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

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

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

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

评论

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