css 使圖片水平垂直置中

來源:互聯網
上載者:User

標籤:

1.利用display:table-cell,具體代碼如下:

html代碼如下:

1 <div class="img_wrap">2   <img src="wgs.jpg">3 </div>

css代碼如下:

1 .img_wrap{2     width: 400px;3     height: 300px;4     border: 1px dashed #ccc;5     display: table-cell; //主要是這個屬性6     vertical-align: middle;7     text-align: center;8 }

效果如下:

 

2.採用背景法:

html代碼如下:

1 <div class="img_wrap"></div>

css代碼如下:

.img_wrap{    width: 400px;    height: 300px;    border: 1px dashed #ccc;    background: url(wgs.jpg) no-repeat center center;}

效果如:

 

3.圖片外面用個p標籤,通過設定line-height使圖片垂直置中:

html代碼如下:

1 <div class="img_wrap">2     <p><img src="wgs.jpg"></p>3 </div>

css代碼如下:

 1 *{margin: 0px;padding: 0px} 2 .img_wrap{ 3     width: 400px; 4     height: 300px; 5     border: 1px dashed #ccc; 6     text-align: center;} 7 .img_wrap p{ 8     width:400px; 9     height:300px;10     line-height:300px;  /* 行高等於高度 */11 }12 .img_wrap p img{13     *margin-top:expression((400 - this.height )/2);  /* CSS運算式用來相容IE6/IE7 */14     vertical-align:middle;15     border:1px solid #ccc;16 }

如下:

css 使圖片水平垂直置中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.