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中居中的方法有哪些的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

使用js或css如何实现瀑布流布局,几种方案介绍

jquery怎么取消css样式

css是一种语言吗

css设置table圆角边框不起作用是什么原因

css怎么改变文字颜色

css如何设置最小高度

css sprites是什么

css属性区分大小写吗

css怎么设置文字间距

详解css行高line-height属性

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




打赏

取消

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

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

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

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

评论

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

    暂无评论...