700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > html用css让div居中显示图片 css3怎么使div图片居中显示?

html用css让div居中显示图片 css3怎么使div图片居中显示?

时间:2020-08-01 05:32:13

相关推荐

html用css让div居中显示图片 css3怎么使div图片居中显示?

CSS3的语法是建立在CSS原先版本基础上的,它允许使用者在标签中指定特定的HTML元素而不必使用多余的class、ID或JavaScript。

css3使div图片居中显示的方法:

1、利用display:table-cell,具体代码如下:

html代码如下:

css代码如下:.img_wrap{

width: 400px;

height: 300px;

border: 1px dashed #ccc;

display: table-cell; //主要是这个属性

vertical-align: middle;

text-align: center;

}

效果如下:

2、采用背景法:

html代码如下:

css代码如下:.img_wrap{

width: 400px;

height: 300px;

border: 1px dashed #ccc;

background: url(wgs.jpg) no-repeat center center;

}

效果如下图:

3、图片外面用个p标签,通过设置line-height使图片垂直居中:

html代码如下:

css代码如下:*{margin: 0px;padding: 0px}

.img_wrap{

width: 400px;

height: 300px;

border: 1px dashed #ccc;

text-align: center;}

.img_wrap p{

width:400px;

height:300px;

line-height:300px; /* 行高等于高度 */

}

.img_wrap p img{

*margin-top:expression((400 - this.height )/2);

/* CSS表达式用来兼容IE6/IE7 */

vertical-align:middle;

border:1px solid #ccc;

}

效果图如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。