css將div居中顯示圖片的方法:1.利用圖片的margin屬性將圖片水平居中,代碼如“margin:0 auto;”;2、利用div的padding屬性將圖片垂直居中,代碼如“padding-top:50px;”。
本教學操作環境:Windows7系統、Dell G3電腦、HTML5&&CSS3版。
css怎麼將圖片在div中置中顯示?
圖片在div中居中的方式
利用圖片的margin屬性將圖片水平居中,利用div的padding屬性將圖片垂直居中。
結構程式碼同上;
css程式碼如下:
div { width:300px; height:150px; background-color:#eee; padding-top:50px; border:#000 1px solid; } img {display:block; margin:0 auto;}
備註:
img是內嵌元素,要設定其margin屬性使其居中,就要將其轉換為區塊元素display:block;然後利用margin:0 auto;實現圖片的水平居中;(有的設計師為圖片再加個div標籤,然後透過div標籤的margin實現居中
想法:利用text-align屬性將圖片水平置中,然後設定padding-top的值使其垂直置中。
結構如下:
<style type="text/css"> div{ width:180px; height:180px; border:1px solid #000; position:relative; display:table-cell; text-align:center; vertical-align:middle; } div p{ position:static; +position:absolute; top:50%; } div img{ position:static; +position:relative; top:-50%; left:-50%; } </style> <div><p><img src="/static/imghwm/default1.png" data-src="0225/12986229678396.jpg" class="lazy" style="max-width:90%" style="max-width:90%" alt="css中怎麼將div居中顯示圖片" ></p></div><br> <div><p><img src="/static/imghwm/default1.png" data-src="0225/12986229678396.jpg" class="lazy" style="max-width:90%" style="max-width:90%" alt="css中怎麼將div居中顯示圖片" ></p></div>
方法二
<div> <img src="/static/imghwm/default1.png" data-src="images/tt.gif" class="lazy" style="max-width:90%" style="max-width:90%" / alt="css中怎麼將div居中顯示圖片" > </div> css样式如下: div { width:300px; height:150px; background-color:#ccc; border:#000 1px solid; text-align:center; padding-top:50px; }
(建議:css影片教學)
以上是css中怎麼將div居中顯示圖片的詳細內容。更多資訊請關注PHP中文網其他相關文章!