1.img 圖片內容在不同分辨率下居中顯示(如果隱藏多余,在img外面套一個div 設定overflow: hidden。div的大小就是img顯示區域的大小)
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> <style type="text/css"> body,div,p,a,img,ul,ol,li{ margin: 0; padding: 0; } #tab { overflow:hidden;
width:100%;要顯示的寬度 } #tab img{ overflow:hidden; width:1920px; height:447px; position: relative;
left:50%; 剛好在中間
margin-left:-960px;圖片寬度一半 } #tab>img:not(:first-child){ display:none; } </style> <script> //js幻燈片 window.onload = function(){ var images = document.getElementsByTagName('img'); var pos = 0; var len = images.length; setInterval(function(){ images[pos].style.display = 'none'; pos = ++pos == len ? 0 : pos; images[pos].style.display = 'inline'; },3000); }; </script> </head> <body> <div id="tab"> <img src="images/20160907_100403_003.jpg" width="1920px" height="447"/> <img src="images/20160907_100403_004.jpg" width="1920px" height="447"/> </div> </body> </html>
2.背景 圖片內容在不同分辨率下居中顯示(演示的圖片寬度是1920px,當圖片寬度大於顯示屏的寬度時)
<div class="index-panel-pic1"></div>
.index-panel-pic1{ background: url(../images/20160907_100403_000.gif) no-repeat center center; width:100%; height:650px; background-size: cover; }