CSS如何实现图片上下垂直居中_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:40:56
Original
1336 people have browsed it

CSS如何实现图片上下垂直居中:

在很多时候需要将图片上下垂直居中,不过没有一个直接了当的属性能够实现此功能。当然实现图片上下垂直居中的方法有多重,下面就简单介绍一下其中的两种。

方法一:

使用margin方式,使图片在div中上下垂直居中。margin-top值的计算方式是:div的高度/2-图片高度/2。

代码实例如下:

<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content="http://www.softwhy.com/" /><title>蚂蚁部落</title><style type="text/css">div {  height:400px;  width:400px;  border:1px solid red;}div img {margin-top:127px;}</style></head><body><div><img src="mytest/demo/border.jpg" alt="蚂蚁部落"></div></body></html>
Copy after login

方式二:
代码实例如下:

<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content="http://www.softwhy.com/" /><title>蚂蚁部落</title><style type="text/css">div {  height:400px;  width:400px;  border:1px solid red;  vertical-align:middle;  display:table-cell;}</style></head><body><div><img src="mytest/demo/border.jpg" alt="蚂蚁部落" /></div></body></html>
Copy after login

以上代码实现了图片上下垂直居中,不过IE7浏览器并不支持此方式。
实现方式在div中添加了如下代码:

vertical-align:middle;display:table-cell;line-height:400px;
Copy after login

原文地址是:http://www.51texiao.cn/div_cssjiaocheng/2015/0503/593.html

最原始地址是:http://www.softwhy.com/

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!